Strategy is a pattern of object behavior in design patterns, which mainly defines a series of algorithms and encapsulates them into separate classes.
Stratrgy application is more extensive, for example, the company business Change chart, there may be two ways to achieve, one is the line curve, one is the Block Diagram (bar), which is the two algorithms, you can use str
Head first design mode Study Notes (1) -- duck and Strategy Mode
Design Principle 1: Identify potential changes in the application and separate them from those that do not need to be changed.
Design Principle 2: programming on excuses, not on implementation
Design Principl
Big talk design mode The second chapter: the strategy mode algorithm can call each other policy mode is used to encapsulate the algorithm.
Big talk design mode Chapter Three: single duty principle single duty principle: In the case of a class, there should be only one cause for it to change the responsibilities of the class can not be too much, because sometimes
During software development, various decisions are always faced, ranging from module division to function body writing. These are all design problems. It is not difficult to design, but it is difficult to make a good design. But what is a good design? What is the standard?
In military terms, "Tactics serve
Design Pattern (1) Strategy Pattern
The policy mode defines the algorithm family and encapsulates them separately so that they can be replaced with each other. This mode makes the algorithm changes independent of the customers who use the algorithm. This abstract definition is too drunk. Let's take a look at the code ".
The current requirement is to design a Pers
This paper describes the design idea of reusable and extensible MIME coding class, and compares the difference between template method and strategy mode by the way.
I. Background knowledge
MIME is an Internet protocol, all called "Multipurpose Internet Mail Extensions", and the Chinese name is "Multipurpose Internet Mail Extension." In fact, its application is not limited to sending and receiving Internet
I,Definition
I recently started to study the head first design model. This book is refreshing and difficult to get sleepy. Today, I will share with you the policy model.
1.Definition:
Policy mode definedAlgorithmFamily, respectively, so that they can replace each other. This pattern makes the algorithm changes independent of the customers who use the algorithm.
The policy mode structure, where:
separate policy pattern. You only need to provide the name of the policy when you need to verify it. Just like this:Nameinput.addvalidata ({ true, true, += { function (value) { return value!== '; } ,function(value, MaxLen) { return value.length () > maxlen; As you can see, various validation rules are easily modified and replaced with each other. If one day the Product manager recommends that the character be too long, the limit is changed to 60 characters. It only t
Policy mode: defines the algorithm family, which is encapsulated separately so that they can replace each other. This mode allows algorithm changes to be independent of algorithm customers.Simple Factory: completely independent object creation, so that object creation is irrelevant to specific users.
A simple factory is not strictly a design pattern, but a good programming habit.
Glossary:
The Factory model can be understood as follows. Suppose there
, comparator),NewPerson ("C", 173, 66, Comparator)}; sort (persons); //Sorting persons by sorting algorithmSystem.out.println (arrays.tostring (persons)); } Public Static voidsort (person[] persons) { for(inti=0; i) { for(intj=0; j) {person P1=Persons[j]; Person P2= Persons[j+1]; if(P1.compareto (p2) > 0) {Persons[j]=P2; Persons[j+1] =P1; } } } }}View CodeOK, the requirements have been fulfilled. The answer is yes, according to how tall and how weight can be
, each of which has different property values;b) A class it needs to create several kinds of objects, each of which is a subclass of a parent class/interface;The policy class created by the policy mode can only be the second one, and all subclasses must have a common function so that the contextinterface () Call of the context class.The role of the policy model:1. After using the policy mode, if you need to add a policy, you just need to create a subclass that implements the
implementing the different, he can call all the methods in the same way, reducing the coupling between the various algorithm classes and using the algorithm class. On the other hand, it is easy to define the algorithm classes individually, as well as unit tests, because they can be individually tested by their own algorithms. In practice, not only can encapsulation algorithms, but also can be used to encapsulate almost any type of rules, in the analysis process need to apply different business
[] args) {//Select and create the policy object you want to useDiscountstrategy Discountstrategy =NewIsmemberdiscountstrategy (); //Create an environmentCalculatedprice Calculatedprice =NewCalculatedprice (discountstrategy); //Calculate PriceCalculatedprice.quote (300); }}Advantages of the policy model(1) The Strategy mode provides a way to manage the associated algorithm families. The hierarchy structure of a policy class defines an algorithm or a fa
Article Description: The interface design specification reflects Apple's focus on the design and development quality of iOS applications, enabling Third-party developers to work hard to meet the high expectations of users.
The interface design specification reflects Apple's focus on the design and developme
Price * 0.5; else if (PersonType = = ' old ') { Span style= "color: #008000;" >// old customer 30 percent return Price * 0.3; else { return Price; // All other full price We look at the above as a class, if we want to expand a price method, we have to add a new else if, or modify an algorithm logic, you need to modify an if or else if, this is a single class modification, and this situation will often change this class, This violates a principle of the
square. To represent the coverage method, the dominoes are numbered.#include 7 "Round robin CalendarThere are n=2k players competing to design a match schedule that meets the requirements:(1) Each contestant must compete with the other n-1 players once;(2) Each contestant can only race once a day.According to this request, the tournament schedule can be designed as a two-dimensional table of n-row n-1 columns, where section I of row J indicates the c
Policy Mode, encapsulating a specific set of behaviors and algorithms into classes to accommodate certain contexts, a pattern that
Policy Mode,
Policy ModeFor the free switching and expansion of the algorithm, it is widely used
Design PatternsOne.
The policy mode is used for the free switching and expansion of the algorithm, which is one of the most widely used design patterns. The policy pattern corresp
Edward on 2016/7/9.3 */4 Public Abstract classCharacter {5 protectedWeaponbehavior weapon;6 Public Abstract voidfight ();7 Public voidSetweapon (Weaponbehavior weapon)8 {9 This. Weapon =weapon;Ten } One}Weaponbehavior.java1 /** 2 * Created by Edward on 2016/7/9. 3 */ 4 Public Interface Weaponbehavior {5 void useweapon (); 6 }King.java1 /**2 * Created by Edward on 2016/7/9.3 */4 Public classKingextendsCharacter {5 @Override6 Public voidfight () {7
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.