The policy mode belongs to the behavior pattern of the object. It is intended for a set of algorithms that encapsulate each algorithm in a separate class with a common interface, so that they can be replaced with each other. The policy pattern allows the algorithm to change without affecting the client.Structure of the policy patternThe strategy pattern is the wrapper over the algorithm, which separates the responsibility of the algorithm and the algo
Objective
Just worked overtime, eh, the company stipulated that usually overtime only 10 dollars of meal supplement, Saturday and Sunday overtime, only to give a series of leave, in the state-mandated holidays in accordance with 3 times times the wages issued. So for so many ways to calculate overtime, the company's OA system is how to do it? This is going to be about the strategy design pattern I've summed up here today.
Policy mode
In Gof's desig
the strategy model of Java and Schema (reprinted from http://www.cnblogs.com/java-my-life/archive/2012/05/10/2491891.html)
In Dr Shanhong's "Java and Schema" book, this describes the policy (strategy) pattern:
the policy pattern belongs to the behavior pattern of the object. The intention is to encapsulate each algorithm in a separate class with a common interface for a set of algorithms so that they can
Original source: http://www.cnblogs.com/java-my-life/archive/2012/05/10/2491891.html
In Dr Shanhong's "Java and Schema" book, this describes the policy (strategy) pattern:
the policy pattern belongs to the behavior pattern of the object. The intention is to encapsulate each algorithm in a separate class with a common interface for a set of algorithms so that they can be replaced by each other. The policy pattern allows the algorithm to change witho
Intent
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
Capture the specified action in an interface, bury implementation details in derived classes.
Problem
One of the dominant strategies of object-oriented design is the "open-closed principle ".
Figure demonstrates how this is routinely achieved-encapsulate interface details in a base c
What?Define a family of algorithms, encapsulate each one, andMake them interchangeable. Strategy lets the algorithmVary independently from the clients it. --gofDefine a series of algorithms, encapsulate each one, and make each package interchangeable with each other. The strategy model replaces this series of algorithmic algorithms in a way that is independent of the client using these algorithms. Free to r
In order to understand the motive of introducing the concept of "mixed strategy" into game theory, we look at the result of solving the fairly simple "guessing game" with "Underline method", the result is as shown in Figure 8.3.1.
The answer is that, in the sense of pure strategy, "guessing game" has no solution, that is, there is no Nash equilibrium in the sense of pure
I. Strategy ModeThe rule mode is intended for a group of algorithms,Encapsulate each algorithm into an independent class with a common interfaceSo that they can be replaced with each other. The policy mode allows the algorithm to change without affecting the client.
Suppose we want to design a shopping cart CAT System for e-commerce websites selling books. The simplest case is to multiply the unit price of all goods by the quantity, but the actual sit
Design Pattern Explained Reading Notes 4 -- Strategy, patternexplainedWhat?
Define a family of algorithms, encapsulate each one, andMake them interchangeable. Strategy lets the algorithmVary independently from the clients that use it. -- GOF
Define a series of algorithms, encapsulate each and make each encapsulation replaceable. The Strategy Mode replaces these a
The 8th chapter of the design pattern-strategy mode (Java implementation) "Year ago Big bargain, Ah, now Barber rushed 500 to get 300, rushed 1000 to send 500." Fish brother hurriedly charge Ah, haircut this thing basic one months a back, very affordable AH. However, the boss of the barber shop is so silly, rushed 1000 to send 500, than two times 500, so you can send 600. "It only means that you are not very stupid, but also not smart." "Ah?" Did I t
Rule Mode
The behavior pattern of an object is an algorithm that encapsulates each algorithm into an independent class with a common interface, so that they can be replaced with each other.
The policy mode allows the algorithm to change without affecting the client.
Policy mode is the packaging of algorithms. It separates the responsibility for using algorithms from the algorithms themselves and delegates them to different objects for management.
Roles involved in rule mode:
Environment role C
Design Mode (11) Strategy Mode
I. Discounts
A website that sells books wants to build a settlement system. Part of the system is to calculate the price of books. The books on this website are basically discounted, and the discounts for different types of books are different, for example, a comic book may take off, or a novel may take off. Their initial design was like this.
Solution 1: Judge on the client
if(book is comic) price*=0.9;else if(book i
definition: Define a set of algorithms that encapsulate each algorithm and allow them to be interchanged.Type: behavior class modeClass Diagram:The policy pattern is the encapsulation of the algorithm, which encapsulates a series of algorithms into corresponding classes, and these classes implement the same interface, which can be replaced by each other. In the behavior class pattern, there is a pattern is also concerned about the encapsulation of the algorithm-template method mode, the contro
Strategy Mode (Strategy pattern)
embodies the principles of two very basic object-oriented design: The concept of encapsulation change and the use of interfaces in programming, rather than the implementation of interfaces.
1, the definition of the policy model:
Defines a set of algorithms that encapsulate each algorithm and allow them to be interchangeable. The policy pattern enables these algorithms to cha
Policy patterns are well applied in TreeSet and TreeMap in the Java collection, and we can implement the comparator interface implementation CompareTo () method to define our own collation, and then through TreeSet, TreeMap constructs an instance of implementing the interface, the order in the map is our custom order. We can completely define our own rules and use them very conveniently. So, what is the strategy model?Policy pattern Definition: Define
The rule mode is similar to the provider mode I just wrote. However, the configuration is different. Below are some basic demos:
Mode definitionDefinition: "prepare a set of algorithms and encapsulate each one so that they can be exchanged. "Pattern intentThe purpose of the rule mode is to encapsulate each algorithm into an independent class with a common interface for mutual replacement. These algorithms can be replaced with each other and provide a method to select the most suitable algorithm.
Policy pattern: Defines the algorithm family, encapsulates each other, so that they can be replaced each other, this pattern makes the change of the algorithm independent of the client who uses the algorithm.
The subclass family of the parent class needs to extend the new function frequently, in order to use the parent class to add the subclass more flexibly, and to write the behavior of the parent class as an interface (interface);
Use the Set () method to reset the behavior of the interface.
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 calling algorithm.The following is the strategy class diagram of the big talk design pattern
, confusion, there is a kind of wood to write again once again impulse? Well, if you're in a situation like this. Design patterns may be part of your rescue.But. There are a lot of people in the design pattern who are addicted to the design pattern, and every few lines of code you have to consider--is there a need for a pattern! Remember. Mode is a set of coding rules that is summed up in order to facilitate our solution to this problem. It is not that we do not need to design pattern code exten
The example of this article for everyone to share the Java strategy model for your reference, the specific contents are as follows
1, the Strategic model (strategy pattern) is a relatively simple model, also known as the Policy Model (Policypattern).The definition is as follows:
Define a family of algorithms,encapsulate each one,and make them interchangeable.(defines a set of algorithms that encapsulate e
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.