-In situations such as Dependency Reversal Principle (DIP), that is, if you want to implement an abstraction-dependent implementation, try implementing it in Python. --Try using the TypeHints type annotations available from Python 3.5. --Python doesn't have Interface as a language feature, so it uses the `` `abc``` library. ――Type annotations don't get angry even if the types are different, so just write it like that. .. ..
--Environment - python: v.3.6.8
--animal module
--Implement an Interface class called Animal
and the Cat
class that inherits it.
--The Animal
class has acry ()
method.
--In a concrete class such as Cat
, print each concrete animal cry withcry ()
method. It is the one that comes out when explaining polymorphism.
--Although a Dog
class is also prepared, this class does not inherit Animal
.
--myclass module
--Implement a MyClass
class that takes a concrete class of Animal
and hits cry ()
method. Here in My Class
Cat
class. (I want to modify the concrete class and prevent it from being affected even if another concrete class arrives.Animal
--main module
--Concrete the Cat
and Dog
classes and pass them to the method of MyClass
.--Try to create Interface class with inherited class using `` `abc``` library
class Animal(object, metaclass=abc.ABCMeta):
"""Interface class that represents an animal.
"""
@abc.abstractmethod
def cry(self):
"""Animal bark interface
"""
pass #No specific implementation
--Try to create a concrete class by inheriting the Animal
class
class Cat(Animal):
"""Class representing cats
Args:
Animal:Interface class
"""
def cry(self):
"""Concrete method of cat barking
"""
print('meow')
--Check the behavior of the Animal
and Cat
classes
--Instantiate the Cat
class and try executingcry ()
```
cat = Cat() cat.cry() meow
````
--I get angry when I try to instantiate the Interface class as it is
```shell
>>> animal = Animal()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Animal with abstract methods cry
```
--Even if you don't implement method in the concrete class, you get angry
```shell
class Cat(Animal): ... """Class representing cats ... Args: ... Animal:Interface class ... """ ... def nocry(self): ... """Verification method ... """ ... print('???') ...
cat = Cat() Traceback (most recent call last): File "
", line 1, in TypeError: Can't instantiate abstract class Cat with abstract methods cry
```
--Similar at first glance, but inherits the Animal
class *** Not *** Try to create a Dog
class
class Dog(object):
"""A class that represents a dog. Interface is not inherited
"""
def cry(self):
"""Specific method of dog barking
"""
print('bow wow')
cry ()
method--Use type annotation to specify that the argument is a class that inherits Animal
Interface
from animal import Animal
class MyClass(object):
def run(self, animal: Animal) -> None:
animal.cry()
--Prepare logic to instantiate the actual concrete class and delegate the processing to MyClass
from animal import Cat, Dog
from myclass import MyClass
myclass = MyClass()
cat = Cat()
dog = Dog()
myclass.run(cat)
myclass.run(dog)
$python main.py
meow
bow wow
--After all, dogs are barking (´ ・ ω ・ `)
--Type annotation is just an annotation, so it works even if it is not a derived class of Interface class. ――For the time being, you can do something like that (aside from the meaning) ――It's just a memo of a simple experiment, but I hope it will be helpful for you. .. .. ――Is it possible to give a meaning like "Please pay attention to the Interface class when implementing with multiple people"?
Recommended Posts