抽象工厂能给一套零件做出限制。【必须成套!!比如AppleCpu+IOS+shell/高通+安卓+shell】
"""
@File : abs_fac.py
@Time : 2021-10-21 16:17
@Author : XD
@Email : gudianpai@qq.com
@Software: PyCharm
"""
from abc import abstractmethod, ABCMeta
class PhoneShell(metaclass = ABCMeta):
@abstractmethod
def show_shell(self):
pass
class CPU(metaclass = ABCMeta):
@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_os(self):
pass
@abstractmethod
def make_cpu(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):
def show_cpu(self):
print("骁龙CPU")
class MediaTekCPU(CPU):
def show_cpu(self):
print("联发科CPU")
class AppleCPU(CPU):
def show_cpu(self):
print("苹果CPU")
class Android(OS):
def show_os(self):
print("Android系统")
class IOS(OS):
def show_os(self):
print("IOS系统")
class MiFactory(PhoneFactory):
def make_os(self):
return Android()
def make_cpu(self):
return SnapDragonCPU()
def make_shell(self):
return BigShell()
class HuaWeiFactory(PhoneFactory):
def make_os(self):
return Android()
def make_cpu(self):
return MediaTekCPU()
def make_shell(self):
return BigShell()
class AppleFactory(PhoneFactory):
def make_shell(self):
return AppleShell()
def make_os(self):
return IOS()
def make_cpu(self):
return AppleCPU()
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(HuaWeiFactory())
p1.show_info()
print("-----")
p2 = make_phone(AppleFactory())
p2.show_info()
手机信息
联发科CPU
Android系统
普通手机大手机壳
-----
手机信息
苹果CPU
IOS系统
苹果手机壳
|