模式定义

简单工厂模式(Simple Factory Pattern):又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式。在简单工厂模式中,可以根据参数的
不同返回不同类的实例。简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。
简单工厂模式:给程序带来更大的可扩展性和可维护性。

  

 

模式结构

简单工厂模式包含如下角色:
Factory:工厂角色
Product:抽象产品角色
ConcreteProduct:具体产品角色

  

python实现简单工厂模式

 

实例

现在有一个工人,他的工作是砍树,工具为花岗岩石斧和铁斧,不同的环境:现代社会与原始社会。

现在我们是直接在work下创建斧头。
人与斧头耦合性强,相当于现在,我们需要自己根据不同的需求,去创造不同的斧头。
我们使用不同的斧头需要修改work下的代码。
依赖性太强。

  

 1 class Person(object):
 2     def __init__(self,name):
 3         self.name=name
 4     def work(self):
 5         print("%s开始工作"%self.name)
 6 
 7         #在原始社会,需要一把石斧
 8         axe1=StoneAxe('花岗岩石斧')
 9         #在现代社会,需要一把铁斧头
10         axe2=SteelAxe("铁斧头")
11         axe1.Cut_tree()
12         axe2.Cut_tree()
13 
14 
15 class Axe(object):
16     def __init__(self,name):
17         self.name=name
18     def Cut_tree(self):
19         print("%s斧头砍树"%self.name)
20 
21 class StoneAxe(Axe):
22     def Cut_tree(self):
23         print("使用%s开始砍树"%self.name)
24 class SteelAxe(Axe):
25     def Cut_tree(self):
26         print("使用%s开始砍树"%self.name)
27 
28 p1=Person("lili")
29 p1.work()

 

 

现在我们来解决强耦合的问题。

工人在不同的环境下有不同的需求,以前需要自己根据请求去创造斧头,然后砍树。

现在我们工厂来了,工人只需要告诉工厂(工厂类),我需要什么类型的斧头(参数),工厂然后根据你的需求来创建斧头。

其实就相当于多了第三者,我们只需要提供不同的参数,工厂就可以根据不同的参数,做出判断,创造产品。

Product:抽象产品角色

1 class Axe(object):
2     def __init__(self,name):
3         self.name=name
4     def Cut_tree(self):
5         print("%s斧头砍树"%self.name)

 

ConcreteProduct:具体产品角色

1 class StoneAxe(Axe):
2     def Cut_tree(self):
3         print("使用%s开始砍树"%self.name)
4 class SteelAxe(Axe):
5     def Cut_tree(self):
6         print("使用%s开始砍树"%self.name)

 

 

 Factory:工厂角色

 1 class Factory(object):
 2     @staticmethod
 3     def create_axe(type):
 4         if type=="Stone":
 5             axe=StoneAxe("花岗岩石斧")
 6             return axe
 7         elif type=="Steel":
 8             axe=SteelAxe("铁斧头")
 9             return axe
10         else:print("输入类型错误,没有此类型的斧头")

 

class Person(object):
    def __init__(self,name):
        self.name=name
    def work(self,axe_type):
        print("%s开始工作"%self.name)

        #在原始社会,需要一把石斧
        axe=Factory.create_axe(axe_type)
        axe.Cut_tree()
        #在现代社会,需要一把铁斧头


class Axe(object):
    def __init__(self,name):
        self.name=name
    def Cut_tree(self):
        print("%s斧头砍树"%self.name)

class StoneAxe(Axe):
    def Cut_tree(self):
        print("使用%s开始砍树"%self.name)
class SteelAxe(Axe):
    def Cut_tree(self):
        print("使用%s开始砍树"%self.name)

#工厂类
class Factory(object):
    @staticmethod
    def create_axe(type):
        if type=="Stone":
            axe=StoneAxe("花岗岩石斧")
            return axe
        elif type=="Steel":
            axe=SteelAxe("铁斧头")
            return axe
        else:print("输入类型错误,没有此类型的斧头")

p1=Person("lili")
p1.work("Steel")
完整代码

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2021-12-13
  • 2021-07-23
  • 2022-12-23
  • 2021-07-13
  • 2021-07-28
猜你喜欢
  • 2021-10-02
  • 2021-06-18
  • 2022-12-23
  • 2021-08-25
  • 2021-11-26
  • 2022-01-13
  • 2022-12-23
相关资源
相似解决方案