Talking about the six principles of design patterns in combination with unity project development (1)

  Object-oriented, six principles, design patterns, data structures and algorithms, these knowledge points exist independently of programming languages. This means that they are "more abstract", and abstract things mean that it takes a longer time to learn, understand, and apply.

    Today we mainly talk about the six principles of design patterns:

    First, who are the six principles?

    They are Single Responsibility, Open-Closed Principle, Dependency Inversion Principle, Liskov Substitution Principle, Interface Segregation Principle, and Demeter Principle.

    Second, why do you need to learn these six principles?

    The six principles are the essential principles for us to improve the quality of object-oriented programming code, and also the prerequisite for our understanding of design patterns.

   After you have figured out the learning object and learning purpose, you can get to the main topic. What are these six design principles in essence, what are they used for, and what are the practical applications?

1. Single Responsibility

Definition: For a class, there should be only one reason for it to change. A simple understanding is that a class is only responsible for one responsibility (function).

Understand single responsibility from concrete examples in life,

Are you hungry: single duty is to order food

2306: Single duty is to book train tickets

The singularity of things in life is to concentrate (energy/financial/material) and then make the best of it, and only then can you be more competitive.

The singularity in programming is also for the purpose of concentration, and the functions, functions and uses of a class are concentrated to one point. This can avoid code redundancy, facilitate management, facilitate team communication, and improve project development efficiency.

There are two points of attention in the development process of the Unity project:

① The unity of class function;

②The unity of method and function;

A class is only responsible for one specific thing, and a method is only responsible for one specific function. when you find a party

When the method completes two things, it needs to be properly refactored into two methods, and the class is the same.

Application of single responsibility in unity project development:

①Singularity of the functional class library: For example, the JSON in UGUI is only used for parsing JSON.

②The single function of the engine component class: There is a set of lighting components in the Unity engine , which are used to realize the lighting rendering function. It is divided into 4 components based on different functions: directional light, point light, spot light, area light.

 

The principle of opening and closing

Definition: The full name of the open and closed principle, software entities (classes, methods, modules) should be extendable, but not modified.

specific applications in life,

laptop

Enclosed: The entire notebook is closed, and the back of the notebook is marked with the seals of "Non-professionals, please do not attempt to disassemble or repair" and "Tear up the warranty void".

Open: It means that the notebook provides several USB sockets for us to expand.

The closure on the notebook is to ensure the safety of the device itself. The opening on the notebook is to reserve an expandable interface for the user. The closure in programming is also to ensure the original security . When a new requirement arises, it is extended instead of modifying the original logic. In the face of requirements, changes to the program should be solved by adding new code, rather than changing old code (this is especially important in the direction of game development).

The Unity engine itself is a good example of the open-closed principle.

After Unity officially packaged and released this engine, the programmer of the engine itself cannot modify it, because you do not have the source code ---> close the modification. However, the Unity engine provides an "editor extension function", which provides a complete set of APIs, through which we can extend N plug-ins to the Unity engine to meet our specific needs.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324380588&siteId=291194637