Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Design patterns in Unity Game Development

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of design patterns in Unity game development, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

1. What is the design pattern?

First of all, we should know that the design pattern is in accordance with the "principle of object-oriented design", emphasizes taking class, object, inheritance and combination as the way of software design analysis, and puts forward solutions to similar problems, and mainly meets the following requirements.

Solve recurring problems

Put forward a plan to solve the general problem

Make the solution reusable

In short, the design pattern focuses on the word "pattern", which, like opening a franchise, provides a set of reusable strategies, from location selection, decoration, supply channels, etc., and can expand rapidly.

Similarly, when software developers encounter the same problem, they no longer have to think about analysis and design, but can find the corresponding solution directly from the design pattern and use it directly. In this way, it not only shortens the development time, but also enhances the stability and maintainability of the software.

two。 Game development and design pattern

In game development, we will face many problems, as follows

An extremely stable framework is needed to hold tens of millions of users

A growing number of game systems

Changes to existing game features

Agile development

At this point, we need to use the design pattern, because it is the wisdom of our ancestors, it is a proven pattern, and there is no need to think about new solutions.

Design pattern has become a common voice in the field of software design, so it can also reduce misunderstandings in communication between people, and can directly indicate what design patterns are used in the game system. For small game teams, it also improves the stability and expansibility of the system.

3. Seven design principles

Principle of single responsibility

This principle emphasizes that "when designing and encapsulating a class, the class should be responsible for only one thing."

It seems simple to say, but in fact, the division of functions is often one of the biggest headaches for developers.

The solution is to constantly restructure the class, extract part of the function into a new class, and then use the combination way to add the new class to the original class, which will slowly become a class only responsible for the implementation of a single function.

Opening and closing principle

This principle emphasizes that "a class should be open to extensions and closed to modifications."

Specifically, for functions that have been tested or put online, we should no longer modify any interfaces or implementations of this class, but we should remain open to the increase in functionality.

In order to meet the requirements of this principle, we need to raise the "method" to the interface and devolve the "implementation" to the subclass, so that when we add a requirement, we re-implement a subclass that inherits from the interface or the old subclass. then add new functionality to the new subclass, ensuring that the old functionality remains unchanged (off for modification) and new functionality (open for extension).

Richter's substitution principle

What is emphasized here is that "the subclass must be able to replace the parent class"

With regard to this concept, what is introduced in general books is relatively abstract and has perplexed many people. The author here unifies the multi-party material, talks about own understanding.

First of all, the Richter substitution principle is the basis of inheritance and reuse, which reflects the relationship between parent classes and subclasses.

Popularly speaking, where there is a parent class, it is all replaced by a subclass, which does not affect the operation of the program, so it meets the principle of Richter scale replacement.

What kind of subclass will not affect the running of the program when replacing the parent class?

This subclass can extend the functionality of the parent class, but cannot modify the original functionality of the parent class. This also complements the principle of single responsibility-open to extensions and closed to modifications.

If the principle of Richter scale replacement is violated, the probability of program error will be greatly increased.

Give me a chestnut?

We define a parent class, the bird, and have a method to return the flying altitude of the bird. Two more subclasses are defined: sparrows, penguins, and penguins rewrite the method to return flight altitude so that the return value is 0. When the outside needs to get the altitude of all birds and use it as the denominator of the divisor, we all know that 0 cannot be used as the denominator, and the program goes wrong.

This chestnut has the taboo of "the child class modifies the original function of the parent class", which violates the Richter substitution principle, that is, the inheritance structure cannot be adopted and the relationship between them should be redesigned.

Principle of dependency inversion

This principle contains two themes.

High-level modules cannot rely on underlying modules, both of which should rely on abstract concepts

The abstract interface should not depend on the implementation, and the implementation should depend on the abstract interface

The abstract concept here, I understand as an interface, so this is telling us to program for interfaces, not for implementation.

So why are we programming for interfaces? let me give you a real-life chestnut.

Our computer is the high-level module, and all kinds of hardware devices are the bottom modules. every time we add a hardware device (bottom module), the computer (high-level module) needs to design a new interface to be compatible with the device. in this way, the high-level module depends on the bottom module, and this does not meet the opening and closing principle, because every new device has to be modified to the computer. But if the computer defines a general interface, each hardware device follows the interface protocol, and everyone can plug into the same interface, then the computer is no longer dependent on the hardware device, and both now only need to communicate with the middle-tier interface, that is, both rely on abstract concepts.

So from here we can see that the dependency-induced principle is also one of the important ways to realize the open-close principle, and its purpose is to reduce the coupling between classes through interface-oriented programming.

Interface isolation principle

What is emphasized here is that "clients should not be forced to use interface methods that they do not need"

In fact, it requires us to establish a dedicated interface for each class, instead of trying to build a huge interface for all the classes that need it to call it.

Principle of least knowledge (Dimitt's rule)

By definition, a class should have the least knowledge about other classes

If there is no need for direct communication between the two classes, then do not call each other, just give it to a third party for forwarding

Give me a chestnut?

The boss of the company does not need to communicate directly with every employee in the company, he just needs to communicate with the project manager, and the project manager is responsible for conveying the boss's instructions, so that the boss has the least knowledge of other employees and removes his dependence on each employee (coupling). Now he only depends on the project manager. As for the grass-roots personnel, of course, they can just talk about it.

However, it needs to be weighed repeatedly when using the Demeter rule. If it is not used properly, a large number of intermediary classes will be generated, which will confuse the project structure.

Principle of synthetic reuse

In fact, the principle of synthetic reuse is that if combination can be used to solve problems, there is no need to inherit.

That is, combination is better than inheritance.

Why is combination better than inheritance?

First of all, if inheritance is used, the subclass overrides the parent method, which violates the Richter substitution principle and makes the program more likely to make errors. Here, the principle of synthetic reuse and the principle of Richter substitution complement each other.

Second, with inheritance, the subclass depends on the implementation of the parent class, which is not conducive to the extension and implementation of the class.

Finally, C# can not use multiple inheritance, the use of combination will be more clear than layers of inheritance, conducive to the maintenance of the project.

The way to realize this principle is also very simple, which is achieved by adding the existing object into the new object as a member object of the new object, and the new object can call the function of the existing object to achieve reuse.

Thank you for reading this article carefully. I hope the article "sample Analysis of Design patterns in Unity Game Development" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report