diff --git a/Intro_to_Solid.md b/Intro_to_Solid.md index 6811078..e668b80 100644 --- a/Intro_to_Solid.md +++ b/Intro_to_Solid.md @@ -177,9 +177,162 @@ That will be more simple to do extension. This convention tells us to create a substitutable subclass for their parents. So, if you want to create an object in the subclass, you have to be able to pass it in the interface. +Let's make an example ! +We will write a _Walk_ class and his subclass, _Jump_ class. +```python +class Walk(abc.ABC): + def __init__(self): + abc.ABC.__init__(self) + @abc.abstractmethod + def Speed(self): + pass + @abc.abstractmethod + def Ahead(self): + pass + '''the player walk ahead''' + + @abc.abstractmethod + def Behind(self): + pass + '''the player walk behind''' +``` +And the _Jump_ subclass. +```python +class Jump(Walk): + def __init__(self): + pass + + def Speed(self): + pass + + def Ahead(self): + pass + + def Behind(self): + pass +``` +As you can see, the _Jump_ subclass has all abstract method of _Walk_ class. +But we have a big problem ! +The _Jump_ subclass need a new method, the _height_ method. +And he does not need the _Ahead_ and _Behind_ method. +If you remove abstract method and add a new method in _Jump_ subclass, you will be in a big trouble. +This subclass will be different about mother class. +The simple way to resolve this trouble is to create a new mother class for _Walk_ and _Jump_ class. + +```python +class Movement(abc.ABC): + def __init__(self): + abc.ABC.__init__(self) + + @abc.abstractmethod + def Speed(self): + pass + + @abc.abstractmethod + def Animation(self): + pass + + @abc.abstractmethod + def Direction(self): + pass +``` +The _Movement_ class will be the mother class of _Walk_ and _Jump_ classes. +These subclasses will have three methods (_Speed_, _Animation_ and _Direction_ methods). +The problems that we had is resolved with the _Direction_ method. +With this method, you have choices to go ahead, behind, height, etc. + +
+ +Subclasses + +The _Walk_ subclass: + +```python +class Walk(Movement): + def __init__(self): + pass + + def Speed(self): + pass + + def Animation(self): + pass + + + def Direction(self): + pass + +``` +And the _Jump_ subclass: + +```python +class Jump(Movement): + def __init__(self): + pass + def Speed(self): + pass + + def Animation(self): + pass + + def Direction(self): + pass + +``` + +
+ ### Interface Segregation Principle The _Interface Segregation Principle_ means that all interfaces have to be separated. That means that clients has not functions that they do not need. +OK ! So, let's make an example with client manager. + +```python +class GoodCustomer(abc.ABC): + def __init__(self): + abc.ABC.__init__(self) + + @abc.abstractmethod + def FirstName(self): + pass + @abc.abstractmethod + def LastName(self): + pass + @abc.abstractmethod + def Address(self): + pass + + @abc.abstractmethod + def Work(self): + pass +``` +The _GoodCustomer_ mother class is a class where good customer information are saved. + +```python +class BadCustomer(GoodCustomer): + def __init__(self): + pass + + def FirstName(self): + pass + + def LastName(self): + pass + + def Address(self): + pass + + def Work(self): + pass +``` +The _BadCustomer_ subclass is a class where bad customer information are saved. +For this example, we don't want to know the addresses of bad guys. +So what can we do ? + +You have to create a new mother class for these two classes like the preceding example. ### Dependency Inversion Principle + +This convention says that all classes that we use depends on the interface or abstract classes. +Like precedents example, all methods was abstracts.