Design Patterns Notes twelve template mode

Milk production issues

Programming produced milk, as follows:

  1. Production of milk in the selection process -> Add ingredients -> soak -> Soymilk broken into
  2. By adding different ingredients, you can create different flavors of milk
  3. Selection, and soak into Soymilk break these steps to create each flavor of soy milk are the same
  4. Please use the template method pattern is completed (Note: because the template method pattern is relatively simple, it is easy to think of this program, so the direct use, no longer use the traditional approach to elicit template method pattern)

Template Method pattern basic introduction

basic introduction

  1. Method pattern template (Template Method Pattern), also known template pattern (Template Pattern), z is disclosed in an abstract class defines a method execute its template. Its subclass can override methods as needed to achieve, but the invocation will be defined in the abstract class.
  2. Briefly, the template method skeleton model algorithm defined in one operation, while deferring some steps to subclasses, subclasses can not change such a configuration algorithm, to certain steps of the algorithm can redefine
  3. This type of design pattern belongs behavioral patterns.

Template Method pattern class diagram principle

16.3.1 Template Method pattern class diagram of the principle
Here Insert Picture Description
 class diagram illustrative of the principles - that (roles and responsibilities of the template method pattern)

  1. AbstractClass abstract class, the class implements the template method (Template), the algorithm defines the skeleton, concrete subclasses need to implement other methods abstract operationr2,3,4
  2. ConcreteClass implement the abstract methods operationr2,3,4, to complete the steps of the algorithm in the characteristics subclass

Template method pattern to solve the problem of milk production

  1. Examples of applications requires
    programming of the production of milk, as follows:
    Production of milk flow selection -> Add ingredients -> soaking -> Soymilk broken into by adding different ingredients, can produce different flavors of milk
    selection, immersion and discharge Soymilk broken into several steps for every taste milk production are the same (red beans, peanuts, milk ...)

  2. Ideas and analysis scheme (FIG class)
    Here Insert Picture Description

Hook method template method pattern

  1. In the parent class template method pattern, we can define a method that does nothing by default, as the case may be subclasses do not cover it, which is called "hook."
  2. Or do the milk with the above examples to explain, for example, we also want to make pure milk, do not add any ingredients, use a hook method to transform the front of the template method
  3. Code demonstrates:
//抽象类,表示豆浆
public abstract class SoyaMilk {

	//模板方法, make , 模板方法可以做成final , 不让子类去覆盖.
	final void make() {
		
		select(); 
		if(customerWantCondiments()) {
			addCondiments();
		}
		soak();
		beat();
		
	}
	
	//选材料
	void select() {
		System.out.println("第一步:选择好的新鲜黄豆  ");
	}
	
	//添加不同的配料, 抽象方法, 子类具体实现
	abstract void addCondiments();
	
	//浸泡
	void soak() {
		System.out.println("第三步, 黄豆和配料开始浸泡, 需要3小时 ");
	}
	 
	void beat() {
		System.out.println("第四步:黄豆和配料放到豆浆机去打碎  ");
	}
	
	//钩子方法,决定是否需要添加配料
	boolean customerWantCondiments() {
		return true;
	}
}

16.6 Template Method pattern analysis in source Spring Framework application

  1. When applied to initialize the Spring IOC container Template Method Pattern
  2. Analysis character code analysis + + class described in FIG.
    Here Insert Picture Description
  3. The class diagram for the source code (instructions hierarchy)
    Here Insert Picture Description

Notes and details of the template method pattern

  1. The basic idea is: algorithm exists only in one place, that is in the parent class, easy to modify. When the need to modify the algorithm, as long as the parent class's method to modify the template or already implemented certain steps, the subclass will inherit those changes
  2. Maximizes code reuse. Template Method parent class and subclass some of the steps will be realized directly using inheritance.
  3. Both unified algorithm, also provide a great deal of flexibility. Template parent class method to ensure that the algorithm structure remains unchanged, while providing some of the steps achieved by subclasses.
  4. Inadequacies of the model: each of the different implementations require a subclass to achieve, resulting in increasing the number of classes, making the system more substantial
  5. General template methods are combined with the final keyword, to prevent subclasses override the template method.
  6. Template Method pattern usage scenarios: when to complete in a process, the process to perform a series of steps, this series of steps is basically the same, but the individual steps may be different when implemented, are generally considered to deal with the Template Method Pattern
Published 93 original articles · won praise 31 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43866567/article/details/104841518