& Lt; 2 & gt; read & lt; big talk design mode & gt; Policy mode, big talk Design Mode

Source: Internet
Author: User

<2> Read <big talk design mode> Strategy Mode and big talk Design Mode

I met you again and insisted on writing the second article.

<Big talk design model> the explanation strategy model starts with the mall cash register software program. The question is, which mall has a strong cash register software, and I am joking. I have read the previous article "simple factory mode", which has two disadvantages: 1. The client depends on two classes and has high coupling. 2. If there are too many algorithms, many classes need to be written. The above problem is solved by the Policy mode.

Policy mode: it defines the algorithm family and encapsulates them separately so that they can be replaced with each other. This mode changes the algorithm and does not affect the customers who use the algorithm.

Mall cash register software: unit price * discount algorithm = Price. For such software, the discount algorithm needs to be changed frequently. In fact, it can be used in a simple factory mode, directly adding the discount algorithm, and then the factory can produce instance objects.

However, this mode solves the problem of object creation. Because the factory itself includes all the charging methods, the mall may change the discount algorithm frequently. Every time you maintain or expand the charging method, you must change the factory, this is very troublesome and does not follow the principles of scalability and reusability. In fact, the change point of the software is the discount algorithm. We only need to encapsulate the change point, and the policy mode can achieve this.

Okay, I got the code and learned from the example.

1. Strategy class, which defines public method interfaces implemented by all Algorithms

/*

* Strategy

*/

PublicpolicactclassStrategy {

 

/*

* Define public interfaces for all Algorithms

*/

PublicpolicactvoidAlgorithmlnterface ();

}

2. Maintain a reference to the Strategy class. To call that class, call that class.

/*

* Maintain reference to the strategy object

*/

PublicclassContext {

 

Strategystrategy;

PublicContext (Strategy strategy ){

This. Strategy = strategy;

}

PublicvoidContextInterface (){

Strategy. algorithmlnterface ();

}

}

3. algorithm

PublicclassConcreteStrategyAExtendsStrategy {

 

@ Override

PublicvoidAlgorithmlnterface (){

//TODOAuto-generated method stub

 

System.Out. Println ("this is algorithm ;");

}

 

}

4. algorithm B

PublicclassConcreteStrategyBExtendsStrategy {

 

@ Override

PublicvoidAlgorithmlnterface (){

//TODOAuto-generated method stub

 

System.Out. Println ("this is algorithm B ;");

}

 

}

5. Client call

PublicclassStrategyClient {

 

/**

*@ ParamArgs

*/

PublicstaticvoidMain (String [] args ){

//TODOAuto-generated method stub

 

/*

* Compared with the factory mode, the client only depends on the Context class, and the decoupling is more thorough.

* To add an algorithm, you only need to add an algorithm class.

*/

Contextcontext =NewContext (NewConcreteStrategyA ());

Context. contextInterface ();

// You can see it. If you want B algorithm, you can call its method by simply taking an example.

Contextcontext2 =NewContext (NewConcreteStrategyB ());

Context2.contextInterface ();

}

 

}

Summary: The Rule mode is a method that defines a series of algorithms. From a conceptual point of view, all these algorithms perform the same job, but they only implement differently, it can call all algorithms in the same way, reducing the coupling between various algorithm classes and algorithm classes.

With Code address: http://download.csdn.net/detail/jzhf2012/8084405


Which of the following is the best design mode?

The big talk is intended for beginners. It is intended for readers who have a certain foundation to improve their abilities.

Reading the story is just a matter of perceptual knowledge. It is suitable for beginners who have no project experience (or the amount of code reading and writing ).

After reading more than 20 basic concepts in 23 design patterns, to further deepen the promotion, Zen has a lot of relevant theories from the author's project experience, which allows people with a certain degree of foundation to reflect on and integrate the knowledge points of the design model of perceptual knowledge during the reading process.

Because the design pattern is not an application-based technology, it is only a theory, it needs to understand, think, and then apply it to a specific programming language to implement the idea.
 
What is the difference between the java rule mode and the proxy mode?

There is no difference between the two...
Not the same thing at all

Rule mode:
Multiple classes only differ in performance. You can use the Strategy Mode to dynamically select the behavior to be executed during running.

Proxy Mode
Provides a proxy for other objects to control access to this object.

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.