[置顶] 设计模式十(适配器模式,python语言实现)

时间:2021-10-08 21:58:58

基本原理请参考相关书籍。

基本说明:

公司总体上分为市场部MarketDepartment和生产部ProductDepartment

市场部有分为:

铣刀市场部:MillMD

钻头市场部:DirllMD

 

生产部又分为:

铣刀生成部:MillPD

钻头生产部:DrillPD

 

客户通过市场部下订单,市场部接到订单通过生产部门完成订单。

 

[置顶]        设计模式十(适配器模式,python语言实现)

 

 

#源代码
# -*- coding: utf-8 -*-######################################################## # adaptor.py# Python implementation of the Class Client# Generated by Enterprise Architect# Created on:      11-十二月-2012 15:00:59# #######################################################from __future__ import divisionfrom __future__ import print_functionfrom __future__ import unicode_literalsfrom future_builtins import *class ProductDepartment(object):    """This class defines an existing interface that needs adapting.    """    def make(self):        pass    class DrillPD(ProductDepartment):    """This class defines an existing interface that needs adapting.    """    def make(self):        print("make drills")        passclass MillPD(ProductDepartment):    """This class defines an existing interface that needs adapting.    """    def make(self):        print("make mills")        passclass MarketDepartment(object):    """This class defines the domain-specific interface that Client uses.    """    def Request():        passclass MillMD(MarketDepartment):    """This class adapts the interface of Adaptee to the Target interface.    """    m_MillPD= MillPD()    def Request(self):        m_MillPD=MillPD()        m_MillPD.make()        passclass DrillMD(MarketDepartment):    """This class adapts the interface of Adaptee to the Target interface.    """    m_DrillPD= DrillPD()    def Request(self):        # adaptee->SpecificRequest()        m_DrillPD=DrillPD()        m_DrillPD.make()        pass        #客户端        if(__name__=="__main__"):            class Client(object):        """This class collaborates with objects conforming to the Target interface.        """        m_MarketDepartment= MarketDepartment()                def __init__(self):            self.m_MarketDepartment=None            pass        def SetOrder(self,department):            self.m_MarketDepartment=department            pass                def GetOrder(self):            self.m_MarketDepartment.Request()            pass              pass        client= Client()    client.SetOrder(MillMD())    client.GetOrder()         client.SetOrder(DrillMD())    client.GetOrder()