简单工厂模式

'''
FACTORY——工程模式?
  追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说“来四个鸡翅”就行了。麦当劳和肯德基就是生产鸡翅的Factory  
  工厂模式:客户类和工厂类分开。消费者任何时候需要某种产品,只需向工厂请求即可。消费者无须修改就可以接纳新产品。缺点是当产品修改时,工厂类也要做相应的修改。如:如何创建及如何向客户端提供。   


内容:不直接向客户端暴露对象创建的实现细节,而是通过一个工厂类来负责创建产品类的实例。


角色:


工厂角色(Creator)
抽象产品角色(Product)
具体产品角色(Concrete Product)
优点:


隐藏了对象创建的实现细节
客户端不需要修改代码
缺点:


违反了单一职责原则,将创建逻辑几种到一个工厂类里
当添加新产品时,需要修改工厂类代码,违反了开闭原则




'''
#========================1============================
# from abc import abstractmethod,ABCMeta
#
class Payment(metaclass=ABCMeta):
    @abstractmethod
    def pay(self,money):
        pass
#
#
# 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()
#         elif method == "yuebao":
#             return Alipay(True)
#         elif method == "applepay":
#             return ApplePay()
#         else:
#             raise NameError(method)
#
# f = PaymentFactory()
# p=f.create_payment("yuebao")
# p.pay(100)
#=======================2================================
# from  abc import ABCMeta,abstractmethod
#
# class Product(metaclass=ABCMeta):
#     @abstractmethod
#     def eat(self):
#         pass
# class Food(Product):
#
#     def eat(self):
#         print(" I am is eating food")
#
# class Fruit(Product):
#
#     def eat(self):
#         print(" I am is eating fruit")
#
# class ChickenWing(Product):
#
#     def eat(self):
#         print(" I am is eating chickenWing")
#
#
# class Factory():
#     def create_food(self,name):
#         if name == "fruit":
#             return Fruit()
#         elif name == "chickenWing":
#             return ChickenWing()
#         else:
#             return Food()
#
#
# f = Factory()
# p=f.create_food("chick")
# p.eat()
# # #===================3=======================


from abc import ABCMeta,abstractmethod


class StuTool(metaclass=ABCMeta):


    @abstractmethod
    def usering(self):
        pass


class StuPen(StuTool):
    def usering(self):
        print("I am is using Pen to student")
class MyTool(StuTool):
    def __init__(self,name):
        self.name=name
    def usering(self):
        print("i love student%s"% self.name)
class StuPaper(StuTool):
    def usering(self):
        print(" I am is using Paper to student")


class CreateTool(object):
    def selec_tool(self,tool):
        if tool.lower() == "pen":
            return StuPen()
        elif tool.lower == "paper":
            return StuPaper()
        else:
            return MyTool(tool)


f=CreateTool()
p=f.selec_tool("pn")
p.usering()





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值