The "in Java" rumination note (5)---The Strategy design pattern of the interface __java

Source: Internet
Author: User
See a code in the 9.3 (fully decoupled) section in the Nineth interface of the look in Java, posted as follows:
Import java.util.*;   Class Processor {public String name () {return getclass (). Getsimplename (); Object process (object input) {return input;}}
Class Upcase extends Processor {string process (Object input) {//Covariant return return ((String) input). Toupperca   SE (); } }
Class Downcase extends Processor {string process (Object input) {return ((String) input). toLowerCase (); } }
Class Splitter extends Processor {string process (Object input) {//The split () argument divides a string into Piec   Es:return arrays.tostring (((String) input). Split ("")); } }
public class Apply {public static void process (Processor p, Object s) {System.out.println ("Using Processor" + P.N     Ame ());   System.out.println (p.process (s)); }
public static String s = ' disagreement with beliefs ' by definition incorrect ';
public static void Main (string[] args) {process (new Upcase (), s);      Process (new Downcase (), s);   Process (new Splitter (), s); } }
The strategy design pattern is used above. In the above code I just see the abstract class, that is, with the processor abstract class, and then the base class inherits it, that's all.        Well, since this pattern is not carefully explained in this book. Ask yourself a few questions first:
1. What is the policy model? 2, what is the use.  3, what the occasion to use.  4, what harm.                 5, give a code example bar. Curiosity is the source of all wisdom.
OK, start asking yourself the answer to your own model.
first, what is the strategy model? As this is written in Java and schema, the policy pattern belongs to the behavior pattern of the object. The intention is to encapsulate each algorithm in a separate class with the same interface for a set of algorithms so that they can be replaced by each other.         The policy pattern allows the algorithm to change without affecting the client. With the definition, that is to analyze the definition: 1, said is the object of the behavior pattern, that is, this model is ultimately targeted at the behavior of objects, is highly abstract of the behavior of the object, then why the object of the behavior of highly abstract it.              It's not yet known, look below. 2, it is intended for a set of algorithms, each algorithm is encapsulated into a separate class with the same interface, so that they replace each other. This is a bit of a hassle, it might be said, to encapsulate an identical set of algorithms into an interface.    Similar to the following code: 3, the policy pattern allows the algorithm to change without affecting the client. This is the ultimate goal. second, what is the use AH. That means there's some good. Can be summed up in a word: You can dynamically change the behavior of the object, the expansion of good AH.
Iii. what occasions to use. 1, if there are many classes in a system, the difference between them is only their behavior, then use the policy model can dynamically let an object in many behaviors choose one behavior.                 2. A system needs to dynamically select one of several algorithms. 3, if an object has a lot of behavior, if not the appropriate pattern, these behaviors will have to use multiple conditional selection statements to achieve.
Note: Have you noticed. In other words, if it is all calculation, calculation has plus, minus, multiply, in addition to four kinds of words, users can choose one, then implement this can use the policy model. The emphasis is on having multiple choices, and then selective computing, which, if not done, can only be chosen with multiple conditions.
four, what harm. The client must know all the policy classes and decide which policy class to use.
The policy pattern will result in a number of policy classes that can reduce the number of objects to some extent by using the pattern.

Let 's give a code example.
Step 1
Create an interface.
Strategy.java
Public interface Strategy {
public int dooperation (int num1, int num2);
}
Step 2
Creates an entity class that implements an interface.
Operationadd.java
public class Operationadd implements strategy{
@Override
public int dooperation (int num1, int num2) {
return NUM1 + num2;
}
}
Operationsubstract.java
public class Operationsubstract implements strategy{
@Override
public int dooperation (int num1, int num2) {
return num1-num2;
}
}
Operationmultiply.java
Public class Operationmultiply implements strategy

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.