Before learning desing pattern we should take deep knowledge about Object Oriented Programming.
The key features of encapsulation:
- An objects behavior is kept hidden from the outside world.
- Client not directly change internal state acting on them rather client can send request to object. According to this request object change state such as set and get .
- In python doesn't have public, private and protected keywords but accessibility can be made using __(double underscore) in the variable or function name.
The key features of polymorphism:
- Two types of polymorphism:
- An object provides different implementation of the method based on input parameters.
- The same interface can be used by objects different types.
- In python polymorphism build in feature.For example: + operator can add two or more integers and also concate string
The key features of inheritance:
- Inheritance indicates that one class derives functionality from the parent class.
- Inheritance is described as an option to reuses functionality of parent class.
- Inheritance creates hierarchy via the relationships among objects of different classes.
The key features of Abstruct:
- It provides you simpe interface to the client , where the clients can interact with class objects and call methods defined in the interface.
- Abstruct can create abstruct method as well as can implement method inside class but interface can't do that.
The key features of Composition:
- It is way to combine objects or classes into more complex data structure or software implementation.
- In composition, an object is used to call member functions in other modules thereby making base functionality available across modules without inheritance.
class A:
def a1(self):
print("a1")
class B:
def b(self):
print("b")
A().a1()
obj = B()
obj.b()
Single responsibility principle states that a class should have only one reason to change.
If a class taking care of two functionality, it is better to split them.
The open/close principle state that classes or objects and methods are open for extension but close from modifications.
That means when we develop our software make sure that our writing classes or modules in a generic way. That's you can do extend behavior of class but not change to class itself.
For example: In Django user has to create a class implementation by extending the base abstract class to implement the required behavior insteat of changing the abstract class.
The inversion of control principle states that hight level modules shouldn't be depend on low level modules; they should both dependent on abstractions. Details should depend on abstractions and not the other way round.
This principle suggested that any two modules shouldn't be dependent on each other(loose coupling or decouple).
This priciciples also suggested that the details of your class should represent the abstractions.
This principle states that clients shouldn't be forced to depend on interface they don't use.
This principle suggestes that Develop mothods relevant to functionality. If there any method that is not related to the interface, the class dependent on the interface has to implement it unnecessary.
This principle states that derived classes must be able to completely substitute the base classes.
- They are reusable across multiple projects.
- The architectural level problems can be solved.
- They are time-tested and well-proven, which is the experience of developers and architects.
- They have reability and dependence.
- Creational Patterns
- Structural Patterns
- Behavioral Patterns
- They work on basis of how objects can be created.
- They isolate the details of objects creations.
- Code is independent of the type of object
- They design the structure of objects and classes so that they can compose to achieve larger results.
- The focus is on simplifiying the structure and identifying the relationship between classes and objects.
- They focus on class inheritance and composition.