Template Method Design Pattern Basics!

Template Method Design Pattern
The problem solved by this design pattern is: applications with fixed algorithms (steps). However, these algorithm steps have different implementations for different users (situations). In this design pattern, there are two categories of methods: template methods and step methods. The step method is divided into three types : abstract method, final method, and hook method. Abstract method: A method that subclasses must implement. Final method: A method modified by final, a method that cannot be overridden by subclasses. Hook method: A method that has a default implementation but can be overridden by subclasses. // abstract methods can only appear in abstract classes // but abstract classes do not necessarily have abstract methods public abstract class DinnerService { // template method public void havingDinner() { // order order(); // eat (); // payment pay(); } // --------- The following methods are called step methods ------------- // final method: not overridable by subclasses Method public final void order() { System.out.println("Order with iPad"); } // Abstract method: a method that must be implemented by subclasses



                        
                        
                        

























public abstract void eat();


// Hook method: a method that can be overridden by subclasses

public void pay() {
System.out.println("Pay with cash");
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324788500&siteId=291194637