The company talked about DI (Dependency Injection), I didn't know Imaichi-chan, so I decided to study
・ Dependency injection (literal translation) ・ Promotion of componentization by reducing the degree of coupling ・ Efficiency of unit tests · Reduced reliance on specific frameworks ・ There seems to be a DI container (hey)
Personally, I was attracted to the fact that unit testing became easier.
Later, even if it was (apparently) componentized at the time of implementation There is not much merit if they depend on each other.
(The image that you can't do it with Sujiko and how much you have to do is really useless)
Anyway, I'll write it.
First, an example of being dependent.
no_di.py
class Country:
def getCountry(self):
return "Japanese"
class Position:
def getPosition(self):
return 'SuperStar'
class HorseName:
def getName(self):
return 'Caesario'
class GreatRealCondition:
def __init__(self):
self.c = Country()
self.p = Position()
self.h = HorseName()
def getGreatRealCondition(self):
return self.c.getCountry() + "\n" + self.p.getPosition() + "\n" + self.h.getName() + "!"
if __name__ == '__main__':
g = GreatRealCondition()
print(g.getGreatRealCondition())
Execution result.
Japanese
SuperStar
Caesario!
If you don't know what the text is, youtube Search for "Japanese Superstar Cesario!"
The GreatRealCondition class
・ Country class ・ Position class ・ HorseName class
It has a close relationship with these classes (** Three Cs **).
So it can be said that these classes look disjointed and are actually almost one class.
It can't be reused, and it can't be tested in the first place (you can't change the value or anything at the moment). You can change the value by putting a setter in the Country class etc., but it is no longer a test of the GreatRealCondition class.
So, try lowering the dependency.
on_di.py
class Country:
def getCountry(self):
return "Japanese"
class Position:
def getPosition(self):
return 'SuperStar'
class HorseName:
def getName(self):
return 'Caesario'
class GreatRealCondition:
def __init__(self, c: Country, p: Position, h: HorseName):
self.c = c
self.p = p
self.h = h
def getGreatRealCondition(self):
return self.c.getCountry() + "\n" + self.p.getPosition() + "\n" + self.h.getName() + "!"
if __name__ == '__main__':
c = Country()
p = Position()
h = HorseName()
g = GreatRealCondition(c, p, h)
print(g.getGreatRealCondition())
Execution result.
Japanese
SuperStar
Caesario!
The result was the same as before.
The big change here is
** GreatRealCondition class no longer has other classes **
Exhausted.
The GreatRealCondition class, which relied on the three-cs class and had no other use,
By having 3 dense classes in the constructor, various uses were possible. (Actually, I thought I'd try various patterns, but it's going to be long, so this time)
Well, I used the constructor to "inject dependencies".
At first it looks like this.
This time I injected using the constructor,
It seems that there are various methods such as injection with setter and injection by defining the interface, so I will definitely try it.
Also, python may be quite DI troublesome. (I'm just not used to it)
Thanks!!
Here is a helpful article. Thank you very much. https://qiita.com/mkgask/items/d984f7f4d94cc39d8e3c
Recommended Posts