Study notes (two): design patterns in games

1. It is necessary to deeply realize the meaning of design patterns.
Game development is a software project, and its characteristic is that the requirements are extremely changeable. For any software project, we need to organize the code to improve its readability (it is easy for others to understand), safety (not often bugs), and ease of use (the logic is complex, the thinking is too biased, and its use is different. Kind of trouble), robustness (to be able to handle unexpected situations well), maintainability (whether it is easy to repair and modify), scalability (whether it is easy to add functions), encapsulation and reusability (don’t copy and paste a piece of code everywhere)

2. The characteristics
of the game project For the game project, in addition to the above problems, we should pay more attention to its scalability, because the needs of the game may change every day, so we must consider as much as possible when writing the game logic In this case, considering the convenience of future modifications, etc. Therefore, we need to reduce the coupling of the code as much as possible, so that different functions do not affect each other as much as possible. However, the reduction of coupling here not only refers to the coupling between modules written by oneself, but also considering coupling with modules written by others. Try to avoid over-dependence on other people's modules, so that a little modification of other people's code will not cause us Large-scale modification of the code.

3. Don't over-design.
Because the requirements change too frequently, it is possible that a plan you designed for the current requirements is perfect or not will completely fail when the requirements change the next day. In addition, the design of a requirement requires time cost. It may take a week to think about a nearly perfect solution, but the requirement changes in the second week. This week’s time is almost completely wasted. This is almost impossible to fully grasp, but the problem can be avoided as much as possible through the accumulation of experience.

4. Game performance is a very important indicator.
Unlike other software, many game logics are not message-driven and need to be called every frame. Therefore, when we add methods to the Tick() function, we must think about whether we can drive in another way to reduce the cost of function execution.

5. Factory model: simple factory, ordinary factory, abstract factory The
so-called factory, the purpose is to produce products. Therefore, we encapsulate at least one factory class and one product class. Generally speaking, a factory does not only produce one product. So, we abstract the product, and we can derive a variety of similar products based on this product. This is a simple factory that can produce a variety of products of different series. For example, a manufacturing plant produces civilian aircraft, fighter jets, and drones. Then these three aircraft are of the same type (all aircraft), but the series are different (different application scenarios).

Of course, sometimes in order to make the division of labor clear, I don't want one factory to produce all types of products, so I abstract the factory, and different types of factories produce the same type and different series of products. This is also a factory model, let's call it an ordinary factory . For example: I can't make fighter jets and civilian airplanes be produced in the same factory, so I will get a new fighter jet manufacturing factory to produce fighter jets, and civilian airplanes will get a new civilian manufacturing factory to make them.

So what is an abstract factory?
No matter how abstract the above simple factory is, a specific factory only produces a specific product. Each concrete factory in the abstract factory can produce multiple products. Example: The manufacturer finds that there are a lot of extra materials in the production of airplanes, which is too wasteful to throw away, so research the production of small cars. In this way, a civil aircraft manufacturing plant can produce cars again, and then renamed a civil manufacturing plant. The fighter jet factory produced bulletproof vehicles and was renamed the military factory.

Compare the simple factory, the ordinary factory and the abstract factory: the
simple factory has only one kind of factory, responsible for producing all products of the same type, so it can simply be processed in one factory, and the factory does not need to be abstract, but because there may be multiple products, so The product must be abstract.
An ordinary factory has multiple factories, but each factory only produces one product of the same type and series. Considering that there may be multiple products of the same type and a series of products corresponds to a factory, it is necessary to abstract products and factories.
The abstract factory has many kinds of factories, and each kind of factory may produce many types of products. Considering that there may be multiple products of the same type, it is also necessary to abstract.

Factory mode in game design: The objects that need to be frequently generated and created in the game world of GamePlay logic can be managed by factory mode. Such as spawning monsters can then be further abstracted into ordinary factories according to the types of monsters.

6.Builder mode:
subdivide and split the construction details of an object to achieve the effect of freely adjusting the construction order and construction content.

Builder mode in game design: I don’t use it much. I personally think that different types of assembled weapons can be created through this, and parts can be split. In short, the same resource can be reused to achieve diversity of performance.

7. Prototype mode:
Copying an instance of an object is simply a clone. It is generally used to create a new object based on the object itself, hiding the details of object creation.

Prototype mode in game design: Generally, the objects dragged into the editor are created in a similar way, such as the Prefab in Unity. First, the player drags an object from the resource file to the editor scene. At this time, an object is created. Subsequently, the player clicks on play, and the new world will clone an identical object.

8. Singleton/Singleton:
There is only one global object in the entire program running.

Singleton in game design: such as a global NPC management, event recorder, console management, etc.

9. Observer mode/publish and subscribe mode:
When an object's state changes, all objects that depend on it are notified and automatically updated. The target object can bind N observers freely. He doesn't care who the subscribers are. Anyway, he informs all observers every time he changes, which achieves the purpose of reducing coupling.
The publish-subscribe model is different from the observer model. There is a unified dispatch center for publishing. Subscribers (analog observers) can subscribe to content from the dispatch center at will, and publishers (analog target objects) can publish messages to the dispatch center at any time, and then dispatch The center processes the publication of messages according to whether the subscribers subscribe.

Observer mode in game design: For
example, in the task system, all players who receive the same global task will receive updated information about the task. This mode is very similar to the realization of the multicast agent in C++ language.

10. Adaptor:
Bridge the classes that perform the same function but the interfaces are not compatible so that they can work together, mainly to deal with the incompatibility of the old interface.

Adapter mode in game design: generally used when the project or engine code is incompatible with the original interface after the project or engine code is updated, or when the old project code is applied in a new game. For example, the old project uses the functions of the new engine to call the underlying interface of the new engine. You can create a new adapter to inherit the old class, and then save the properties of a new engine corresponding to the class type. When calling the old interface, it will overwrite the original old logic and change it to the interface of the new engine corresponding class. You can also add a new engine to the old class. New interface only available.

11.Bridge bridge mode:
If an object has two dimensional change factors, then one dimensional change can be realized through inheritance abstraction, and another dimensional change factor can be aggregated in the abstract class (this is also abstracted through inheritance, and also You can continue to aggregate abstractions), this is the bridge mode.

Bridge mode in game design: I haven't used it yet.

12. Decorator: It
is to decorate some existing classes to expand some functions. The value of decorators lies in decoration, and it does not affect the core functions of the decorated class itself. Usually in an inherited system, subclasses are usually mutually exclusive, but some functions are common. In order to dynamically add different functions to different subclasses, you can use decorators to achieve this effect.

Bridge mode in game design: I haven't used it yet.

Original link (please indicate if reprinted): http://blog.csdn.net/u012999985/article/details/79090524

Guess you like

Origin blog.csdn.net/qq_43801020/article/details/108921041