Java design pattern "one"

Source: Internet
Author: User

Design patterns are a set of reusable, most known, categorized purposes, code design experience Summary. Design patterns are used in order to reuse code, make code easier for others to understand, and ensure code reliability. There is no doubt that design patterns in others in the system are multi-win; Design patterns make code production truly engineering; Design Patterns are the cornerstone of software engineering , like the structure of a building .

This article will introduce you to the first of the design patterns in Java: The responsibility chain model.

What is the responsibility chain model? For example: As the current price continues to rise, we this group of young, after countless years to fight, finally saved enough money to buy a house, so you happily came to the sale of the house, ready to buy a set of 3 bedroom, this is a sales miss walked by asked: Sir do you need to buy a house? You were happy to say yes to her. After a carefully selected, you fancy a set, this is you to the sales Miss said, can you give me a 5% discount? I'll give you all the money. This is the sales miss a 5% discount in my decision right, so she was happy to say: "Good sir, then you pay it." The next day another person who buys a house, what about this person? It's a diamond bachelor, he said to the sales miss: Give me 10% discount i will buy 3 sets. Sales Miss a think, 10% discount beyond my jurisdiction, so he said to Bachelor: Sir Please wait a moment, I ask our manager. Then the sales miss to find the manager, the situation is reported to the manager, manager a think, 10% of the authority I have, so the sales Miss said can. Then the sales lady will come back to Bachelor said: "Sir, our manager has agreed to give you a 10% discount, you come here to pay the money." Does everyone here have a certain understanding of the chain of responsibility? Let's use the code to implement this process in detail.

1. Create processing Discount Handler object:

/*** @ Price Processing person, responsible for handling customer discount request*/ Public Abstract classPricehandler {//Direct successor for delivery of requests    protectedPricehandler successor;  Public voidSetsuccessor (Pricehandler successor) { This. successor =successor; }        //Process Discount Requests     Public Abstract voidProcessdiscount (floatdiscount);}

2. Create a sales class object:

/** Sales, can be approved within 5% discount*/ Public classSalesextendsPricehandler { Public voidProcessdiscount (floatdiscount) {        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 the discount within 30%*/ Public classManagerextendsPricehandler { Public voidProcessdiscount (floatdiscount) {        if(discount<=0.3) {System.out.println ("Sales Manager Approval"); //System.out.format ("%s Approved discount:%.2f%n", This.getclass (). GetName (), discount);}Else{successor.processdiscount (discount); }    }}

4. Create a vice president of sales object:

/** Vice President of sales, can approve the discount within 50%*/ Public classDirectorextendsPricehandler { Public voidProcessdiscount (floatdiscount) {        if(discount<=0.5) {System.out.println ("Vice president of Sales Approval"); //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 the discount within 40%*/ Public classVicePresidentextendsPricehandler { Public voidProcessdiscount (floatdiscount) {        if(discount<=0.4) {System.out.println ("Approval 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 the discount within 55%*/ Public classCeoextendsPricehandler { Public voidProcessdiscount (floatdiscount) {        if(discount<=0.55) {System.out.println ("President Approval"); //System.out.format ("%s Approved discount:%.2f%n", This.getclass (). GetName (), discount);}Else{System.out.println ("The president refused to approve"); //System.out.format ("%s Rejected discount:%.2f%n", This.getclass (). GetName (), discount);        }    }}

7, the factory method of creating Pricehandler:

 Public classPricehandlerfactory {//to create a factory method for Pricehandler     Public StaticPricehandler Createpricehandler () {Pricehandler sales=NewSales (); Pricehandler Mans=NewManager (); Pricehandler dir=NewDirector (); Pricehandler VP=Newvicepresident (); Pricehandler CEO=NewCEO ();        Sales.setsuccessor (man);        Man.setsuccessor (dir);        Dir.setsuccessor (VP);                Vp.setsuccessor (CEO); returnsales; }}

8. Write our test class:

/** Customer, Request discount*/ Public classCustomer {Private StaticPricehandler Pricehandler;  Public voidSetpricehandler (Pricehandler pricehandler) { This. Pricehandler =Pricehandler; }         Public voidRequestdiscount (floatdiscount)    {Pricehandler.processdiscount (discount); }         Public Static voidMain (string[] args) {Customer customer=NewCustomer ();                Customer.setpricehandler (Pricehandlerfactory.createpricehandler ()); Random Random=NewRandom ();  for(inti = 0; i<=100; i++) {System.out.print (i+":");        Customer.requestdiscount (Random.nextfloat ()); }    }    }

9. Code Run Test:

 

10. Add sales Team Leader object:

/**/Publicclassextends  pricehandler {    public  void processdiscount (float  discount) {        if(Discount < 0.15 {            System.out.println ("Sales Team long Approval");        } Else {            successor.processdiscount (discount);     }}}

11. Add team leader to the chain of responsibility:

 Public classPricehandlerfactory {//to create a factory method for Pricehandler     Public StaticPricehandler Createpricehandler () {Pricehandler sales=NewSales (); //Create a sales team leader objectPricehandler Group =NewGroup (); Pricehandler Mans=NewManager (); Pricehandler dir=NewDirector (); Pricehandler VP=Newvicepresident (); Pricehandler CEO=NewCEO ();                Sales.setsuccessor (group); //Add sales team leader to the chain of responsibilitygroup.setsuccessor (man);        Man.setsuccessor (dir);        Dir.setsuccessor (VP);                Vp.setsuccessor (CEO); returnsales; }}

12. Code Test:

  

For the chain of responsibility model, we will introduce the completion. Next: Single-case mode and template method mode

Java design pattern "one"

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.