consider the possibility of using a policy model to handle this change.(6) The Simple Factory mode requires the client to recognize two classes, and the use of the policy mode and the simple factory pattern, the client only need to know a class context.Iv. implementation of C + +(1) Strategy: Strategy.h#ifndef strategy_h#define strategy_h#include (2) Strategy: CashContext.h#ifndef cashcontext_h#define cash
Abstract
Head first design patterns is using strategy pattern as the number one example, and Professor Chen Junshang is also using strategy as the first pattern of teaching, Visible strategy is indeed suitable for beginners to learn the first pattern.
Intent
A whole family algorithm is defined, and each algorithm is
In Gof's design model: The basics of reusable object-oriented software, the strategy pattern is said: Define a series of algorithms, encapsulate them one by one, and make them interchangeable with each other. This pattern allows the algorithm to be independent of the customers who use it.Strategy mode in order to adapt to different needs, only the change point encapsulation, the change point is to achieve d
The policy pattern is also a very common design pattern and is not complex. Let's take a look at the pattern here.Definition: A policy pattern defines a series of algorithms, encapsulates each algorithm, and makes them interchangeable. The policy pattern allows the algorithm to vary independently from the customer who uses it.
Role:
Abstract policy Role (strategy): Abstract Policy class.
Specific
mode applies only to the clientKnowing all the algorithms or behaviors of the situation.2. The policy model causes a lot of policy classes. It is sometimes possible to design a policy class to be shareable by saving the environment-dependent state to the client, so that the policy class instance can be used by different clients. In other words, you can makeUse the enjoy meta mode to reduce the number of objects.The
an interface. So the duck's behavior will not be tied to death on the Duck class.}3. Multi-use combination, less inheritance {In the example, the combination is a combination of the Duck class and the fly and other behavior classes, using the combination to build the system can have a great flexibility, but also can change the behavior at run time}4. The process of rewriting the above example is using the strategy mode {The
Abstract
In (original) My Design journey: using template to improve strategy pattern (OO) (c + +) (template), use C + + Template improved strategy pattern, this article uses C # 's generic to improve strategy pattern.
Introduction
C # 2.0 joined the generic support for generics, so you want to change the original C
In our development process, often because the initial structure is not reasonable and may cause the back of the customer or project manager to change the requirements, we need to modify the original code, on the one hand is likely to write a long time to review the trouble, on the other hand is reaching, change a place cause all the places need to change, This shows how important a good architecture is. So today we are going to talk about a strategy
The definition of the policy pattern: Define the algorithm families, encapsulate each other, so that they can be replaced with each other, this pattern allows the algorithm to change independently of the customers using the algorithm. Essence: Separation algorithm, select implementationObject-Oriented design principles:Package changesMulti-use combination, less inheritanceProgramming for interfaces, not for implementationFind out where changes may be
= = = ' S ') ){ - returnlevels (salary); - } the if(level = = = ' A ' ){ - returnLevela (salary); - } - if(level = = = ' B ') ){ + returnLevelb (salary); - } + }; A atCalculate (' A ', 10000);//Output: 30000This code is better than the above, but still does not solve the key problem, the Calculate function may become more and more large, and the function is still lack of elasticity.idea three : The strategy
diagram is very similar to that of bridge.
Similarly, context contains a pointer to the strategy object, so that context can use different policy algorithms, or even sub-classes of context, so that the class diagram is exactly the same as that of bridge.
In fact, all modes can only be divided into class mode and object mode. Class mode uses inheritance While object mode uses delegation, the bridge mode is similar to the
;usingSystem.Text;usingSystem.Threading.Tasks;namespacePolicy Mode { Public classBlockenemy:istrategy { Public voidoperate () {Console.WriteLine ("This is strategy 3 ....."); Console.WriteLine (""); } }}Step three: Write the calling codeusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacePolicy Mode { Public classContext {/// ///constructor to pass in the policy you want to use/
The strategy mode of PHP design mode
Policy mode:
The policy pattern is the behavior pattern of an object, which is intended to encapsulate a set of algorithms. Dynamic selection of the desired algorithm and use.
The strategy model refers to a pattern that involves decision control in a program. The strategy mode
Layout strategy of tablet computer
Using fragmentation and multiple pane views
The building blocks of the honeycomb design are "fragments." Fragmentation is the layout component of a self-contained system that can change its size and placement depending on the direction and size of the screen. A further solution to design this problem for a variety of size skin
Source: SP Forum
Author: fdsa
1) Background of the game
The big mobile phone strategy is a very good macro War simulation game. Based on World War II, it simulates more than 20 historical and hypothetical battles on mobile phones, it basically simulates all the elements of war except politics, including production, economy, strategy, weapons, tactics, weather, terrain, day and night, generals, reconnaissanc
Based on the OO Foundation, begin to learn design patterns carefully! Design patterns are essential in Java design!
Apple.java
Package strategy;
/**
* *
@author Andy * */Public
class Apple implements discountable {
//weight
private double Weight;
The exact calculation of
Looking at "C + + Design new thinking", found in the beginning of a large length of the introduction of the Strategy Model (policy), the strategy model is not a classic design pattern, but in fact, in our day-to-day development is essential. Policy, strategy, policy, the mea
and common design principles, high cohesion, low coupling.Disadvantages:1, because each specific policy class will produce a new class, it will increase the number of classes the system needs to maintain.2, in the basic policy model, the choice of the specific implementation of the responsibility to be borne by the client object, and transferred to the policy mode of the context object
Implementation example:Strategy.h
#include
Strategy.cpp
Temperature so know new, each reading design mode will have a different experience, adhere to a design pattern every day, until skilled use of design patterns.The policy pattern defines many algorithms that do the same work, but with different implementations, it can invoke all the algorithms in the same way, reducing the coupling between the algorithm and the ca
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.