While studying design patterns, I found the following questions on a textbook, and I am having a hard time figuring out the answer.
- 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.
- Be able to replace the implementation of an interface at run time.
Here, the “run-time” requirement is really giving me a headache.
I think this could be the Strategy Design pattern, in that 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.
- Extend the functionality of objects dynamically during run time.
Again, the “run-time” 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.
- 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.