1. What is a template pattern
A template pattern is a method defined in an abstract class that executes its methods (template methods). Subclasses of the abstract class override the methods of the parent class according to the specific business needs, but execute them using the methods defined in the abstract class.
In the template pattern, an abstract exposure defines how its methods are executed (templates). Its subclasses can override method implementations, but the calls will be made with methods defined in the abstract class. This type of design pattern is behavioral
2. Advantages/disadvantages of template mode and use of Yangtze River
2.1 Advantages of template mode
- The duplicate code that removes the subclass method is easy to maintain by separating the invariant parts from the subclass into the subclass
- Extending mutable parts, subclasses implement some of the details of the template’s parent class to help extend the template’s parent class
- An operation implemented by a parent class is called by a subclass, and a new behavior is added by subclass extension (the behavior is controlled by the parent class, and the subclass implementation complies with the open closed principle)
2.2 Disadvantages of the template pattern
- By design, abstract classes are responsible for declaring the most abstract and general transaction properties and methods, and implementation classes are responsible for completing the concrete transaction properties and methods. However, the template approach is the opposite. The results of the subclass execution affect the results of the parent class, which makes the code more difficult to read
- Each different implementation requires a subclass to implement, resulting in an increasing number of classes, making the system larger
2.3 Application Scenarios of template Mode
- Multiple subclasses have methods in common and basically the same logic
- Core algorithms and important functions can be designed as template methods, subclasses to implement the relevant details of the function
- When the system is refactoring or functional optimization can be removed from the subclass duplicate code into the parent class
3. Template pattern implementation
3.1 First create an abstract Phone class
package com.zhangsan.shard.pojo;
/ * * *@ClassName Phone
* @Description TODO
* @Author ZhangSan_Plus
* @Date 2021/9/9 19:43
* @Version1.0 * * /
public abstract class Phone {
abstract void openPhone(a);
abstract void endPhone(a);
abstract void upVolume(a);
abstract void downVolume(a);
public final void usePhone(a) {
/ / boot
openPhone();
// Turn up the volume
upVolume();
// Turn down the volume
downVolume();
}
public final void end(a) {
// Turn off the phoneendPhone(); }}Copy the code
3.2 Create a Xiaomi phone subclass
package com.zhangsan.shard.pojo;
/ * * *@ClassName XiaoMi
* @Description TODO
* @Author ZhangSan_Plus
* @Date 2021/9/9 19:47
* @Version1.0 * * /
public class XiaoMi extends Phone {
@Override
void openPhone(a) {
System.out.println("Xiaomi phone is turned on.");
}
@Override
void endPhone(a) {
System.out.println("Xiaomi phone has been turned off.");
}
@Override
void upVolume(a) {
System.out.println("Xiaomi phone volume +1");
}
@Override
void downVolume(a) {
System.out.println("Xiaomi Phone volume -1"); }}Copy the code
3.3 Create an iPhone subclass
package com.zhangsan.shard.pojo;
/ * * *@ClassName Apple
* @Description TODO
* @Author ZhangSan_Plus
* @Date2021/9/9 timnathserah *@Version1.0 * * /
public class Apple extends Phone {
@Override
void openPhone(a) {
System.out.println("IPhone turned on.");
}
@Override
void endPhone(a) {
System.out.println("IPhone is turned off.");
}
@Override
void upVolume(a) {
System.out.println("IPhone volume +1");
}
@Override
void downVolume(a) {
System.out.println("IPhone volume -1"); }}Copy the code
3.4 Create a user to use the phone
package com.zhangsan.shard.pojo;
/ * * *@ClassName TemplateDemo
* @Description TODO
* @Author ZhangSan_Plus
* @Date 2021/9/9 19:51
* @Version1.0 * * /
public class TemplateDemo {
public static void main(String[] args) {
Phone xiaoMi = new XiaoMi();
xiaoMi.usePhone();
xiaoMi.endPhone();
Phone apple = newApple(); apple.usePhone(); apple.endPhone(); }}Copy the code
4. Reference of template methods in Java
4.1 class AbstractList
public abstract E get(int var1);
Copy the code
4.1.1 ArrayList implementation of get() method
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
Copy the code
4.1.2 SubList implementation of get() method
public E get(int index) {
rangeCheck(index);
checkForComodification();
return l.get(index+offset);
}
Copy the code
4.2 AbstractMap class
public abstract Set<Entry<K,V>> entrySet();
Copy the code
4.2.1 ConcurrentHashMap implementation of entrySet()
public Set<Map.Entry<K,V>> entrySet() {
EntrySetView<K,V> es;
return(es = entrySet) ! =null ? es : (entrySet = new EntrySetView<K,V>(this));
}
Copy the code
4.2.2 TreeMap implementation of entrySet()
public Set<Map.Entry<K,V>> entrySet() {
EntrySet es = entrySet;
return(es ! =null)? es : (entrySet =new EntrySet());
}
Copy the code