Video link: Introduction to game development( Two) Design patterns in the game <>(3 Lesson
often: about2 hour40 Minute)
This video mainly focuses on the common design patterns in the game to explain and Practice, We need more practice and understanding

Notes and summary( Please learn the video content first):

1. It is necessary to realize the significance of design pattern

Game development is a software engineering, His characteristic is that the demand is very changeable. For any software engineering, We need to improve the readability of the code in terms of its organization( It's easy for others to understand), Safety( Not oftenbug), Ease of use( Logic complexity, Too biased thinking, Various troubles in use), Robustness( Be able to deal with unexpected situations well), Maintainability( Whether it is easy to repair and modify), Expansibility( Is it easy to add features), Encapsulation and reusability( Don't copy and paste a piece of code everywhere)

2. Characteristics of game items

For game projects, Except for the above questions, We should pay more attention to its expansibility, Because the demand for games can change every day, So when writing game logic, we must consider as many situations as possible, Considering the convenience of future modification, etc. therefore, We need to minimize code coupling, Different functions should not affect each other as much as possible. However, The decoupling reduction here is not just about the coupling between the modules you write, Also consider coupling with modules written by others, Try not to rely too much on other people's modules, In this way, a little modification of other people's code will not cause large-scale modification of our code.

3. Don't over design

Because demand changes too often, It's likely that the perfect solution you've designed for your current requirements will fail the next day when the requirements change. in addition, It takes time and cost to design a requirement, It may take a week to think about a near perfect solution, But the demand changed the next week, This week is almost a complete waste of time. This is almost impossible to grasp, But problems can be avoided as much as possible through experience accumulation.

4. Game performance is a very important indicator

Games are different from other software, Many game logic is not message driven, Need to call every frame. therefore, We are heading forTick() When adding a method to a function, We have to think about whether we can reduce the cost of function execution by driving it in another way.

5. Factory mode: Simple factory, Ordinary factory, Abstract factory

The so-called factory, The purpose is to produce products. therefore, We package at least one factory class and one product class. Generally speaking, A factory doesn't produce only one product. So what? Let's abstract the product, Many similar products can be derived based on this product. This is a kind of
Simple factory
了, The factory can produce a variety of products of different types and series. for instance, A manufacturing plant produces civil aircraft, Fighter, UAV. So these three planes are the same type( They are all planes.), But different series( Different application scenarios).

Of course. Sometimes for the sake of division of labor, I don't want a factory to produce all kinds of products, So I abstract the factory, Different types of factories produce the same type and different series of products. It's also a factory model, We call him
Ordinary factory. Give an example: I can't make fighter planes and civil aircraft in the same factory, So what? We need a new fighter manufacturing plant to produce fighters, And the civil aircraft will be manufactured by a new civil manufacturer.

So what is abstract factory?

The simple factory above, no matter how abstract, A specific factory produces only one specific product. Abstract factory each concrete factory can produce many kinds of products. Give an example: The manufacturer found that there are many more materials in the production of airplanes, It's a waste, So let's study the production site of small cars. such, A civil aircraft manufacturer can make cars again, And then it was renamed civil manufacturing plant. Production of bulletproof vehicle in fighter factory, Renamed military manufacturing plant.

Compare simple factories, Ordinary factory and abstract factory:
There is only one kind of simple factory, Responsible for producing all products of the same type, So it can be handled in one factory, Factories don't need abstraction, But there may be many kinds of products, So products need to be abstract.
Ordinary factories have many kinds of 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 corresponding to a factory, So it is necessary to abstract products and factories.
Abstract factories have many kinds of factories, Every factory can produce many kinds of products. Considering that there may be multiple products of the same type, So it's also necessary to abstract.

Factory mode in game design:
GameGamePlay Objects that need to be generated and created frequently in logical game world can be managed through factory mode. Like brush monster, Then it can be further abstracted into ordinary factories according to the types of monsters.

6.Builder Pattern:
Subdivide and split the construction details of an object, Achieve the effect of freely adjusting the construction order and content.

In game designBuilder Pattern: Not used much, I think we can create different types of assembled weapons through this, Can split parts, etc. in short, Can reuse the same resource to achieve diversity of performance.

7. Archetypal model:
Copy an instance of an object, In short, cloning. Generally used to create a new object according to the object itself, Hide object creation details.

Prototype pattern in game design: In general, objects dragged in the editor are created by similar methods, asUnity MediumPrefab. First, players drag an object from the resource file to the editor scene, An object is created. subsequently, Player clicksplay, The new world willclone An identical object.

8. Single case/ singleton:
There is only one global object in the whole program.

Single example in game design: Like a global oneNPC Administration, Event recorder, Console management, etc.

9. ObserverObserver Pattern/ 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 be bound freelyN Observer, He doesn't care who the subscribers are, Anyway, every time I make a change, I'll inform all the observers, The purpose of reducing coupling is achieved.

There are differences between publish and subscribe mode and observer mode, There is a unified dispatching center, Subscriber( Analogy observer) You can subscribe from the dispatching center at will, Publisher( Analogy target object) You can publish messages to the dispatching center at any time, Then the dispatch center processes the release of messages according to whether subscribers subscribe..

Observer mode in game design:
For example, in the task system, All players receiving the same global task will receive the update information of the task. This mode andC++ The implementation of multicast agent in language is very similar.

10.Adaptor Adapter:
Bridging classes that perform the same function but are not interface compatible so that they work together, Mainly dealing with the incompatibility of old interfaces.

Adapter pattern in game design: Generally, it is not compatible with the original interface after the project or engine code is updated, Or use it when the old project code is applied in a new game. For example, old projects use new engine functions, Call the underlying interface of the new engine. You can create a new adapter to inherit the old class, Then save the properties of the corresponding class type of a new engine, When calling the old interface, the old logic is overwritten and the interface of the corresponding class of the new engine is changed, You can also add new interfaces for old classes that are only available with new engines.

11.Bridge Bridging mode:

If an object has two dimensional changing factors, Then we can implement a dimension change through inheritance abstraction, By aggregating elements of another dimension change in an abstract class( It's also abstracted by inheritance, And you can continue to aggregate abstractions), This is the bridge mode.

Bridge mode in game design: Not used yet.

12.Decorator Decorator:

It is to decorate some existing classes, To expand some functions, The value of ornament is decoration, It does not affect the core function of the decorated class itself. Usually in an inherited system, Subclasses are usually mutually exclusive, And some functions are universal, In order to dynamically add different functions to different subclasses, You can use decorators to achieve this effect.

Bridge mode in game design: Not used yet.

Last article: Introduction to game development( One) Game development overview <>
Next article: Introduction to game development( Three) Graphic rendering <>

Original link( Reprint please indicate):