abstract class Template {
abstract excute1(): void; // must be implemented in derived classes
abstract excute2(): void; // must be implemented in a derived class
abstract excute3(): void; // must be implemented in a derived class
excute() {//This is the template method, which defines the order of execution. All children are in this order.
this.excute1();
this.excute2();
this.excute3();
}
}
class aaa extends Template {
excute1 () {
YBLog.log("aaa", "Done 1 ");
};
excute2() {
YBLog.log("aaa", "Done 2 ");
};
excute3() {
YBLog.log("aaa", "Done 3 ");
};
}
class bbb extends Template {
excute1 () {
YBLog.log("bbb", " 做了 1 ");
};
excute2() {
YBLog.log("bbb", " 做了 2 ");
};
excute3() {
YBLog.log("bbb", " 做了 3 ");
};
}
new aaa().excute();
new bbb().excute();
1. Advantages: The unchanging part is placed in the parent class. The variable part is placed in the subclass. Extract common codes for easy maintenance. The specific functional logic is placed in a sub-category for easy expansion
2. Disadvantages: Subclasses that implement abstract methods may also affect the parent class.