Delphi mode programming strategy mode (on)

zhaozj2021-02-16  56

Delphi mode programming strategy mode

Liu Yi

1.1 mode explanation

Strategy mode is used to define a set of algorithms and package each algorithm into a separate class with a common interface, allowing them to replace each other. Policy mode allows algorithms to change independently of its clients.

To understand the motivation and meaning of the policy mode, we have to start with an interesting example. In a material management system, the warehousing and the warehousing module are the core part of the system (here we analyze as an example).

For programmers without object-oriented programming, they tend to put all the logic of the library on the client (outbound boundary), and to determine the category branch statement in the client side. The borrowing is still reported to select different outbound settlement methods, as shown in Figure 1-1. In this way, the client's code is complex and difficult to maintain. For example: When you need to add a new storage list type, you must modify the judgment condition, recompile and publish the client. When the situation is getting more and more complicated, the conditional branches will be more and more, and the added program code will be more and more, so that the client is more harder and difficult to maintain, the possibility of mutual impact and error increases.

Figure 1-1 Outbound module based on process ideas

If you are analyzing the object-oriented idea, you can use the item, the lever, and the reporter will be used as the derived class of the outbound list, as shown in Figure 1-2. Such a library list as a common interface for the document class, and the use of inheritance can achieve different outbound behavior in the subclass. This actually utilizes an important concept in objects: polymorphism.

But such a design is also a place in the United States, which is closely coupled to the environment and behavior. That is, the algorithm of the documents and the specific library is closely coupled together. Strong coupling makes both unable to evolve independently, limiting reuse and scalability.

Figure 1-3 is an outgoing library module that is redesigned with the policy mode. Outlet documents reference the library policy object by an outbound operation object (ie, the context in policy mode). Various specific outbound strategies are implemented by derived classes of the outbound policy. The outlet document can provide an outbound settlement method and a document display interface from the outbound operation and the document style. In this way, the strategy mode will independently independent of the behavior of the library from the environment of the outbound document, and the increase or decrease of the library algorithm will not affect the environment and client.

Figure 1-2 Outbound module based on object-oriented ideology

Figure 1-3 Outbound module based on design pattern ideas

The advantage of the strategy mode is that the algorithm and the environment are separated, both can evolve independently. In order to better illustrate the benefits of algorithms and environmental separation, let's take a look at the design of Figure 1-4. In this design, there is no concept of warehouses and warehocks, because I will abstract all / wrapper documents, the interface and behavior of the shipping period dynamic combination document. You can maintain, query, configure different behavioral classes through outlet / warehousing operation. Abstract outlet / storage behavior encapsulates its corresponding algorithms in a policy class to complete different types of access library documents. This is obvious to improve the system's reuse and scalability, and reduce the difficulty of maintenance.

Figure 1-4 The advantage of the strategy model is that the algorithm and the environment are separated, both can evolve independently

This shows that the policy mode is suitable for the following scenarios:

• When many related classes are only in their behavior. Policy models can dynamically let an object select a behavior in many behaviors.

• When there are multiple optional algorithms to achieve a purpose, such as those defined in different advantages and disadvantages (ie, different strategies equivalent to application). These specific algorithms can be encapsulated into an abstract algorithm class, and enjoy the unified interface of the abstract algorithm. With the polymorphism, the client can use any specific algorithm as long as one abstract algorithm is held.

· When the data used by an algorithm cannot let the client know. Use policy modes to avoid exposure of complex data structures related to algorithms. In fact, the client does not necessarily know that these knowledge and data related to algorithms are known. · When a class definition has many behaviors, and use multiple conditional statements to determine if these behaviors are selected. Policy models can transfer these behaviors to the corresponding specific policy class, thereby avoiding multiple cases of multi-critical conditions that are difficult to maintain, reflecting object-oriented programming ideas.

1.2 Structure and Usage

The structure of the strategy mode is shown in Figure 1-5, which includes the following participants:

• Abstract Strategy (TSTRATEGY) - a common interface is declared for all supported algorithms. TCONTEXT uses this interface to call algorithms defined and encapsulated by TCONCRETESTRATEGY.

• Tconcretestrategy - packages concrete algorithms or behaviors. Implement the TSTRateGY interface.

· TCONTEXT - holds a reference to TSTRategy. Call the TSTRateGY interface, dynamically configure specific algorithm or behavior.

Figure 1-5 Structure of the strategy mode

In the policy mode, the selected algorithm is implemented through the interaction of TSTRategy and TCONText. When the algorithm is called, TCONTEXT can pass all the data required by the algorithm to the TSTRateGY. Alternatively, TCONText can pass itself as a parameter to the TStrategy action.

When TContext forwards the client request to its TSTrategy, the customer usually creates and passes a TconCreteStrategy object to the TCONText; thus, the client only interact with TContext. There is usually a series of TconCretestrategy classes to choose from.

-------------------------------------------------- -----------------------------------------

More related articles and sample program source code can be downloaded by the author website:

http://www.liu-yi.net

转载请注明原文地址:https://www.9cbs.com/read-23461.html

New Post(0)