Application of bridge mode in Python design pattern programming with instance parsing

Source: Internet
Author: User
Let's first look at an example:

#encoding =utf-8 # #by Panda #桥接模式 def printinfo (info): Print Unicode (info, ' utf-8 '). Encode (' GBK ') #抽象类: Mobile phone brand class Hand Setbrand (): soft = None def sethandsetsoft (self, soft): Self.soft = Soft def Run (self): Pass #具体抽象类: Mobile Brand 1 class HandsetBrand1 (Handsetbrand): def Run (self): Printinfo (' Mobile phone brand 1: ') Self.soft.Run () #具体抽象类: Mobile phone brand 2 class Ha    NdsetBrand2 (Handsetbrand): def Run (self): Printinfo (' Mobile phone brand 2: ') Self.soft.Run () #功能类: Mobile software Class Handsetsoft (): def run (self): Pass #具体功能类: Game class Handsetgame (Handsetsoft): def run (self): Printinfo (' Run mobile games ') #具体功 Can class: Address Book Class handsetaddresslist (Handsetsoft): def run (self): Printinfo (' Running mobile phone record ') def clientui (): H1 = Handsetb Rand1 () H1. Sethandsetsoft (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 summed up like this:

Therefore, the concept of bridging mode is to separate the part of the system abstraction from its implementation, so that they can vary independently.
Because the target system has multiple angles of classification, each classification will have a variety of changes, then you can separate the multi-angle, let them change independently, reduce the coupling between them.

Let's look at one more example:

Basic Principles please refer to the relevant books, here directly to the example

Holiday tourism from the point of view can be divided into Shanghai and Dalian, from the perspective of the way can be divided into groups and monolithic

Bridging mode connects the two classifications to make the choice.

Class Diagram:

#-*-Coding:utf-8-*-######################################################## # tour.py# Python implementation of the C Lass dalian# Generated by Enterprise architect# Created on:11-December-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 C  Lasses. "" "Def __init__ (self, form= ' stay at Home '): Self.form=form pass def getform (self): return Self.form pass P  Assclass 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 (Travelfo  RM): "" 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 abstraction's in  Terface, 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->oper    Ation (); 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 abstraction. "" "Def __init__ (self, info=" Go to DaLian "): Super (Dalian,self). __init__ (Info) passclass Shanghai (Destination):"  "This class extends the interface defined by abstraction. "" "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 ()

Run results

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.