0
Isn't it bad if we use unique pointer in this case
Hi I tried to create a scenario of Chain of responsibility pattern as below: https://www.sololearn.com/compiler-playground/cBV3c5sLaUWn Is shared_pointer usage proper? I know it takes more overhead compared to unique pointer but this is the only choice. Right? If we could have used unique pointer , then ptrLevel1->setNextApprover(ptrLevel2); will enforce us to move ptrLevel2. Now, ptrLevel2 is not valid and cannot set third level approver on this null pointer. Is this correct? https://code.sololearn.com/cBV3c5sLaUWn/?ref=app
2 Antworten
+ 2
Certainly, in the Chain of Responsibility pattern, using shared_ptr is a suitable choice because it allows multiple handlers to share responsibility for processing requests without ownership issues, which is common in this pattern. Using unique_ptr would restrict you from sharing handlers, making it less appropriate for this scenario.
+ 1
Thanks