javascript and design patterns(1)

Assume that a director needs to shoot a movie, actors and photographers are needed, the director calls action, and the staff members take their positions
// 摄影师
class Photographer {
  record() {
    console.log('start record')
  }
}
// 演员
class Actors {
  performance() {
    console.log('start performance')
  }
}

// 导演
class Director {
  action() {
    console.log('各部门准备,开始!')
    const photographer = new Photographer()
    const actors = new Actors()
    photographer.record()
    actors.performance()
  }
}

const director = new Director()
director.action()
Now I need to add another gaffer
class LightingEngineer {
  openLight() {
    console.log('open light')
  }
}

class Director {
  action() {
    console.log('各部门准备,开始!')
    const photographer = new Photographer()
    const actors = new Actors()
    const lightingEngineer = new LightingEngineer()
    photographer.record()
    actors.performance()
    lightingEngineer.openLight()
  }
}
const director = new Director()
director.action()

#####I need to add props, sound, assistant director, etc., and then the director is exhausted
#####Because every time the director calls out to start, he must assign different tasks to the staff, which is very easy to make mistakes, the director Tiredness is also inevitable.


#####In the actual scene, the director shouted "Workers and everyone, action!" Everyone started their own work. #####Trained staff will know what they need to do when the director calls action , so how should we modify the code?

#####Add your own action method to each person
class Photographer { record() { console.log('start record') } action() { this.record() } } class Actors { performance() { console .log('start performance') } action() { this.performance() } } class LightingEngineer { openLight() { console.log('open light') } action() { this.openLight() } }






















// 将工作人员实例当作参数传入action 方法
class Director {
  action(members) {
    members.map(member => {
      member.action()
    })
  }
  cut(){
    console.log('cut')
  }
}

const director = new Director()
const photographer = new Photographer()
const actors = new Actors()
const lightingEngineer = new LightingEngineer()
director.action([photographer, actors, lightingEngineer])
director.cut()
director.action([photographer, actors])
Well, the director can concentrate on filming, and the staff also know their responsibilities
If you understand the above content, you have mastered the principles of polymorphic encapsulation and strategy patterns, so you don’t need to know what polymorphic encapsulation principles and strategy patterns are, you can already use them, and all the principles and design patterns are It was created to solve specific problems.
At this time you will ask, why do you need to learn design patterns? I just need to know how to use them. In fact, design patterns can be understood as professional terms. For example, when playing King of Glory, you will say 4-1 split push, everyone understand
The 4-1 split push is an in-game strategy that is often used to increase the efficiency of a team's tower pushing. This strategy is based on numerical superiority, where four players focus on one lane to push towers, while the other moves elsewhere to pressure other enemy towers or kill opponents. In this way, the opponent's tower can be knocked down faster, creating a chance for our side to win.

Design patterns are best practices in software development that provide solutions to problems in specific situations. Understanding the purpose and method of design patterns is the key to truly understanding and effectively applying design patterns.

Resistance can come from misunderstanding or unfamiliarity with the concept of design patterns. Some people may think that design patterns complicate the code or add extra work. In reality, however, design patterns are meant to improve code reusability, maintainability, and extensibility, thereby making code more flexible and adaptable to change.
Rote copying is also a problem to be avoided. While design patterns provide possible solutions, it doesn't mean that every problem requires the use of design patterns. The purpose of properly understanding design patterns is knowing when and how to use them, not simply applying them to every scenario.
To truly understand design patterns, it is recommended that you not only learn their definitions and basic ideas, but also delve into their principles and practices. This includes understanding the issues, benefits and applicability behind design patterns and trying to apply them in real projects. Through practice and experience, you'll be able to better grasp design patterns and apply them to appropriate situations.

Guess you like

Origin blog.csdn.net/weixin_42657666/article/details/131824609