Design Patterns in Object Oriented Programming(OOP)
Design Patterns in OOP:
In Object Oriented Programming (OOP) pattern consists of description of certain objects and classes along with the attributes and dependencies of the class. It also provides the general purpose on solving the problem. So, patterns are reusable solutions to commonly occurring problems that are encountered repeatedly. Furthermore design patterns speed up the development process by providing tested and proven development paradigm.It became popular when it came in a formalized form. For the first time patterns were published with C++ and Smalltalk code sample. Design Patterns can be implemented in any programming languages and is very popular with Java and C# .Similarly design patterns provide better software interaction by allowing developers to communicate using well understood and known names.
But certain patterns are not necessary in functional languages like Scala and it is difficult to implement in vast range of problems.
Design patterns includes information like:
- Name of the pattern
- Problem solved by the pattern
- Context of the occurrence of the problem
- Solution of the problem
- Context of the solution
- Uses and related patterns
There are basically three types of design patterns and their subtypes.
1. Creational Patterns:
Creational Patterns implement and support the creation of objects. It’s all about class instantiation. This pattern can be subdivided into class-creation patterns and object-creational patterns. Class creational patterns use inheritance effectively in the instantiation process whereas object-creation class use delegation effectively to get the task completed.
Some of its pattern includes:
Singleton: In Singleton pattern only single instance of a class is created and provided with global access to that object.
Factory Method: Creates instance of several families of classes without exposing instantiation logic to the client
Abstract Factory: Offers interface for creating family of related objects without specifying their concrete class.
Builder: Lets subclasses decide which class to instantiate and allows better control over the construction process. It enables more readable object creation and lets us provide the fields that are specifically required.
Prototype: Uses the clone method to duplicate existing instances to be used as a prototype instance for creating new objects. This method is used when the new instance is expensive to create and the required objects are similar to existing objects.
2. Structural Patterns:
Structural Patterns helps to define relationships between objects.
Some of its patterns include:
Adapter: Works between two independent and incompatible interfaces by converting the interface into another interface client expect.
Bridge: Represent part-whole hierarchies by composing objects into tree structures. Individual objects and composition of objects are treated uniformly by the client in Bridge.
Composite: Treats a group of objects as a single object. Used in tree like structures where the parent node is dependent on child node.
Decorator: The Decorator pattern helps to insert functionality to an object dynamically during the run time.
Flyweight: Flyweight pattern is implemented if lots of objects from a single class needs to be constructed where the objects are shared to limit memory overload.
Proxy: In Proxy pattern an object is a proxy to something else and can control it. It allows the creation and access of anything like a large object in memory, file or other resources.
3. Behavioral Patterns:
Behavioral patterns defines the behavior of the objects. It is concerned with the communication between objects.
Some of its pattterns include:
Chain of responsibility: This pattern defines a way for passing request between a chain of objects.
Command: In this pattern an objects is used for encapsulating every information required for performing action or trigger events in future.
Interpreter: Interpreter pattern defines a representation for the grammar providing a way to include language attributes in the program.
Mediator: Pattern for defining cooperation and communication between classes.
State: Modify an objects behavior when its state changes.
Memento: This pattern is required when a certain state of an object needs to saved for later usage.
State: State pattern lets the object alter the internal behavior when the state changes.
Visitor: Visitor pattern allows the developer to add one or more operation in an object during the run time.
Reference: Click Here
Also Read: Programming Language to learn in 2019