master #12
			
				
			
		
		
		
	| 
						 | 
					@ -44,7 +44,7 @@ OK ! Now we can start the invoice class.
 | 
				
			||||||
This class will calculate the final price for a customer.
 | 
					This class will calculate the final price for a customer.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```python
 | 
					```python
 | 
				
			||||||
class Invoice:
 | 
					class Invoice(object):
 | 
				
			||||||
    def __init__(self, book, quantity, discountRate, taxRate, total):
 | 
					    def __init__(self, book, quantity, discountRate, taxRate, total):
 | 
				
			||||||
        self.book = book
 | 
					        self.book = book
 | 
				
			||||||
        self.quantity = quantity
 | 
					        self.quantity = quantity
 | 
				
			||||||
| 
						 | 
					@ -111,7 +111,7 @@ With these three classes, we respect the first principle of **SOLID**.
 | 
				
			||||||
### Open-Closed Principle
 | 
					### Open-Closed Principle
 | 
				
			||||||
 | 
					
 | 
				
			||||||
This principle says that classes are open for extension and closed to modification.
 | 
					This principle says that classes are open for extension and closed to modification.
 | 
				
			||||||
Extension mean news functionality and modification mean modifying your code.
 | 
					Extension mean news functionalities and modification mean modifying your code.
 | 
				
			||||||
If you want to add new functionalities, you are able to add it without manipulating the existing program.
 | 
					If you want to add new functionalities, you are able to add it without manipulating the existing program.
 | 
				
			||||||
If you touch the existing code, you have a risk to have news bugs.
 | 
					If you touch the existing code, you have a risk to have news bugs.
 | 
				
			||||||
So, if you want to add something else, you can use abstract classes and help of interface.
 | 
					So, if you want to add something else, you can use abstract classes and help of interface.
 | 
				
			||||||
| 
						 | 
					@ -131,4 +131,36 @@ class InvoicePersistence(object):
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
The _saveToDataBase_ method is used to save information in a Data Base.
 | 
					The _saveToDataBase_ method is used to save information in a Data Base.
 | 
				
			||||||
We have modified the _InvoicePersistence_ class.
 | 
					We have modified the _InvoicePersistence_ class.
 | 
				
			||||||
This class will be more difficult to make easily extendable.
 | 
					And this class will be more difficult to make easily extendable.
 | 
				
			||||||
 | 
					So, we violate the **OCP** convention.
 | 
				
			||||||
 | 
					If you want to respect this principle, you have to create a new class.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```python
 | 
				
			||||||
 | 
					class InvoicePersistence(abc.ABC):
 | 
				
			||||||
 | 
					    @abstractmethod
 | 
				
			||||||
 | 
					    def save(self, invoice):
 | 
				
			||||||
 | 
					        pass
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					The new _InvoicePersistence_ class has an abstract method.
 | 
				
			||||||
 | 
					So, if a class inherits the _InvoicePersistence_ class, you have to implement the _save_ method.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					And for example, we will create a _DataBasePersistence_ class, and this class inherit the abstract _InvoicePersistence_ class.
 | 
				
			||||||
 | 
					```python
 | 
				
			||||||
 | 
					class DatabasePersistence(InvoicePersistence):
 | 
				
			||||||
 | 
					    def __init__(self, invoice):
 | 
				
			||||||
 | 
					        self.invoice = invoice
 | 
				
			||||||
 | 
					        self.save(self.invoice)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def save(self, invoice):
 | 
				
			||||||
 | 
					        print("Save in database ...")
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					Let's make the same thing with _FilePersistence_ class.
 | 
				
			||||||
 | 
					```python
 | 
				
			||||||
 | 
					class FilePersistence(InvoicePersistence):
 | 
				
			||||||
 | 
					    def __init__(self, invoice):
 | 
				
			||||||
 | 
					        self.invoice = invoice
 | 
				
			||||||
 | 
					        self.save(self.invoice)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def save(self, invoice):
 | 
				
			||||||
 | 
					        print("Save to file ...")
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue