The Class Adapter The Object Adapter The Class Adapter From dog import Dog Class Creature (object ): "The base class for creatures in 2D Land """ Def make_noise (self ): """ This is a technique to fake an ABC In Python 2.X """ Raise NotImplementedError Class Person (Creature ): "A representation of a person in 2D Land """ Def _ init _ (self, name ): Self. name = name Def make_noise (self ): Return "hello" Class DogClassAdapter (Creature, Dog ): "Adapts the Dog class through multiple inheritance """ Def _ init _ (self, name ): Dog. _ init _ (self, name) Def make_noise (self ): """ Provide the 'make _ noise 'method that The client expects """ Return self. bark () The Object Adapter Class DogObjectAdapter (Creature ): "Adapts the Dog class through encapsulation """ Def _ init _ (self, canine ): Self. canine = canine Def make_noise (self ): "This is the only method that's adapted """ Return self. canine. bark () Def _ getattr _ (self, attr ): "Everything else is delegated to the object """ Return getattr (self. canine, attr) * Simple * From dog import Dog Class Person (object ): "A representation of a person in 2D Land """ Def _ init _ (self, name ): Self. name = name Def make_noise (self ): Return "hello" Class DogAdapter (object ): "Adapts the Dog class through encapsulation """ Def _ init _ (self, canine ): Self. canine = canine Def make_noise (self ): "This is the only method that's adapted """ Return self. canine. bark () Def _ getattr _ (self, attr ): "Everything else is delegated to the object """ Return getattr (self. canine, attr) Def click_creature (creature ): """ React to a click by showing the creature's Name and what is says """ Return (creature. name, creature. make_noise ()) Test From dog import Dog From listing3 import Person, DogAdapter Def exercise_system (): Person = Person ("Bob ") Canine = DogAdapter (Dog ("Fido ")) For critter in (person, canine ): Print critter. name, "says", critter. make_noise () If _ name _ = "_ main __": Exercise_system () |