https://github.com/WittyKyrie/UnityUtil/blob/main/%E5%B8%B8%E7%94%A8%E5%B7%A5%E5%85%B7/Object
Code warehouse ↑
[Game development design pattern] Singleton pattern, design pattern to beware of! ! _哔哩哔哩_bilibili
Reference ↑
Combination mode:
Inheritance will cause a lot of code reuse and difficult subclass expansion
Different classes are realized by combining different components with each other
Try to avoid using GetComponent in Update, otherwise the performance cost is too expensive
Singleton mode:
Ensure that a class has only one instance and has a global access point
Having only one instance is a very important feature for systems that need to record the state of each modification to a file
Use static to achieve global access function
The advantages of the singleton mode: the singleton mode will only be created when the first request is made, it will not be created independently, memory saving (not necessarily, there are lazy and hungry modes
There is only one object for action, without going through object creation and destruction, saving performance
Allows you to link game modules very easily, for example, any of your classes can easily call the file system
Disadvantages of the singleton pattern:
As the project grows, the degree of coupling increases and maintenance is difficult
Singletons can make scaling difficult
command mode:
Encapsulates a request as an object, allowing you to parameterize clients with different requests, queue or log requests, and support undoable operations
Observer pattern:
Object pool mode:
Predefine a pool containing reusable objects, create the object and set it to inactive mode when the pool is initialized,
When the object needs to be created, set the object to the active mode, and when the object needs to be destroyed, set it to the inactive mode
Compared with the traditional creation and destruction mode, there will be a huge performance improvement in cpu and memory consumption
The object pool mode will directly allocate a complete large memory, instead of generating a lot of memory fragments like continuous creation and destruction, which will cause performance waste
Factory pattern:
The factory pattern is a creational design pattern for creating objects and encapsulating the object creation process in a class
The factory pattern can improve code maintainability, extensibility and readability by separating client code from the process of actually creating objects