Design Pattern-Factory Pattern (2)

definition:

The factory pattern is still a creational design pattern. As a factory, it is concerned with the production of products, that is, the creation of objects. We use factories to create objects without having to create objects ourselves. We do not need to understand how to create objects. You only need to make a request to the factory, and let the factory produce the products you want and the corresponding objects for you according to your requirements. This model is called the factory model.

Advantages of factory mode:

  1. Loosely coupled, objects created are independent of class implementation
  2. The user does not need to know the class of the created object, only the interface, parameters, and methods passed. You can then create the required objects.
  3. Easy to expand. The factory adds other types of data types, only need to modify the parameters.

achieve:

"1" Simple Factory Mode:

from abc import ABCMeta, abstractmethod
 # ABCMeta is a metaclass of Python, used to create abstract base classes in Python programs, abstract methods declared in abstract base classes, decorated with abstractmethod decorator. 
class Coke (metaclass = ABCMeta):
    @abstractmethod
    def drink(self):
        pass

class Coca(Coke):
    def drink(self):
        print('drink Coca-Cola')

class Pepsi(Coke):
    def drink(self):
        print('drink Pepsi-Cola')

class Fast_food_restaurant():
    def make_coke(self ,name):
        return eval(name)()

KCD=Fast_food_restaurant()
coke=KCD.make_coke('Coca')
coke.drink()
#drink Coca-Cola

eval (class name) returns an object of class type
We created an abstract class of Coke. Pepsi and Coca-Cola inherited this abstract class. We also established the fast food restaurant class, which is called the factory class, to make it produce Coke. When the user needs coke, just tell the fast food restaurant what brand of coke to make, tell the fast food restaurant cola name, and then the fast food restaurant uses make_coke method to make cola, and returns the object you need-a cup of Coca-Cola, and then Happy drinking cola. .
View Code

"2" Factory Method Mode

from abc import ABCMeta,abstractmethod

class Coke(metaclass=ABCMeta):
    @abstractmethod
    def drink(self):
        pass

class Coca(Coke):
    def drink(self):
        print('drink Coca-Cola')

class Pepsi(Coke):
    def drink(self):
        print('drink Pepsi-Cola')

class Sfencs(Coke):
    def drink(self):
        print('drink Sfencs-Cola')

class Fast_food_restaurant(metaclass=ABCMeta):
    @abstractmethod
    def make_coke(self):
        pass

class Coca_produce(Fast_food_restaurant):
    def make_coke(self):
        return Coca()

class Pepsi_produce(Fast_food_restaurant):
    def make_coke(self):
        return Pepsi()

class Sfencs_produce(Fast_food_restaurant):
    def make_coke(self):
        return Sfencs()

KCD=Sfencs_produce()
coke=KCD.make_coke()
coke.drink()#drink Sfencs-Cola


The factory method pattern turns the original factory class into an abstract class. Different types of cola are produced through different subclasses. That is, the factory method pattern defines an interface for creating objects, but the specific class of objects created is determined by the subclass. The logical judgment in this way is equivalent to handing over to the client, that is, KCD = Sfencs_produce () to choose which subclass to use, so that if a new Coke product appears, only need to write another subclass inheriting factory abstract class.
View Code

"3" abstract factory pattern

The main purpose of the abstract factory pattern is to provide an interface to create a series of related objects without specifying a specific class. The difference between this pattern and the factory method pattern is that a method subclass can create a series of objects.

from abc import ABCMeta,abstractmethod

class Ice_coke(metaclass=ABCMeta):
    @abstractmethod
    def drink(self):
        pass

class Ordinary_coke(metaclass=ABCMeta):
    @abstractmethod
    def drink(self):
        pass

class Coca_ice(Ice_coke):
    def drink(self):
        print('drink  Coca-ice-Cola')

class Pepsi_ice(Ice_coke):
    def drink(self):
        print('drink Pepsi-ice-Cola')

class Coca_ordinary(Ordinary_coke):
    def drink(self):
        print('drink Coca-ordinary-Cola')

class Pepsi_ordinary(Ordinary_coke):
    def drink(self):
        print('drink Pepsi-ordinary-Cola')

class Fast_food_restaurant(metaclass=ABCMeta):
    @abstractmethod
    def make_ice_coke(self):
        pass

    @abstractmethod
    def make_ordinary_coke(self):
        pass

class Coca_produce(Fast_food_restaurant):
    def make_ice_coke(self):
        return Coca_ice()
    def make_ordinary_coke(self):
        return Coca_ordinary()

class Pepsi_produce(Fast_food_restaurant):
    def make_ice_coke(self):
        return Pepsi_ice()
    def make_ordinary_coke(self):
        return Pepsi_ordinary()

KCD = Coca_produce ()
coke=KCD.make_ice_coke()
coke.drink()#drink  Coca-ice-Cola
View Code

 

Guess you like

Origin www.cnblogs.com/topass123/p/12705985.html