|
| 1 | +# Learning Python Design Patterns |
| 2 | + |
| 3 | +Author: Chetan Giridhar |
| 4 | + |
| 5 | +[Available here](https://www.amazon.com/Learning-Python-Design-Patterns-Second-ebook/dp/B018XYKNOM) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +# Ch1. Introduction to design patterns |
| 10 | + |
| 11 | +## Understanding object-oriented programming |
| 12 | +- Concept of *objects* that have attributes (data members) and procedures (member functions) |
| 13 | +- Procedures are responsible for manipulating the attributes |
| 14 | +- Objects, which are instances of classes, interact among each other to serve the purpose of an application under development |
| 15 | + |
| 16 | +### Classes |
| 17 | +- Define objects in attributes and behaviors (methods) |
| 18 | +- Classes consist of constructors that provide the initial state for these objects |
| 19 | +- Are like templates and hence can be easily reused |
| 20 | + |
| 21 | +### Methods |
| 22 | +- Represent the behavior of the object |
| 23 | +- Work on attributes and also implement the desired functionality |
| 24 | + |
| 25 | +## Major aspects of OOP |
| 26 | + |
| 27 | +### Encapsulation |
| 28 | +- An object's behavior is kept hidden from the outside world or objects keep their state information private |
| 29 | +- Clients can't change the object's internal state by directly acting on them |
| 30 | +- Clients request the object by sending requests. Based on the type, objects may respond by changing their internal state using special member functions such as `get` and `set` |
| 31 | + |
| 32 | +### Polymorphism |
| 33 | +- Can be of two types: |
| 34 | + - An object provides different implementations of the method based on input parameters |
| 35 | + - The same interface can be used by objects of different types |
| 36 | +- In Python polymorphism is a feature built-in for the language (e.g. + operator) |
| 37 | + |
| 38 | +### Inheritance |
| 39 | +- Indicates that one class derives (most of) its functionality from the parent class |
| 40 | +- An option to reuse functionality defined in the base class and allow independent extensions of the original software implementation |
| 41 | +- Creates hierarchy via the relationships among objects of different classes |
| 42 | +- Python supports multiple inheritance (multiple base classes) |
| 43 | + |
| 44 | +### Abstraction |
| 45 | +- Provides a simple interface to the clients. Clients can interact with class objects and call methods defined in the interface |
| 46 | +- Abstracts the complexity of internal classes with an interface so that the client need not be aware of internal implementations |
| 47 | + |
| 48 | +### Composition |
| 49 | +- Combine objects or classes into more complex data structures or software implementations |
| 50 | +- An object is used to call member functions in other modules thereby making base functionality available across modules without inheritance |
| 51 | + |
| 52 | +## Object-oriented design principles |
| 53 | + |
| 54 | +### The open/close principle |
| 55 | +> **Classes or objects and methods should be open for extension but closed for modifications** |
| 56 | +
|
| 57 | +- Make sure you write your classes or modules in a generic way |
| 58 | +- Existing classes are not changed reducing the chances of regression |
| 59 | +- Helps maintain backward compatibility |
| 60 | + |
| 61 | +### The inversion of control principle |
| 62 | +> **High-level modules shouldn't be dependent on low-level modules; they should be dependent on abstractions. Details should depend on abstractions and not the other way round** |
| 63 | +
|
| 64 | +- The base module and dependent module should be decoupled with an abstraction layer in between |
| 65 | +- The details of your class should represent the abstractions |
| 66 | +- The tight coupling of modules is no more prevalent and hence no complexity/rigidity in the system |
| 67 | +- Easy to deal with dependencies across modules in a better way |
| 68 | + |
| 69 | +### The interface segregation principle |
| 70 | +> **Clients should not be force to depend on interfaces they don't use** |
| 71 | +
|
| 72 | +- Forces developers to write thin interfaces and have methods that are specific to the interface |
| 73 | +- Helps you not to populate interfaces by adding unintentional methods |
| 74 | + |
| 75 | +### The single responsibility principle |
| 76 | +> **A class should have only one reason to change** |
| 77 | +
|
| 78 | +- If a class is taking care of two functionalities, it is better to split them |
| 79 | +- Functionality = a reason to change |
| 80 | +- Whenever there is a change in one functionality, this particular class needs to change, and nothing else |
| 81 | +- If a class has multiple functionalities, the dependent classes will have to undergo changes for multiple reasons, which gets avoided |
| 82 | + |
| 83 | +### The substitution principle |
| 84 | +> **Derived classes must be able to completely substitute the base classes** |
| 85 | +
|
| 86 | +## The concept of design patterns |
| 87 | + |
| 88 | + |
| 89 | + |
0 commit comments