Examples of Adapter mode in Python Design Mode Programming, pythonadapter
Converts an interface of a class to another interface that the customer wants. So that those classes that cannot work together due to interface incompatibility can work together.
Application Scenario: You want to reuse some existing classes, but the interfaces are inconsistent with the requirements of the Reuse Environment.
Mode features: converts an interface of a class into another interface that the customer wants.
Category: Class adapter (inherited by multiple methods) and Object Adapter.
In the following example, the user uses a class method through the adapter.
class Target: def Request(): print "common request."class Adaptee(Target): def SpecificRequest(self): print "specific request."class Adapter(Target): def __init__(self,ada): self.adaptee = ada def Request(self): self.adaptee.SpecificRequest()if __name__ == "__main__": adaptee = Adaptee() adapter = Adapter(adaptee) adapter.Request()
Class diagram:
Instance:
Let's take a look at a simple Adapter example.
# Encoding = UTF-8 ## by panda # adapter mode def printInfo (info): print unicode (info, 'utf-8 '). encode ('gbk') # Player class Player (): name = ''def _ init _ (self, name): self. name = name def Attack (self, name): pass def Defense (self): pass # Forward class Forwards (Player): def _ init _ (self, name ): player. _ init _ (self, name) def Attack (self): printInfo ("Forward % s Attack" % self. name) def Defense (self, name): printInfo ("Forward % s Defense" % self. name) # Center (target class) class Center (Player): def _ init _ (self, name): Player. _ init _ (self, name) def Attack (self): printInfo ("center % s Attack" % self. name) def Defense (self): printInfo ("center % s Defense" % self. name) # guard class Guards (Player): def _ init _ (self, name): Player. _ init _ (self, name) def Attack (self): printInfo ("Defender % s Attack" % self. name) def Defense (self): printInfo ("Defender % s Defense" % self. name) # foreign Center (to be adapted) # center class ForeignCenter (Player): name = ''def _ init _ (self, name): Player. _ init _ (self, name) def ForeignAttack (self): printInfo ("Foreign center % s attack" % self. name) def ForeignDefense (self): printInfo ("Foreign center % s defense" % self. name) # Translation (adaptation class) class Translator (Player): foreignCenter = None def _ init _ (self, name): self. foreignCenter = ForeignCenter (name) def Attack (self): self. foreignCenter. foreignAttack () def Defense (self): self. foreignCenter. foreignDefense () def clientUI (): B = Forwards ('battier ') m = Guards ('mcged') ym = Translator ('yaking') B. attack () m. defense () ym. attack () ym. defense () return if _ name _ = '_ main _': clientUI ();
Articles you may be interested in:
- Measure the test taker's knowledge about the observer mode and Strategy Mode in Python Design Mode Programming.
- Simple program example of interpreter mode in Python Design Mode Programming
- An example of how to use the design pattern in Python
- Analysis of the key points of the Decorator pattern in Python
- Instance parsing Python Design Mode Programming-Application of Bridge Mode
- Application Example of prototype in design mode in Python Program
- In-depth analysis of the use of builder mode in Python Design Mode Programming
- An example of how to use the abstract factory mode in Python Design Mode Programming
- The example explains how to use the factory method mode of programming in Python design mode.
- Detailed description of the use of the factory method mode in the design mode in the Python Program
- Use the simple factory mode for Python Design Mode Programming
- Example of using the responsibility chain mode and iterator mode in the design mode in Python