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

Design patterns-Command, Template Method, Strategy

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Command-- object behavior pattern

The intention is to encapsulate a request into an object so that you can parameterize customers with different requests; queue or log requests; and support undoable operations. The alias Action,Transaction applicability abstracts the actions to be performed to parameterize an object to specify, arrange, and execute requests at different times. Cancel operation is supported. Log modification is supported. Construct a system with high-level operations built on primitive operations. structure

The effect Command mode decouples the object that invokes the operation from the object that knows how to implement it. Command is a first-class object. They can be manipulated and extended like other objects. You can assemble multiple commands into a compliant command and add a new Command easily, because there is no need to change existing classes. To implement the question, to what extent should a command object only support undo and redo to avoid the accumulation of errors during the cancellation operation using C++ templates

Template method---- class behavior pattern

It is intended to define the skeleton of an operation's algorithm, while delaying a step to a subclass. Applicability implements part of an algorithm at once and leaves variable behavior to subclasses to implement. Common behaviors in each subclass should be extracted and grouped into a common parent class to avoid code duplication. Control subclass extension. structure

The effect template method leads to a reverse control, a structure sometimes referred to as the "Hollywood rule", that is, "Don't look for us, we'll find you." There are three problems with the implementation: using C++ access control, a primitive operation of a template method call can be defined as a protected member. Minimize primitive manipulation naming conventions, which we use almost every day, onCreate,onResume in Activity in android development, draw in View, and so on. The Strategy-- object behavioral pattern is intended to define a series of algorithms, encapsulate them one by one, and make them interchangeable. This mode allows the algorithm to change independently of the customers who use it. Alias Policy applicability many related classes simply differ in behavior. Policy provides a way to configure a class with one of multiple behaviors that requires a different variation of the algorithm. The algorithm uses data that the customer should not know. Policy patterns can be used to avoid exposing complex, algorithm-related data structures. A class defines multiple behaviors, and these behaviors occur in the case of multiple conditional statements in the operation of the class. Move related conditional branches into their respective Strategy classes to replace these conditional statement structures

Collaborate between Strategy and Context to implement the selected algorithm. When the algorithm is called, the Context can pass all the data needed by the algorithm to the Strategy. Alternatively, Context can pass itself as a parameter to the Strategy operation. This allows Strategy to call back Context when needed. Context forwards its customer's request to its Strategy. The customer usually creates and passes a ConcreteStrategy object to the Context; so that the customer only interacts with the Context. There is usually a range of ConcreteStrategy classes for customers to choose from. Effect (pros and cons) A series of related algorithms an alternative inheritance method to eliminate the choice of some conditional statements the customer must understand the communication overhead between different StrategyStrategy and Context increases the number of objects implementation problem definition Strategy and Context interfaces use Strategy as a template parameter to make the Strategy object optional

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