Factory:
represents the class responsible for creating other types of objects
Advantages:
1, loosely coupled, to create an object can be independent of the implementation class
2, clients do not need to know to create objects of the class, but you can use it to create objects. Only need to know the transmission interfaces, methods and parameters
3, add additional class factory to create other types of objects, without having to change the client code
4, the plant can reuse existing object
Category
1, a simple factory pattern
allows the interface to create objects, but not exposed to create a logical object
2, the factory method pattern
allows the interface to create objects, but which class to create an object, referred to the subclasses decide
3, abstract factory pattern
Abstract Factory is able to create a series of related objects, without specifying the interface / disclosing specific classes,
the model can provide an object other plants, to create objects in its interior
Simple factory pattern
# -*- coding: utf-8 -*-
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def say_hi(self):
pass
class Dog(Animal):
def say_hi(self):
print("汪汪汪...")
class Cat(Animal):
def say_hi(self):
print("喵喵喵...")
class Factory(object):
def make_sound(self, animal):
eval(animal)().say_hi()
if __name__ == '__main__':
factory = Factory()
factory.make_sound("Dog")
factory.make_sound("Cat")
"""
汪汪汪...
喵喵喵...
"""
Factory Method pattern
1, define an interface to create the objects, the plant itself is not responsible for creating an object, completed by a subclass
2, creating the plant, rather than a method inherited by instantiating complete
3, having a factory method makes the design more customizable, returns the same instance or subclass, rather than objects of a certain type (similar to the simple factory method)
# -*- coding: utf-8 -*-
from abc import ABC, abstractmethod
# 组件部分
class Section(ABC):
@abstractmethod
def describe(self):
pass
class PersonalSection(Section):
def describe(self):
print("PersonalSection")
class AlbumSection(Section):
def describe(self):
print("AlbumSection")
class PatentSection(Section):
def describe(self):
print("PatentSection")
class PublicationSection(Section):
def describe(self):
print("PublicationSection")
# 工厂部分
class Profile(ABC):
def __init__(self):
self.sections = []
self.create_profile()
@abstractmethod
def create_profile(self):
pass
def show_sections(self):
for section in self.sections:
section.describe()
def add_section(self, section):
self.sections.append(section)
class LinkedIn(Profile):
def create_profile(self):
self.add_section(PersonalSection())
self.add_section(PatentSection())
self.add_section(PublicationSection())
class FaceBook(Profile):
def create_profile(self):
self.add_section(PersonalSection())
self.add_section(AlbumSection())
if __name__ == '__main__':
linkedin = LinkedIn()
linkedin.show_sections()
"""
PersonalSection
PatentSection
PublicationSection
"""
facebook = FaceBook()
facebook.show_sections()
"""
PersonalSection
AlbumSection
"""
Abstract Factory Method
It provides an interface for creating families of related objects, without specifying concrete classes
# -*- coding: utf-8 -*-
from abc import ABC, abstractmethod
class FruitPizza(ABC):
@abstractmethod
def prepare(self):
pass
class MeatPizza(ABC):
@abstractmethod
def serve(self):
pass
class ApplePizza(FruitPizza):
def prepare(self):
print("ApplePizza")
class PeachPizza(FruitPizza):
def prepare(self):
print("PeachPizza")
class PorkPizza(MeatPizza):
def serve(self):
print("PorkPizza")
class BeefPizza(MeatPizza):
def serve(self):
print("BeefPizza")
# 抽象工厂
class PizzaFactory(ABC):
@abstractmethod
def create_meat_pizza(self):
pass
@abstractmethod
def create_fruit_pizza(self):
pass
class IndianPizzaFactory(PizzaFactory):
def create_meat_pizza(self):
return PorkPizza()
def create_fruit_pizza(self):
return ApplePizza()
class USPizzaFactory(PizzaFactory):
def create_meat_pizza(self):
return BeefPizza()
def create_fruit_pizza(self):
return PeachPizza()
class PizzaStore(object):
def make_pizza(self):
for factory in [IndianPizzaFactory(), USPizzaFactory()]:
meat_pizza = factory.create_meat_pizza()
fruit_pizza = factory.create_fruit_pizza()
meat_pizza.serve()
fruit_pizza.prepare()
if __name__ == '__main__':
pizza_store = PizzaStore()
pizza_store.make_pizza()
"""
PorkPizza
ApplePizza
BeefPizza
PeachPizza
"""
the difference
Factory Method | Abstract Factory Method |
---|---|
The client open a way to create objects | It contains one or more factory method to create a series of related objects |
Use inheritance and subclasses decide which object is created | Combination will create objects of the task entrusted to other classes |
Used to create a product | Used to create a series of related products |
to sum up:
1, the plant is simple: can be created at run time based on the client type corresponding to the parameters passed Example
2, the factory method: a defined interface to create an object, but the object is created by subclasses complete
3, abstract factory method: providing an interface without specifying a particular class, you can create a series of related objects
Reference
"Python Design Patterns 2nd Edition," Chapter III factory mode