Introduction to game development （ Two ） Design patterns in the game
Video link ： Introduction to game development （ Two ） Design patterns in the game <http://gad.qq.com/content/coursedetail/7175671>（3 Class
often ： about 2 hour 40 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 ）, Security （ Not often bug）, Ease of use （ Complex logic , 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 ）, Expandability （ 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're trying to Tick() 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 , General Factory , Abstract factory
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 , Let's abstract the product , Many similar products can be derived based on this product . This is a kind of
了 , 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 （ It's all airplanes ）, 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 , 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 ：
game GamePlay Objects that need to be generated and created frequently in logical game world can be managed through factory mode . Like a brush , 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 design Builder pattern ： Not much use , 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. Prototype mode ：
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 , as Unity In Prefab. First, players drag an object from the resource file to the editor scene , An object is created . subsequently , Player Click play, The new world will clone 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 one NPC Administration , Event recorder , Console management, etc .
9. Observer Observer 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 freely N Observers , 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 is connected with C++ 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 Bridge 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 we 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 .
Previous ： Introduction to game development （ One ） Game development overview <http://blog.csdn.net/u012999985/article/details/78797928>
Next ： Introduction to game development （ Three ） Graphics rendering <http://blog.csdn.net/u012999985/article/details/79090657>
Original link （ Reprint please indicate ）：http://blog.csdn.net/u012999985/article/details/79090524