Use scenarios of various design patterns

Use scenarios of various design patterns

1. Creational Patterns

1.1 Prototype Pattern

Use prototype instances to specify the types of objects to be created, and create new objects by copying these prototypes.

Application scenario: not the same object, but belong to the same kind of copy, such as copy technology.

1.2 Singleton Pattern

Make sure that there is only one instance of a certain class, instantiate it by itself and provide this instance to the entire system.

Application scenarios: control the sound playing in the game; UI window display; resource loading; timer, etc.

1.3 Abstract Factory Pattern

Provide an interface to create a family of related or dependent objects without specifying a specific implementation class.

Application scenarios: the same type of use, game difficulty, game camp: different types of buildings, heroes and skills can be generated in the game at different levels of difficulty; AI tactics selection: to determine several optional tactics as product families, and each tactic is based on Similar to the navy, air force, and army as the product level structure, specific arms are selected for production and construction; internationalization: when the user changes the language environment, the response text display, sound effects, picture display, operation habit adaptation, etc.; skin replacement or resource management: The user's choice of skins with different themes will affect the image display and animation effects; screen adaptation: use different display resources for high, medium, and low resolution mobile devices.

1.4 Builder Pattern Builder Pattern

Separating the construction of a complex object from its representation so that the same construction process can create different representations. This design pattern is called the builder pattern.

Application scenario: The complex internal structure of the product objects that need to be generated, these products have commonality; the creation and use of complex objects are isolated, and different products created by the same creation.

1.5 Factory Method Pattern

Define an interface for creating objects and let the subclass decide which class to instantiate. The factory method delays the instantiation of a class to its subclasses.

Application scenarios: game scene conversion; role AI state management.

2. Structural Patterns

2.1 Adapter Pattern

Convert the interface of a class to another interface that the customer wants. The adapter pattern allows classes that cannot work together because of interface incompatibility to work together.

Application scenario: transfer, reuse existing functions, but the existing function interface is not what the client wants.

2.2 Bridge Pattern

Decouple abstraction and implementation so that the two can be changed independently.

Application scenario: A class has two independent changing dimensions, and both of these dimensions need to be expanded; the structure needs to be simplified from inheritance or multiple inheritance, and to cope with frequently changing requirements.

2.3 Composite Pattern

Combine objects into a tree structure to represent a "part-whole" hierarchical structure, so that users have consistency in the use of single objects and combined objects.

Application scenario: The logic and UI of different interfaces are uniformly controlled through a manager.

2.4Decorator Pattern decoration pattern

Add some additional responsibilities to an object dynamically. In terms of increasing functions, the decoration mode is more flexible than generating subclasses.

Application scenario: Write a corresponding combat module, but add some functions before or after entering, such as log output, data monitoring, and data collection triggering.

2.5 Facade Pattern

It is required that the communication between the external and internal of a subsystem must be carried out through a unified object. The appearance mode provides a high-level interface, making the subsystem easier to use.

Application scenario: The internal operation mechanism of a car is complicated, but it provides us with advanced interfaces such as steering wheel, instrument panel, brake, and throttle. We don’t need to understand the engine system, power transmission system and other complex systems, so the focus of the appearance mode is: Hide the interactive details inside the system and provide a simple and convenient interface. After that, the client only needs to use this interface to operate a complex system and let them run smoothly.

2.6 Flyweight Pattern

Using shared objects can effectively support a large number of fine-grained objects.

Application scenario: Gobang creation; some objects that are constantly created and destroyed in the game, the bullet object pool.

2.7 Proxy Pattern

Using shared objects can effectively support a large number of fine-grained objects.

Application scenarios: proxy the functions of Text, Button, Image and other components; login settings; delegation; proxy downloading resources, etc.

3. Behavioral Patterns

3.1 Command Pattern

The command mode encapsulates "requests" into objects so that different requests, queues, or logs can be used to parameterize other objects, while supporting undoable operations.

Application scenario: The user's client request realizes the requested queue operation and log light, and supports the cancellation and rollback of the operation.

3.2 State Pattern

When an object's internal state changes, allowing it to change its behavior, the object looks like it has changed its class.

Application scenarios: character status; AI status; account login status; scene status; animation machine status, etc.

3.3 Observer Pattern

Define a one-to-many dependency relationship between objects, so that whenever an object changes state, all objects that depend on it will be notified and automatically updated.

Application scenarios: publish; subscribe to messages; the character's health bar value is changed (defensive power, attack power, etc.).

3.4 Chain of Responsibility Pattern

Allow multiple objects to have the opportunity to process the request, thereby avoiding the coupling relationship between the sender and receiver of the request. Connect these objects into a chain and pass the request along this chain until an object handles it.

Application scenarios: such as the setting of the enemy character in the game, the clearance conditions, and the record of the next level.

3.5 Mediator Pattern

An intermediary object is used to encapsulate a series of object interactions. The intermediary enables objects to interact without explicit interaction, thereby loosening the coupling, and can independently change the interaction between them.

Application scenario: Perception system, which involves visual sensors, auditory sensors, visual triggers, auditory triggers and other interactions that can be completed by an intermediary.

3.6 Interpreter Pattern

Given a language, define a representation of its grammar, and define an interpreter that uses that representation to interpret sentences in the language.

Application scenario: Compiler; these program codes are written in a general text editor and placed in a designated location, such as Lua.

3.7 Iterator Pattern

Provide a way to access various elements in a container object without exposing the internal details of the object.

Application scenario: C# comes with IEnumerator; you need to iterate and query the elements in the collection in a specified order.

3.8 Memento Pattern

Under the premise of not destroying the encapsulation, capture the internal state of an object and save this state outside the object. In this way, the object can be restored to its original state in the future.

Application scenarios: resource backup; historical records; restore operations.

3.9 Strategy Pattern

Define a set of algorithms, encapsulate each algorithm, and make them interchangeable.

Application scenario: The algorithm and the environment are independent, and the increase, decrease, and modification of the algorithm will not affect the environment and the client. For example: plug-in [synchronized pictures, famous quotes, jokes, respective sales algorithms]; construction of skill effect classes and methods .

3.10 Template Method Pattern

Define the framework of an algorithm in operation, and delay some steps to subclasses. This allows subclasses to redefine certain specific steps of an algorithm without changing the structure of an algorithm.

Application scenario: construction of skill effect classes and methods.

3.11 Visitor Pattern

Encapsulate some operations that act on each element in a certain data structure. It can define new operations that act on these elements without changing the data structure.

Application scenario: For example, a class is used to indicate that both the enemy and the soldier can access and do the same thing, and a parent class can be encapsulated, and then subclasses can inherit.

Guess you like

Origin blog.csdn.net/qq_36382679/article/details/114832606