Reddit Experience · May 2021

Appropriate design pattern for four situations

9 upvotes 5 replies

Interview Experience

While studying design patterns, I found the following questions in a textbook, and I am having a hard time figuring out the answers. ​ **1. Re-use an existing class but provide new function

Full Details

While studying design patterns, I found the following questions in a textbook, and I am having a hard time figuring out the answers. ​ 1. Re-use an existing class but provide new functionality. I believe this could be the Decorator pattern: you can wrap the existing class in another one that provides new functionality. 2. Be able to replace the implementation of an interface at runtime. Here, the "run-time" requirement is really giving me a headache. I think this could be the Strategy Design pattern, because it allows for a client to ask the Context which strategy implementation to use. However, the "run-time replacement" feels a bit of a stretch here. 3. Extend the functionality of objects dynamically during runtime. Again, the "runtime" requirement feels problematic to me. I could argue that the Decorator extends the functionality of objects, and if you have written all the Decorators you need you can pick one of them at run-time. 4. Decouple clients of a system X from dependencies on subsystems of X. This feels like the Bridge pattern, where you split a class into loosely coupled classes.

Free preview. Unlock all questions →

Topics

Oop