The Temlplate Child is assigned to the subclass design pattern
The framework that defines the process in the parent class is implemented in the child class. Each subclass may do everything differently, but it does the same thing.
Second, the demand for
Sometimes services need to automatically identify all subclasses that meet certain rules and distribute service data to subclasses for processing according to specified identifiers.
Third, the environment
Language: the javascript
Fourth, the realization of the scene
- The design pattern parent class defines abstract methods that are implemented by subclasses.
- Code logic
-
Define the instanceId for each instance
-
The parent TaskHandler class defines subclass tasks that need to do dealTasks.
-
The parent TaskHandler class assigns the behavior of the task to the subclass dealTask
-
Define the parent static method registInstance
function TaskHandler (instanceId) {
this.instanceId = instanceId
this.registInstance = function () {
// Subclass this is an instance of a subclass
TaskHandler.registInstance.call(this)}// The instance is registered every time an instantiation is invoked
this.registInstance.call(this)
// The purpose of an empty function is to let subclasses decide
this.dealTask = function () {}}This method subclass can be called indirectly by calling the parent class's this.registTask
TaskHandler.registInstance = function () {
console.log("registTask".this)
if(! TaskHandler.cacheInstance) { TaskHandler.cacheInstance = [] } TaskHandler.cacheInstance.push(this);
}
This method can only be launched by the parent class
TaskHandler.triggerTasks = function (tasklist) {
// TaskList can be an array of tasks [task] or a task
if (tasklist instanceof Array) {
// It's time to do a lot of work
tasklist.forEach(task= > {
TaskHandler.cacheInstance.forEach(instance= > {
instance.dealTask.call(instance, task)
})
})
} else if (typeof tasklist === 'sting' && [...tasklist].length > 0) {
// Do one thing
TaskHandler.cacheInstance.forEach(instance= > {
instance.dealTask.call(instance, tasklist)
})
}
}
Copy the code
- Call registInstance in a subclass and call the static method registInstance of the parent class to automatically register the instance to the parent class for convenient management of the parent class
function EasyTask (instanceId) {
this.instanceId = instanceId
this.difficultType = 1 / / simple
TaskHandler.call(this.this.instanceId)
this.dealTask = function (taskName) {
console.log(this.instanceId + 'I'm doing the easy task. Let's get to work.'.'Mission name is' + taskName)
// ...}}function DifficultTask (instanceId) {
this.instanceId = instanceId
this.difficultType = 2 / / / difficult
TaskHandler.call(this.this.instanceId)
this.dealTask = function (taskName) {
console.log(this.instanceId + 'I'm the recipe for a difficult task. Let's get to work.'.'Mission name is' + taskName)
// ...}}Copy the code
- After each subclass registers an instance, the parent class registInstance assigns it to the subclass to handle its own business
const easy = new EasyTask("Han.")
const diffocult = new DifficultTask("Big Han.")
TaskHandler.triggerTasks(['on the tree'.'the tree'])
// Print the result
registTask EasyTask {
instanceId: "Han.".difficultType: 1.registInstance: [Function]
}
registTask DifficultTask {
instanceId: Big Han..difficultType: 2.registInstance: [Function]} small han han I am a simple task, to begin to work Task name is on the tree Big han han is a difficult task, I will begin to work Task name is on the tree The practice of small han han I am a simple task, to begin to work Task name is under the tree Big han han is a difficult task, I will begin to work Task name is under the tree//
Copy the code
Five, the summary
- The idea that an instance of a subclass will work no matter which one is used in the parent class is a general inheritance principle that is not limited to Template Method
- We register whichever subclass we want to make work for, and then have the parent class distribute them. By redefining methods in subclasses, you can change the behavior of your program
- With Template Method, the Method call logic is defined in the parent class, making subclasses easier, but less flexible. If the parent class defines too few methods, the subclasses will become bloated and even duplicate code.