On the game development of common design patterns

Foreword  

Because game development on architecture to consider the performance and actual demand, developed under the premise of trying not to introduce third-party libraries, frameworks, so time will be frequently used in coding mode design the code reuse, architecture decoupling of business logic minimize hard code.

Singleton (Singleton Pattern)

Strict singleton pattern is often used in a single logical object (typically a heavyweight resources) on such Factory, Context, Resource, Pool Service, and the like, but the details of the code to be noted that open out interface and interface semantics. Singleton pattern usually with lifecycle functions, help combine its own life-cycle management framework to initialize or destroy operations. In the development process will encounter some bad examples, such as static member variables are variables, methods are static method, write code may result in a call to meet the "singleton" is semantics, but let others people confused.

Factory pattern (Factory Pattern)

Using the factory pattern to create objects in favor of a unified lifecycle management object, usually a combination of single-case model, agent model, strategy pattern, to assemble complex objects, objects created for unified management. The benefits of unified entrance of the object may not be obvious at early stages of development, but with the development progress of the push, the more complex business, unified entrance easily to better track the life cycle of an object, it tends to be unified if some sort of object initialization operation.

Strategy Mode (Strategy Pattern)

For some logic is similar but the details are different particle sizes achieved and relatively small traffic can be guaranteed extracted moderate semantic granularity interfaces out, according to different implementations logic encapsulated into different strategies, by policy container (usually a plant container) in upper combined business calls. This is to ensure a clear and easy extension of the logical structure. Examples of game development, the state machine is using a strategy mode, the state machine itself is a container strategy, it related to the status of the slicing process behavior from a large jump in, make the code more clear structure. Code structure more clear, the less chance of a jump means BUG appear lower and easier to debug. Personally feel that the focus should be on policy mode interface granularity, it differs with the agency model, which requires moderate size, depending on the business strategy allows to dynamically select the code from the package to the customer code. Use the strategy pattern to decouple code is a good choice.

Agent mode (Proxy Pattern)

Proxy mode with the strategy pattern is very similar to the coarse granularity than the strategy pattern to control access to objects. For example, a complex data objects have a common binary sequence interfaces, because the business expand, more and more data sequences, and the network layer protocol limits the maximum length. At this time is the need to change the network infrastructure? The common sequence of interfaces to get rid of? Or in the code that calls the hard code a cropped serialization code? The answer is obvious: only one face of the object access protocol, while this access is only based on business needs to achieve serialization interfaces cropped. Use proxy mode, better packaging protected object, so that business with a stable object changes frequently isolated.

The observer pattern (Observer Pattern) / subscribe release mode (Subscribe / Publish)

The two main modes to decouple coupling between independent objects, here put together in terms, because these two modes the main difference in the structure, the observer pattern is directly coupled publication subscription model is loosely coupled. On the game development often see XXXListener, which is to use these two modes (most with subscription release mode). In game development, in addition to the incident life cycle framework, in fact, you can use these patterns to loose coupling specific business. For example, there are a lot of lines on the strip to develop a complex system to develop, to develop a line there is collusion with each other, in addition to changes to develop a line of operating systems interact with the role of interactions to develop other lines should only stay in this system to develop itself, this time you need observer mode / publish subscribe model will develop interactive isolate these lines open.

Template mode (Template Pattern)

The programming mode is biased to the frame, in the actual development needs to be considered common - domain specific gravity, particle size and the degree of abstraction of the actual needs of the business considerations. Level of abstraction is too high can result in reduced efficiency and development more difficult to achieve, the level of abstraction is too low will lead to operational expansion and maintenance too bad that this is a question worth pondering.

Combined mode (Composite Pattern)

The most important object-oriented multiplexing mode and polymorphism combinations, unstable objects isolation mode, are connected in a tree structure. The advantage of using a combination of modes is frequently part of the changes will not affect the stability of the whole, but the drawback is you can not use this polymorphism syntactic sugar for direct reuse. Rigorously using a combination of modes in the development and inheritance makes the code more clear structure and calmly changed the face of business. Become part of the focus is, like Yin and Yang, as in the actual development of how to use the combination / succession is a problem worthy of careful consideration.

After words

There are many design patterns I have not mentioned, because my actual development scenarios rarely encountered. I think, the design model is dead, but the business is alive. In accordance with the principles of software design in Uncle Bob introduction of "Clean Architecture", combined with the actual use of them, continue to refine the code, I believe you will find coding fun. My humble opinion, would like to start a discussion on this.

Reproduced in: https: //www.cnblogs.com/jason-koo/p/11042283.html

Guess you like

Origin blog.csdn.net/weixin_33985507/article/details/93185984
Recommended