Design Patterns (4) - Factory mode

Factory mode mainly includes factory method and abstract factory , its main objective is to instantiate this activity departing from the class out, reduce the coupling system, the specific approach is to create a package object.

Factory Method

Factory method to create a pattern object defines an interface, but is determined by the subclass to instantiate the class which is a. Let the class factory method to instantiate deferred to subclasses.

By way of example to illustrate the direct mode. We simulated a pizza shop, it needs to achieve Pizza PizzaStore class and class, PizzaStore class needs to contain orderPizza method. The need for certain types of pizza menu instantiated in orderPizza process. If the direct instantiated using new, when we want to update the menu, you must make changes to orderPizza code, it is clear that contrary to our "closed for modification" principle. To this end we declare a factory method , was originally responsible for a specific object instance of all classes, and now by PizzaStore do some small change into subclasses by a group responsible for instantiating, so as to achieve the process of object creation package purpose.

public abstract class PizzaStore {
 
	abstract Pizza createPizza(String item);
 
	public Pizza orderPizza(String type) {
		Pizza pizza = createPizza(type);
		System.out.println("--- Making a " + pizza.getName() + " ---");
		pizza.prepare();
		pizza.bake();
		pizza.cut();
		pizza.box();
		return pizza;
	}
}

Project with Maven to build, test method is in the test directory. The picture shows the class factory method

Abstract Factory

Abstract factory pattern provides an interface for creating families of related or dependent objects without the need to explicitly specify a specific class.

Example following a factory method, in order to ensure that if every pizza franchises use high quality raw materials, we need to build a factory for the production of raw materials and raw materials transported to various stores. Create a raw material for the factory-defined interfaces that abstract factory:

public interface PizzaIngredientFactory {
 
	public Dough createDough();
	public Sauce createSauce();
	public Cheese createCheese();
	public Veggies[] createVeggies();
	public Pepperoni createPepperoni();
	public Clams createClam();
 
}

Raw material factories in different regions have achieved change the interface, and can be personalized according to taste local specialties. Thus, the plant material was decoupled from the specific feedstock.

Abstract factory class diagram as follows:

The factory model embodies another principle mode design: Dependency Inversion principle , that is to rely on abstract and do not rely on a specific class . This illustrates the principle: You can not get the top component depends on low-level components, and regardless of high-level or low-level components, both of which should depend on the abstract.

Comparison of factory method and the abstract factory

  • They are used to create objects packaged, by reducing the dependency promote loose coupling between the application and the particular class.
  • Factory method using inheritance: the delegate object to create a subclass of subclasses to implement the factory method to create the object.
  • Abstract factory object composition: a method to create an object is now a factory interface exposed by the.
  • Factory method allows to delay class to instantiate subclasses.
  • Abstract factory to create a family of related objects, without the need to rely on their specific class.
Published 295 original articles · won praise 37 · views 30000 +

Guess you like

Origin blog.csdn.net/tianshan2010/article/details/104706011