This is a training repo to implement different kinds of design patterns for practicing.
- To have only one instance of a class to provide more storage in memory.
By making a private constructor to prevent creating instances using constructor, and replace that with a function to deal with the same private instance in your class.
You can lock the creation of the instance in case of multithreading.
- Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Copy heavy initialization objects instead of creating it.
This helps to save time consuming to get data every time I want it -from database for example -.
- Separate the construction of a complex object from its representation so that the same construction processes can create different representations.
Build complex objects step by step
... Like string builder class.
Keep the class user away from the complex implementation of the object.
- Define an interface for creating an object, but let subclasses decide which class to instantiate.
Create object without exposing the creation logic to the client and refer to newly created object using a common interface
.
Make an interface to be implemented by the different kinds of classes and to be used to make the loginc needed by another class with will make the required logic.
Make the Factory class deal with different classes to include their functions and call the facory object only one time to use for different cases.
- Provide a surrogate or placeholder for another object to control access to it.
Like a gateway for an object
The client calls the proxy instead of web service, so if the service changed, we deal with the proxy not the client
Remote Proxy
Get data from database using the proxy, and the client deal with the proxy not the databaseVirtual Proxy
Protect the object from the client to control it yourself away from the clientProtection Proxy
.
Allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class.
Thedecorator pattern
is often useful for adherting to theSingle Resposiblity Principle
,
as it allows functionality to be divided between classes with unique areas of concern.
Convert the interface of a class into another interface that clients expect.
TheAdapter pattern
lets classes work together that could not otherwise because ofincompatible interfaces
.
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 fine-grained objects efficiently.
Flyweight
lets you fit more objects into the available amount of RAM by sharing common parts of state between multiple objects instead of keeping all of the data in each object.
- Can we make more than handler for a given request?
- How can we handle a request using sequence of steps?
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
Chain
the receiving objects and pass the request along the chain until an object handles it.