What is Software design pattern?
in Software industry it’s myth what is Software design pattern? and how to know which one is suitable for what module. can we create my own design pattern? to overcome this myth i had go through few article and tried to collect at one place, so let’s start.
Myth 1:- For a good architecture all design patterns have to be implemented in a project.
Fact: – Patterns come out naturally and is completely on-demand.
“Time tested Solution for recurring architecture problems”.
But frankly this definition will not hold true as you start implementing and reading design pattern. At one moment of time you will see design pattern is all about good OOP ( Object oriented programming) principles.
So let me put my definition as per my understanding and experience. I can guarantee that when we start running through all design patterns this definition would be more clear.
“Time tested Solution for recurring OOP problems”.
Categories of software design pattern
Design patterns were originally grouped into the categories: creational patterns, structural patterns, and behavioral patterns, and described using the concepts of delegation, aggregation, and consultation. For further background on object-oriented design, see coupling and cohesion, inheritance, interface, and polymorphism. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model–View–Controller pattern.
Creational patterns further categorized into subcategories:
Ensure a class has only one instance, and provide a global point of access to it.
When to use:
Singleton pattern should be used when we must ensure that only one instance of a class is created and when the instance must be available through all the code. A special care should be taken in multi-threading environments when multiple threads must access the same resources through the same singleton object.
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
When to use:
- A system should be configured with one of multiple families of products
- A system should be independent of how its products are created, composed and represented
- Products from the same family should be used all together, products from different families should not be used togheter and this constraint must be ensured.
- Only the product interfaces are revealed, the implementations remains hidden to the clients.
Separate the construction of a complex object from its representation, allowing the same construction process to create various representations.
Define an interface for creating a single object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses (dependency injection).
Tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. This pattern appears in the GoF catalog as “virtual proxy”, an implementation strategy for the Proxy pattern.
When to use:
Basically, we’ll use an object pool whenever there are several clients who needs the same stateless resource which is expensive to create. like Database Connections, Remote Objects
Specify the kinds of objects to create using a prototypical instance, and create new objects from the ‘skeleton’ of an existing object, thus boosting performance and keeping memory footprints to a minimum.
Structural patterns further categorized into subcategories:
Convert the interface of a class into another interface clients expect. An adapter lets classes work together that could not otherwise because of incompatible interfaces. The enterprise integration pattern equivalent is the translator.
Decouple an abstraction from its implementation allowing the two to vary independently.
Provide a surrogate or placeholder for another object to control access to it.
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
Use sharing to support large numbers of similar objects efficiently.
Behavioral Design Patterns
Behavioral Design Patterns further categorized into subcategories:
Chain of Responsibly
It avoids attaching the sender of a request to its receiver, giving this way other objects the possibility of handling the request too.
The objects become parts of a chain and the request is sent from one object to another across the chain until one of the objects will handle it.
Encapsulate a request in an object, Allows the parameterization of clients with different requests and Allows saving the requests in a queue.
Without violating encapsulation, capture and externalize an object’s internal state allowing the object to be restored to this state later.
Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language / Map a domain to a language, the language to a grammar, and the grammar to a hierarchical object-oriented design.
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses / Template Method lets subclasses redefine certain steps of an algorithm without letting them to change the algorithm’s structure.
Represents an operation to be performed on the elements of an object structure / Visitor lets you define a new operation without changing the classes of the elements on which it operates.
Provide an object as a surrogate for the lack of an object of a given type. / The Null Object Pattern provides intelligent do nothing behavior, hiding the details from its collaborators.