Most design patten books say we should “Favor object composition over class inheritance.” But can anyone give me an example that inheritance is better than object composition. Answer In Java, whenever you inherit from a class, your new class also automatically becomes a subtype of the original class type. Since it is a subtype, it needs to adhere to the
Tag: design-patterns
Why do we need immutable class?
I am unable to get what are the scenarios where we need an immutable class. Have you ever faced any such requirement? or can you please give us any real example where we should use this pattern. Answer The other answers seem too focused on explaining why immutability is good. It is very good and I use it whenever possible.
Using Command Design pattern
Can anyone explain with a simple example the Command Pattern? I tried searching on the internet, but I got confused. Answer For the most part, commands are immutable and contain instructions that encapsulate a single action that is executed on demand. You might also have a RuntimeCommand that accepts instructions upon execution, but this delves more into the Strategy or
Singleton Design Pattern: Pitfalls [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago. Improve this question Not sure of downfalls using strict global state implementation. When is singleton not ideal for an application? Answer Singleton is
What is an efficient way to implement a singleton pattern in Java? [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago. Improve this question What is an efficient way to implement a singleton design pattern in Java? Answer Use an enum: Joshua Bloch explained