DESIGN PATTERNS – Good or Bad?
Design patterns are becoming part of major controversy in the programming world, mainly because some developers rely too much on them and end up over-using them.
But design patterns are meant to be used in the right situations.
When used right, design patterns can make developers more efficient by providing solutions to repeating problems, and preventing developers from trying to reinvent solutions that already exist.
Design patterns can be divided into creational patterns, structural patterns, and behavioral patterns.
- Creational patterns deal with object creation, increasing flexibility and reusability of code. Some creational patterns are factory method, prototype, singleton.
- Factory Method: it says to define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate
- Prototype: cloning of an existing object instead of creating a new one and can also be customized as per the requirement. It hides the complexity of creating an object and allows adding or removing objects at runtime.
- Singleton: define a class that has only one instance and provides a global point of access to it. Since it creates only one instance of a class it saves memory. An example of the use of a singleton pattern is when a user logs in to a website.
- Structural patterns deal with relationships between entities. Some of the structural patterns are adapter pattern, decorator pattern, facade pattern.
- Adapter Pattern: converts the interface of a class into another interface that a client wants. It can allow incompatible objects to interact and promotes reusability.
- Decorator Pattern: attach flexible additional responsibilities to an object dynamically. It adds the responsibility for certain objects without affecting the other.
- Facade Pattern: just provide a unified and simplified interface to a set of interfaces in a subsystem, therefore it hides the complexities of the subsystem from the client. It hides the complexity of a system from users.
- Behavioral patterns deal with communication between objects. Some of the behavioral patterns are observer pattern, strategy pattern, template pattern, interpreter pattern, state pattern.
- Observer Pattern: just define a one-to-one dependency so that when one object changes state, all its dependents are notified and updated automatically. It says that change in one object must be reflected in the other.
- Strategy Pattern: defines a family of functionality, encapsulate each one, and make them interchangeable. This pattern can be used for example when there are variations in an algorithm.
- Template Pattern: just define the skeleton of a function in an operation, deferring some steps to its subclasses. The main benefit of this pattern is code reusability.
- Interpreter pattern: to define a representation of grammar of a given language, along with an interpreter that uses this representation to interpret sentences in the language.
- State pattern: the class behavior changes based on its state. When the behavior of an object depends on its state and it must be able to change its behavior at runtime according to the new state.
These are just some examples of design patterns that exist.
o whether you think design patterns are good or bad, there is no negating that they can be useful. They provide a good blueprint for programming and dealing with problems when it comes to code complexity and usability.