Example parsing: Python Design Pattern programming: Application of Bridge pattern, python Design Pattern
Let's take an example:
# Encoding = UTF-8 ## by panda # Bridge Mode def printInfo (info): print unicode (info, 'utf-8 '). encode ('gbk') # Abstract class: mobile phone brand class HandsetBrand (): soft = None def SetHandsetSoft (self, soft): self. soft = soft def Run (self): pass # specific abstract class: mobile phone brand 1 class HandsetBrand1 (HandsetBrand): def Run (self): printInfo ('mobile phone brand 1: ') self. soft. run () # specific abstract class: mobile phone brand 2 class HandsetBrand2 (HandsetBrand): def Run (self): printInfo ('mobile phone brand 2: ') self. soft. run () # function class: mobile software class HandsetSoft (): def Run (self): pass # specific function class: Game class HandsetGame (HandsetSoft): def Run (self ): printInfo ('running a mobile game') # function class: Address Book class HandsetAddressList (HandsetSoft): def Run (self): printInfo ('running a mobile phone address') def clientUI (): h1 = HandsetBrand1 () extract (HandsetAddressList () h1.Run () h1.SetHandsetSoft (HandsetGame () h1.Run () h2 = HandsetBrand2 () h2.SetHandsetSoft (HandsetAddressList () h2.Run () h2.SetHandsetSoft (HandsetGame () h2.Run () return if _ name _ = '_ main _': clientUI ();
The class diagram can be summarized as follows:
Therefore, the concept of the bridge mode is to separate the abstract Part of the system from its implementation part so that they can be changed independently.
Because the target system has multiple classification angles and each classification has multiple changes, you can separate multiple angles so that they can change independently and reduce coupling between them.
Next let's look at an instance:
For basic principles, see related books.
Holiday tourism can be divided into Shanghai and Dalian from the perspective of destination, from the perspective of mode can be divided into a group and a single body
The Bridge Mode connects the two categories for selection.
Class diagram:
#-*-Coding: UTF-8 -*-#################################### ##################### tour. py # Python implementation of the Class DaLian # Generated by Enterprise effecect # Created on: 11-May 26-2012 16:53:52 ################################## ##################### from _ future _ import divisionfrom _ future _ import print_functionfrom _ future _ import unicode_literalsfrom future_builtins import * class TravelForm (object): "This class defines the interface for implementation classes. "def _ init _ (self, form =" stay at home "): self. form = form pass def GetForm (self): return self. form pass passclass Group (TravelForm): "This class implements the Implementor interface and defines its concrete implementation. "def _ init _ (self, form =" by group "): super (Group, self ). _ init _ (form) pass passclass Independent (TravelForm): "This class implements the Implementor interface and defines its concrete implementation. "def _ init _ (self, form =" by myself "): super (Independent, self ). _ init _ (form) passclass Destination (object): "This class (a) defines the specified action's interface, and (B) maintains a reference to an object of type Implementor. "m_TravelForm = TravelForm () def _ init _ (self, info): self.info = info pass def GetInfo (self): # imp-> Operation (); return print (self.info + "" + self. form. getForm () pass def SetForm (self, form): self. form = form passclass DaLian (Destination): "This class extends the interface defined by role action. "def _ init _ (self, info =" Go to DaLian "): super (DaLian, self ). _ init _ (info) passclass ShangHai (Destination): "This class extends the interface defined by specified action. "def _ init _ (self, info =" Go to ShangHai "): super (ShangHai, self ). _ init _ (info) pass # client if (_ name __= = "_ main _"): destination = ShangHai () destination. setForm (Group () destination. getInfo () destination = DaLian () destination. setForm (Independent () destination. getInfo ()
Running result
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
- Examples of Adapter mode in Python Design Mode Programming
- 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