Design patterns
Design pattern: In software design, a concise and elegant solution to a specific problem.
Five Design Principles
SOLID five design principles
- Single responsibility principle: a program does only one thing well.
- O Open closed principle: open to extension, closed to modification.
- Richter’s substitution principle: a subclass can override its parent and appear in the same place as its parent.
- I Interface independence Principle: Keep interfaces single and independent.
- D Dependency inversion principle: the usage method only focuses on the interface and does not focus on the implementation of the concrete class.
Why are design patterns needed
- Legibility: The use of design patterns can improve the readability of code and improve the efficiency of subsequent development.
- 02 Extensibility: Use design patterns to decouple the code, which can greatly enhance the code’s modifability and extensibility.
- Reusability: Use design patterns to reuse existing solutions without having to do the same work.
- Reliability: Using design patterns can increase the robustness of the system and make code authoring truly engineering.
Design Patterns common in game development
Design patterns are often used not only in JS development, but also in game development.
The singleton pattern
Definition: unique & global access. Ensure that a class has only one instance and provide a global access point to access it.
Application scenario: Contents that can be cached, such as login pop-ups.
In game development, singletons are often used.
It is generally used for the existence of objects objects, resources and other management classes with a singleton pattern.
Publish and subscribe model
Definition: A many-to-many dependency between objects in which all dependent objects are notified when an object’s state changes.
Application scenario: DOM events, message notification.
The publish-subscribe model (the observer model) is generally referred to as the God perspective, and the core idea is one-to-many.
For example, in a game guild, some group events need to be distributed to each member’s mailbox. Since the message is the same and distributed to different hands, this can be called observer mode.
When each object changes, the dependent objects are notified and updated automatically.
Portfolio model
Definition: Use small child objects to build larger objects, and combine objects into a tree structure to represent a partial-whole hierarchy. Application scenario: Is-is to HAS-A.
The composite pattern makes the use of single objects and composite objects consistent. In game development, the Unity engine itself relies on the component model, we create an object, the object itself will have a Transform component, if we want to add functionality to the object, we will add a component to it, many components together make up an object.