JAVA Design Pattern 1, Design Pattern 1

Source: Internet
Author: User
Tags class manager

JAVA Design Pattern 1, Design Pattern 1

Design pattern is a set of summary of code Design experiences that are repeatedly used, known to most people, classified and catalogued. The design pattern is used to make code reusable, make it easier for others to understand, and ensure code reliability. There is no doubt that the design pattern is win-win for others and the system; The design pattern enables code compilation to be truly engineered; The design pattern is the cornerstone of the software engineering, just like the structure of the building.

This article will introduce the first design pattern in JAVA: the responsibility chain pattern.

What is the responsibility chain model? For example, due to the continuous rise in house prices, our young people, after countless years of hard work, have finally enough money to buy a house. As a result, you are very happy to come to the house sales office, I am going to buy a three-bedroom apartment. This is a sales lady who has gone over and asked: Do you need to buy a house, sir? You are happy to say yes to her. After careful selection, you have taken a fancy to it. You said to the sales lady, could you give me a 5% discount? If yes, the full amount will be paid immediately. This is a 5% discount for the sales lady, which is within my decision, so she is very happy to say: Good sir, please pay. The next day, another buyer came. What about this person? He said to the sales lady, "I will buy three sets at a 10% discount. As the sales lady thought, the 10% discount was beyond my permitted range, so he said to Mr. Wang: Please wait, and I will ask our manager. Then the sales lady went to the manager and reported the situation to the manager. The manager thought that I had 10% of the permissions, so he said yes to the sales lady. Then the sales lady came back and said to Wang laowe, "sir, our manager has agreed to give you a 10% discount. Please pay here. Do you have a certain understanding of the responsibility chain? Next we will use the code to implement this process.

1. Create a discount handler object:

/*** @ Price handler, responsible for handling the customer's discount application */public abstract class PriceHandler {// directly successor, used to transmit the request protected PriceHandler successor; public void setSuccessor (PriceHandler successor) {this. successor = successor;} // process discount application public abstract void processDiscount (float discount );}

2. Create a sales object:

/** Sales, you can approve a discount of less than 5% */public class Sales extends PriceHandler {public void processDiscount (float discount) {if (discount <= 0.05) {System. out. println ("sales approval"); // System. out. format ("% s approved discount: %. 2f % n ", this. getClass (). getName (), discount);} else {successor. processDiscount (discount );}}}

3. Create a sales manager object:

/** Sales Manager, can approve discounts of less than 30% */public class Manager extends PriceHandler {public void processDiscount (float discount) {if (discount <= 0.3) {System. out. println ("approved by sales manager"); // System. out. format ("% s approved discount: %. 2f % n ", this. getClass (). getName (), discount);} else {successor. processDiscount (discount );}}}

4. Create a sales Vice President object:

/** Vice president of sales, which can approve discounts of less than 50% */public class Director extends PriceHandler {public void processDiscount (float discount) {if (discount <= 0.5) {System. out. println ("approved by vice president of sales"); // System. out. format ("% s approved discount: %. 2f % n ", this. getClass (). getName (), discount);} else {successor. processDiscount (discount );}}}

5. Create a sales director object:

/** Sales director, can approve discounts of less than 40% */public class VicePresident extends PriceHandler {public void processDiscount (float discount) {if (discount <= 0.4) {System. out. println ("approved by sales director"); // System. out. format ("% s approved discount: %. 2f % n ", this. getClass (). getName (), discount);} else {successor. processDiscount (discount );}}}

6. Create a CEO object:

/** President, can approve discounts of less than 55% */public class CEO extends PriceHandler {public void processDiscount (float discount) {if (discount <= 0.55) {System. out. println ("approved by the President"); // System. out. format ("% s approved discount: %. 2f % n ", this. getClass (). getName (), discount);} else {System. out. println ("the president refuses to approve"); // System. out. format ("% s rejected discount: %. 2f % n ", this. getClass (). getName (), discount );}}}

7. How to Create a PriceHandler Factory:

Public class PriceHandlerFactory {// create PriceHandler's factory method public static PriceHandler createPriceHandler () {PriceHandler sales = new Sales (); PriceHandler man = new Manager (); priceHandler dir = new ctor (); PriceHandler vp = new VicePresident (); PriceHandler ceo = new CEO (); sales. setSuccessor (man); man. setSuccessor (dir); dir. setSuccessor (vp); vp. setSuccessor (ceo); return sales ;}}

8. compile our test classes:

/** Customer, request Discount */public class Customer {private static PriceHandler priceHandler; public void setPriceHandler (PriceHandler priceHandler) {this. priceHandler = priceHandler;} public void requestDiscount (float discount) {priceHandler. processDiscount (discount);} public static void main (String [] args) {Customer customer = new Customer (); customer. setPriceHandler (PriceHandlerFactory. createPriceHandler (); Random random = new Random (); for (int I = 0; I <= 100; I ++) {System. out. print (I + ":"); customer. requestDiscount (random. nextFloat ());}}}

9. Code run test:

 

10. Add sales team leaders:

/** Sales team lead has 15% of the approval permissions */public class Group extends PriceHandler {public void processDiscount (float discount) {if (discount <0.15) {System. out. println ("approved by sales leader");} else {successor. processDiscount (discount );}}}

11. Add the team lead to the responsibility chain:

Public class PriceHandlerFactory {// create PriceHandler's factory method public static PriceHandler createPriceHandler () {PriceHandler sales = new Sales (); // create a sales leader object PriceHandler group = new Group (); priceHandler man = new Manager (); PriceHandler dir = new ctor (); PriceHandler vp = new VicePresident (); PriceHandler ceo = new CEO (); sales. setSuccessor (group); // Add the sales team lead to the responsible chain group. setSuccessor (man); man. setSuccessor (dir); dir. setSuccessor (vp); vp. setSuccessor (ceo); return sales ;}}

12. Code test:

  

The responsibility chain model has been introduced. Next article: Singleton mode and template method mode

Related Article

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.