Design Patterns in the game (1)--observer mode

Source: Internet
Author: User

As the first article in this series, I would like to express my personal understanding of design patterns. So I ask myself to answer a few questions. 1, what is design mode? 2, is the design pattern important today in the fast software iteration? 3, Gang of four proposed 23 design patterns, is not the design problem unparalleled? 4, what is the relationship between design patterns and language? 5, design patterns and object-oriented relationships?
1, what is design mode? Take Baidu Google a bit, you can find a variety of answers, just a lot of answer is really will let people find North, pattern this word is really tall, so often haunt in academia. In view of the design pattern, the author's understanding of the design pattern is that the design pattern can be considered as a general scheme to solve a certain kind of problem, why is it universal, in fact, Gof did not create any new design methods, instead they are the predecessors of the software design method has been fully categorized summary, Get the most famous 23 design patterns, so it can be said that these design patterns have a big name before there are a lot of people in use.
2, is the design pattern important today in the fast software iteration? Important or not, the user's attitude towards it, the design problem is not a sufficient condition, and in a fast iterative development cycle, the results may be more important than the process, after all, the player, planning not care your architecture is graceful, the structure is flexible and extensible. But from the perspective of a software designer (this title is good, please do not call me programmer), you may need a certain sense of beauty (the Siege lion is also a pursuit drop), of course, the most important thing is that you need the architecture has enough extensibility (very ethereal word like cohesion poly low coupling, imaginative achievement here inexpressible). Extensible and Lotus is a pair of natural enemies, if you do not mind lotus, design patterns may be meaningless, otherwise you really need to find gang of four.
3, Gang of four proposed 23 design patterns, is not the design problem unparalleled? The answer to this question is definitely negative, the gof of the choice of the college entrance examination, after all, the history is relatively long, Gof 23 models are classic, but with the development of software, there are many new design patterns. For example, in 2007, Google published an article shocked our siege, put forward the map-reduce model, some people think this is an algorithm, some people think it is a design mode, WHATEVER,MR indeed really promote the development of the industry. So C/S,B/S,MVC,MVP is not design mode, it should be, I personally think (can also be considered as an architecture).
4, what is the relationship between design patterns and language and object-oriented? There's no such thing as a half-dime relationship, so all books about design patterns are explained in the Java language, because the Java language Object-oriented feature easily describes the problem that design patterns are designed to solve, and the Java syntax is similar to C + +, making it easy for most people to understand, The object-oriented Java language is very easy to describe the problem of design patterns to solve, in fact, there is also implicit in the meaning that object-oriented language easier to use design patterns to solve the problem. The design pattern itself is about the abstraction of the problem, and the object-oriented is to abstract the problem, imagine if the use of C language to explain design patterns, single is the definition of function pointers will scare away countless readers.
The following is an analysis of a design pattern to be discussed in this article: Observer patterns. Observer mode, also known as Release-subscriber mode, is one of the 23 design patterns proposed by GOF, which is a kind of behavioral pattern, which is used to solve the communication problems between different modules.      There are 2 characters in The Observer Pattern: (1) the subject (Subject): It can be understood as a message (and must be a cloth). (2) Observer (Observer): the recipient (consumer) of the message. In a standard observer pattern, the interdependence between the subject and the observer is abstract, so the relationship between them can be expressed using the following UML class diagram


It can be seen that when a subject can have multiple observers at the same time, when the subject changes is to notify all observers, the pattern solves such a problem: When module B relies on module A's data, module B needs to know that module A's data is changed, and that the use of abstraction (DIP) mechanism makes the coupling between the modules greatly reduced. When the Observer mode is not used, module B needs to detect the change of the data of module a periodically, which not only increases the complexity of the code, but also complicates the coupling of the modules.
Here is a brief introduction to the basic principle of the observer pattern, because there is a very good explanation on the Internet, so there is no too much analysis here, let's look at how to use this mode in the game to decouple the relationship between modules. Consider a combat unit in the game, and call it Boss,boss has a very important information volume (HP), in a boss battle will have multiple modules dependent on the boss's blood volume Changes: (1) UI module: Boss's blood bar needs to display the current remaining amount of blood in real time. (2) Boss skills: When the amount of blood reduced to a certain value will trigger the corresponding skills (big strokes, summon small strange, etc.). (3) AI module: When the opponent's boss blood volume reaches a certain value, can trigger some kind of AI. (4) More modules may be dependent on HP values.
So many modules rely on changes in HP values, so it is very prudent to handle the dependencies between modules, otherwise the coupling between modules can be extremely complex (of course, if the system is not complex coupling is not a big problem), then in the real project is how to deal with this dependency? Observer pattern, but we are not using the standard observer pattern, which is a variant of the observer pattern, as shown in:

The handler refers to the Python function (class function) We define, the boss is an instance object of our member class, Handler can be a function of any module, such as Skill module, AI module, UI module, as long as its function definition conforms to def function ( Event) format (because Python is a scripting language and therefore an abstraction of meaning), where Evntkey is the event that the function listens on to the object, such as the monitor for HP changes, we define M_HP. When member's HP is changed, the active call to Dispatchevent, all interfaces registered with M_HP will be recalled.
In fact, the observer pattern is a very simple mode, but also a very common mode, which is suitable for solving the problem of 1 to n one-way message delivery type, above HP example, we use this mode is very easy to solve the problem of coupling between different modules, Of course, we did not complete the standard observer pattern of the architecture, simple deformation, but more conducive to the resolution of specific problems.




Design Patterns in the game (1)--observer mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.