设计模式实例代码

一、接口类

接口类的实现主要为了接口化,归一化,统一化,就是每个类的方法都是固定的名称
from
abc import abstractmethod, ABCMeta class Payment(metaclass=ABCMeta):
  """父类实现的方法加入子类继承了必须实现,否则报错""" @abstractmethod
def pay(self, money): pass class Alipay(Payment): def pay(self, money): print("支付宝支付%s元"%money) class ApplePay(Payment): def pay(self, money): print("苹果支付%s元"%money) class WechatPay(Payment): def pay(self, money): print("微信支付%s元"%money) payment = Alipay() payment.pay(100)

二、工厂类

from abc import abstractmethod, ABCMeta

class Payment(metaclass=ABCMeta):
    @abstractmethod
    def pay(self, money):
        raise NotImplementedError

class Alipay(Payment):
    def __init__(self, enable_yuebao=False):   #一个支付软件可以有多种的支付方式
        self.enable_yuebao = enable_yuebao

    def pay(self, money):
        if self.enable_yuebao:
            print("余额宝支付%s元" % money)
        else:
            print("支付宝支付%s元" % money)

class ApplePay(Payment):
    def pay(self, money):
        print("苹果支付%s元" % money)

class PaymentFactory:  # 工厂类 封装了对象创建的细节,封装了所有的支付方式,只是提供给用户一个接口
    def create_payment(self, method):
        if method == "alipay":
            return Alipay()      #调用Alipay()      #模式使用支付宝
        elif method == "applepay":
            return ApplePay()
        elif method == "yuebao":
            return Alipay(True)     #调用Alipay()      采用余额宝功能
        else:
            raise NameError(method)

factory = PaymentFactory()            #实例化工厂函数
payment = factory.create_payment("yuebao")      #调用工厂函数的方法
payment.pay(100)                                #根据用户的选择进行支付

三、工厂类的另一种实现方式

from abc import abstractmethod, ABCMeta

class Payment(metaclass=ABCMeta):
    @abstractmethod
    def pay(self, money):
        raise NotImplementedError

class Alipay(Payment):
    def pay(self, money):
        print("支付宝支付%s元" % money)

class ApplePay(Payment):
    def pay(self, money):
        print("苹果支付%s元"%money)

class PaymentFactory(metaclass=ABCMeta):
    @abstractmethod
    def create_payment(self):
        pass

class AlipayFactory(PaymentFactory):
    def create_payment(self):
        return Alipay()

class ApplePayFactory(PaymentFactory):
    def create_payment(self):
        return ApplePay()

# 用户输入
# 支付宝,120

af = AlipayFactory()
ali = af.create_payment()
ali.pay(120)

四、抽象工厂

from abc import abstractmethod, ABCMeta


# ------抽象产品------
class PhoneShell(metaclass=ABCMeta):      #生产手机壳的类
    @abstractmethod
    def show_shell(self):
        pass

class CPU(metaclass=ABCMeta):      #生产CPU的类
    @abstractmethod
    def show_cpu(self):
        pass

class OS(metaclass=ABCMeta):            #制作系统的类
    @abstractmethod
    def show_os(self):
        pass

# ------抽象工厂------
class PhoneFactory(metaclass=ABCMeta):
    @abstractmethod
    def make_shell(self):
        pass

    @abstractmethod
    def make_cpu(self):
        pass

    @abstractmethod
    def make_os(self):
        pass

# ------具体产品------ class SmallShell(PhoneShell): #生产小的手机壳 def show_shell(self): print("普通手机小手机壳") class BigShell(PhoneShell): #生产大的手机壳 def show_shell(self): print("普通手机大手机壳") class AppleShell(PhoneShell): #生产苹果手机壳 def show_shell(self): print("苹果手机壳") class SnapDragonCPU(CPU): #生产骁龙cpu def show_cpu(self): print("骁龙CPU") class MediaTekCPU(CPU): #生产联发科cpu def show_cpu(self): print("联发科CPU") class AppleCPU(CPU): #生产苹果cpu def show_cpu(self): print("苹果CPU") class Android(OS): #生产Android系统 def show_os(self): print("Android系统") class IOS(OS): def show_os(self): #生产ios系统 print("iOS系统") # ------具体工厂------ class MiFactory(PhoneFactory): #这个工厂生产骁龙cpu def make_cpu(self): return SnapDragonCPU() def make_os(self): #这个工厂制造安卓系统 return Android() def make_shell(self): #这个手机制造大的手机壳 return BigShell() class HuaweiFactory(PhoneFactory): #华为工厂 def make_cpu(self): return MediaTekCPU() def make_os(self): return Android() def make_shell(self): return SmallShell() class IPhoneFactory(PhoneFactory): #iPhone工厂 def make_cpu(self): return AppleCPU() def make_os(self): return IOS() def make_shell(self): return AppleShell() # ------客户端------ class Phone: #生产好的手机信息 def __init__(self, cpu, os, shell): self.cpu = cpu self.os = os self.shell = shell def show_info(self): print("手机信息:") self.cpu.show_cpu() self.os.show_os() self.shell.show_shell() def make_phone(factory): #生产手机 cpu = factory.make_cpu() os = factory.make_os() shell = factory.make_shell() return Phone(cpu, os, shell) p1 = make_phone(IPhoneFactory()) #生产手机(iPhone制造工厂) p1.show_info() #展示制造好的手机

五、建造者模式

import random
from abc import abstractmethod, ABCMeta

#------产品------
class Player:
  """生产一个玩具,这个玩具有头,胳膊,身体,腿"""
def __init__(self, face=None, body=None, arm=None, leg=None): self.face = face self.arm = arm self.leg = leg self.body = body def __str__(self): #打印生产出来的人看效果 return "%s, %s, %s, %s" % (self.face, self.arm, self.body, self.leg) #------建造者------ class PlayerBuilder(metaclass=ABCMeta):
  """负责生产人身体上的每个部件"""
@abstractmethod
def build_face(self): pass @abstractmethod def build_arm(self): pass @abstractmethod def build_leg(self): pass @abstractmethod def build_body(self): pass @abstractmethod def get_player(self): pass class BeautifulWomanBuilder(PlayerBuilder):
  """生产一个漂亮的小女孩"""
def __init__(self): self.player = Player() #首先实例化工厂类,之后实现里面的每一个功能 def build_face(self): self.player.face = "漂亮脸蛋" def build_arm(self): self.player.arm="细胳膊" def build_body(self): self.player.body="细腰" def build_leg(self): self.player.leg="长腿" def get_player(self): return self.player class RandomPlayerBuilder(PlayerBuilder):
  """生产一个即漂亮又难看的人"""
def __init__(self): self.player = Player() def build_face(self): self.player.face = random.choice(["瓜子脸","西瓜子脸"]) def build_arm(self): self.player.arm=random.choice(["长胳膊","短胳膊"]) def build_body(self): self.player.body=random.choice(["苗条",""]) def build_leg(self): self.player.leg=random.choice(["长腿","短腿"]) def get_player(self): return self.player class PlayerDirector: def __init__(self, builder): self.builder = builder # 控制组装顺序 def build_player(self): self.builder.build_body() self.builder.build_face() self.builder.build_arm() self.builder.build_leg() return self.builder.get_player() pd = PlayerDirector(RandomPlayerBuilder())#随机生产一个人并组装好零件 p = pd.build_player() print(p) #可以看到你已经造好了一个玩具人了

六、单例模式

from abc import abstractmethod, ABCMeta

class Singleton(object):
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            cls._instance = super(Singleton, cls).__new__(cls)
        return cls._instance

class MyClass(Singleton):
    def __init__(self, name=None):
        if name is not None:
            self.name = name

a = MyClass("a")
print(a)
print(a.name)

b = MyClass("b")
print(b)
print(b.name)

print(a)
print(a.name)

七、适配器模式

#适配器主要采用加壳的方式实现
from
abc import abstractmethod, ABCMeta class Payment(metaclass=ABCMeta): @abstractmethod def pay(self, money): raise NotImplementedError class Alipay(Payment): def pay(self, money): print("支付宝支付%s元"%money) class ApplePay(Payment): def pay(self, money): print("苹果支付%s元"%money) #------待适配类------ class WechatPay: def huaqian(self, a, b): print("微信支付%s元"%(a+b)) #------类适配器------ class RealWeChatPay(Payment, WechatPay): def pay(self, money): return self.huaqian(money, 0) #------对象适配器------ class PayAdapter(Payment): def __init__(self, payment): self.payment = payment def pay(self, money): return self.payment.huaqian(money, 0) #RealWeChatPay().pay(100) PayAdapter(WechatPay()).pay(1000)

八、混合模式

from abc import abstractmethod, ABCMeta

class Graphic(metaclass=ABCMeta):
    @abstractmethod
    def draw(self):
        pass

    @abstractmethod
    def add(self, graphic):
        pass

    @abstractmethod
    def getchildren(self):
        pass

class Point(Graphic):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def draw(self):
        print(self)

    def add(self, graphic):
        raise TypeError

    def getchildren(self):
        raise TypeError

    def __str__(self):
        return "点(%s, %s)" % (self.x, self.y)

class Line(Graphic):
    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2

    def draw(self):
        print(self)

    def add(self, graphic):
        raise TypeError

    def getchildren(self):
        raise TypeError

    def __str__(self):
        return "线段[%s, %s]" % (self.p1, self.p2)

class Picture(Graphic):
    def __init__(self):
        self.children = []

    def add(self, graphic):
        self.children.append(graphic)

    def getchildren(self):
        return self.children

    def draw(self):
        print("------复合图形------")
        for g in self.children:
            g.draw()
        print("------END------")

pic1 = Picture()
pic1.add(Point(2,3))
pic1.add(Line(Point(1,2), Point(4,5)))
pic1.add(Line(Point(0,1), Point(2,1)))

pic2 = Picture()
pic2.add(Point(-2,-1))
pic2.add(Line(Point(0,0), Point(1,1)))

pic = Picture()
pic.add(pic1)
pic.add(pic2)

pic.draw()

九、代理模式

from abc import ABCMeta, abstractmethod

class Subject(metaclass=ABCMeta):
    @abstractmethod
    def get_content(self):
        pass

class RealSubject(Subject):
    def __init__(self, filename):
        self.filename = filename
        print("读取%s文件内容"%filename)
        f = open(filename)
        self.content = f.read()
        f.close()

    def get_content(self):
        return self.content

    def set_content(self, content):
        f = open(self.filename, 'w')
        f.write(content)
        f.close()

class ProxyA(Subject):
    def __init__(self, filename):
        self.subj = RealSubject(filename)

    def get_content(self):
        return self.subj.get_content()

class ProxyB(Subject):
    def __init__(self, filename):
        self.filename = filename
        self.subj = None

    def get_content(self):
        if not self.subj:
            self.subj = RealSubject(self.filename)
        return self.subj.get_content()

class ProxyC(Subject):
    def __init__(self, filename):
        self.subj = RealSubject(filename)

    def get_content(self):
        return self.get_content()

    def set_content(self):
        raise PermissionError

    # 写一个set_content

b = ProxyB("abc.txt")
#print(b.get_content())

十、责任链模式

from abc import ABCMeta, abstractmethod
#
# class Handler(metaclass=ABCMeta):
#     @abstractmethod
#     def handle_leave(self, day):
#         pass
#
#
# class GeneralManagerHandler(Handler):
#     def handle_leave(self, day):
#         if day < 10:
#             print("总经理批准%d天假"%day)
#         else:
#             print("呵呵")
#
#
# class DepartmentManagerHandler(Handler):
#     def __init__(self):
#         self.successor = GeneralManagerHandler()
#     def handle_leave(self, day):
#         if day < 7:
#             print("部门经理批准%d天假"%day)
#         else:
#             print("部门经理无权准假")
#             self.successor.handle_leave(day)
#
#
# class ProjectDirectorHandler(Handler):
#     def __init__(self):
#         self.successor = DepartmentManagerHandler()
#     def handle_leave(self, day):
#         if day < 3:
#             print("项目主管批准%d天假")
#         else:
#             print("项目主管无权准假")
#             self.successor.handle_leave(day)
#
#
# day = 4
# h = ProjectDirectorHandler()
# h.handle_leave(day)
#

#--高级例子--模仿js事件处理

class Handler(metaclass=ABCMeta):
    @abstractmethod
    def add_event(self, func):
        pass

    @abstractmethod
    def handle(self):
        pass


class BodyHandler(Handler):
    def __init__(self):
        self.func = None

    def add_event(self, func):
        self.func = func

    def handle(self):
        if self.func:
            return self.func()
        else:
            print("已到最后一级,无法处理")


class ElementHandler(Handler):
    def __init__(self, successor):
        self.func = None
        self.successor = successor

    def add_event(self, func):
        self.func = func

    def handle(self):
        if self.func:
            return self.func()
        else:
            return self.successor.handle()

# 客户端

# <body><div><a>

body = {'type': 'body', 'name': 'body', 'children': [], 'father': None}

div = {'type': 'div', 'name': 'div', 'children': [], 'father': body}

a = {'type': 'a', 'name': 'a', 'children': [], 'father': div}

body['children'].append(div)
div['children'].append(a)

body['event_handler'] = BodyHandler()
div['event_handler'] = ElementHandler(div['father']['event_handler'])
a['event_handler'] = ElementHandler(a['father']['event_handler'])

def attach_event(element, func):
    element['event_handler'].add_event(func)

#test

def func_div():
    print("这是给div的函数")

def func_a():
    print("这是给a的函数")

def func_body():
    print("这是给body的函数")

#attach_event(div, func_div)
#attach_event(a, func_a)
#attach_event(body, func_body)

a['event_handler'].handle()

十一、迭代器模式

class LinkList:
    """链表 头结点保存链表的长度"""
    class Node:
        def __init__(self, item=None):
            self.item = item
            self.next = None

    class LinkListIterator:
        def __init__(self, node):
            self.node = node
        def __next__(self):
            if self.node:
                cur_node = self.node
                self.node = cur_node.next
                return cur_node.item
            else:
                raise StopIteration
        def __iter__(self):
            return self

    def __init__(self, iterable=None):
        self.head = LinkList.Node(0)
        self.tail = self.head
        self.extend(iterable)

    def append(self, obj):
        s = LinkList.Node(obj)
        self.tail.next = s
        self.tail = s

    def extend(self, iterable):
        for obj in iterable:
            self.append(obj)
        self.head.item += len(iterable)

    def __iter__(self):
        return self.LinkListIterator(self.head.next)

    def __len__(self):
        return self.head.item

    def __str__(self):
        return "<<"+", ".join(map(str, self))+">>"

li = [i for i in range(100)]
print(li)
lk = LinkList(li)
# for i in lk:
#     print(i)

print(lk)
# print(len(lk))

十二、观察者模式(发布者订阅者模式)

from abc import ABCMeta, abstractmethod


class Observer(metaclass=ABCMeta):
    @abstractmethod
    def update(self, notice):
        pass


class Notice:
    def __init__(self):
        self.observers = [] # 记录该主体的观察者(订阅者)

    def attach(self, obs):
        self.observers.append(obs)

    def detach(self, obs):
        obs.company_info = None
        self.observers.remove(obs)

    def notify(self):
        for obj in self.observers:
            obj.update(self)


class ManagerNotice(Notice):
    def __init__(self, company_info=None):
        super().__init__()
        self.__company_info = company_info

    @property
    def company_info(self):
        return self.__company_info

    @company_info.setter
    def company_info(self, info):
        self.__company_info = info
        self.notify()



class Manager(Observer):
    def __init__(self):
        self.company_info = None

    def update(self, noti):
        self.company_info = noti.company_info


notice = ManagerNotice()

alex = Manager()
wusir = Manager()

# print(alex.company_info)
# print(wusir.company_info)

notice.attach(alex)
notice.attach(wusir)
#
notice.company_info="公司运行良好"
#
print(alex.company_info)
print(wusir.company_info)
#
notice.detach(wusir)
#
notice.company_info="公司要破产了"

print(alex.company_info)
print(wusir.company_info)

十三、策略模式(代码参照前边的算法NB三人组)

from abc import ABCMeta, abstractmethod
import random

class Sort(metaclass=ABCMeta):
    @abstractmethod
    def sort(self, data):
        pass


class QuickSort(Sort):
    def quick_sort(self, data, left, right):
        if left < right:
            mid = self.partition(data, left, right)
            self.quick_sort(data, left, mid - 1)
            self.quick_sort(data, mid + 1, right)

    def partition(self, data, left, right):
        tmp = data[left]
        while left < right:
            while left < right and data[right] >= tmp:
                right -= 1
            data[left] = data[right]
            while left < right and data[left] <= tmp:
                left += 1
            data[right] = data[left]
        data[left] = tmp
        return left

    def sort(self, data):
        print("快速排序")
        return self.quick_sort(data, 0, len(data) - 1)


class MergeSort(Sort):
    def merge(self, data, low, mid, high):
        i = low
        j = mid + 1
        ltmp = []
        while i <= mid and j <= high:
            if data[i] <= data[j]:
                ltmp.append(data[i])
                i += 1
            else:
                ltmp.append(data[j])
                j += 1

        while i <= mid:
            ltmp.append(data[i])
            i += 1

        while j <= high:
            ltmp.append(data[j])
            j += 1

        data[low:high + 1] = ltmp


    def merge_sort(self, data, low, high):
        if low < high:
            mid = (low + high) // 2
            self.merge_sort(data, low, mid)
            self.merge_sort(data, mid + 1, high)
            self.merge(data, low, mid, high)

    def sort(self, data):
        print("归并排序")
        return self.merge_sort(data, 0, len(data) - 1)


class Context:
    def __init__(self, data, strategy=None):
        self.data = data
        self.strategy = strategy

    def set_strategy(self, strategy):
        self.strategy = strategy

    def do_strategy(self):
        if self.strategy:
            self.strategy.sort(self.data)
        else:
            raise TypeError


li = list(range(100000))
random.shuffle(li)

context = Context(li, MergeSort())
context.do_strategy()
#print(context.data)

random.shuffle(context.data)

context.set_strategy(QuickSort())
context.do_strategy()

十五、模板模式

from abc import ABCMeta, abstractmethod


class IOHandler(metaclass=ABCMeta):
    @abstractmethod
    def open(self, name):
        pass
    @abstractmethod
    def deal(self, change):
        pass
    @abstractmethod
    def close(self):
        pass
    def process(self, name, change):
        self.open(name)
        self.deal(change)
        self.close()

class FileHandler(IOHandler):

接口隔离原则:

# 有些东西我有你没有,你有我么有,这是就需要用到接口隔离
#
from abc import abstractmethod, ABCMeta # # class AnimalOnLand(metaclass=ABCMeta): # @abstractmethod # def walk(self): # pass # # class AnimalInSky(metaclass=ABCMeta): # @abstractmethod # def fly(self): # pass # # class AnimalInWater(metaclass=ABCMeta): # @abstractmethod # def swim(self): # pass # # class Tiger(AnimalOnLand): # pass # # class Frog(AnimalInWater, AnimalOnLand): # pass class A: def test(self): pass class B(A): pass class C: def __init__(self): self.a = A() self.a.test()

 

posted @ 2018-02-02 20:40  一石数字欠我15w!!!  阅读(316)  评论(2编辑  收藏  举报