您的位置:首页 > 编程语言 > Python开发

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

2012-12-11 15:29 796 查看
基本原理请参考相关书籍。

基本说明:

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

市场部有分为:

铣刀市场部:MillMD

钻头市场部:DirllMD



生产部又分为:

铣刀生成部:MillPD

钻头生产部:DrillPD



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









#源代码
# -*- 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 division
from __future__ import print_function
from __future__ import unicode_literals
from 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")
        pass

class MillPD(ProductDepartment):
    """This class defines an existing interface that needs adapting.
    """
    def make(self):
        print("make mills")
        pass

class MarketDepartment(object):
    """This class defines the domain-specific interface that Client uses.
    """
    def Request():
        pass

class 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()

        pass

class 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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: