Request chain processing-responsibility chain model (3)

Source: Internet
Author: User
Tags class manager
16.3 complete solution

To make the approval process of a purchase order more flexible and implement chain transfer and processing of the purchase order, sunny developers use the responsibility chain model to implement hierarchical approval of the purchase order. The basic structure is 16-3:

In Figure 16-3, the abstract class approver acts as the abstract processor (Abstract TRANSMITTER), Director, VicePresident, President, and Congress acts as the specific handler (Specific TRANSMITTER), and purchaserequest acts as the request class. The complete code is as follows:
// Purchase order: Request class purchaserequest {private double amount; // purchase amount private int number; // purchase order No. Private string purpose; // public purchaserequest (double amount, int number, string purpose) {This. amount = amount; this. number = number; this. purpose = purpose;} public void setamount (double amount) {This. amount = amount;} public double getamount () {return this. amount;} public void setnumber (INT number) {This. num BER = number;} public int getnumber () {return this. number;} public void setpurpose (string purpose) {This. purpose = purpose;} Public String getpurpose () {return this. purpose; }}// approver class: Abstract handler abstract class approver {protected approver successor; // defines the successor object protected string name; // approver name public approver (string name) {This. name = Name;} // set the successor public void setsuccessor (approver successor) {This. successor = succ Essor;} // abstract request processing method public abstract void processrequest (purchaserequest request);} // director class: Specific handler class director extends approver {public director (string name) {super (name) ;}// specific request processing method public void processrequest (purchaserequest request) {If (request. getamount () <50000) {system. out. println ("" + this. name + "Approval purchase order:" + request. getnumber () + ", amount:" + request. getamount () + "RMB, procurement purpose:" + request. getpurpos E () + ". "); // Process the request} else {This. successor. processrequest (request); // forward request }}// Vice Chairman class: The processor class VicePresident extends approver {public VicePresident (string name) {super (name );} // specific request processing method public void processrequest (purchaserequest request) {If (request. getamount () <100000) {system. out. println ("Vice Chairman" + this. name + "Approval purchase order:" + request. getnumber () + ", amount:" + request. getamount () + "RMB, procurement purpose:" + request. getpu Rpose () + ". "); // Process the request} else {This. successor. processrequest (request); // forward request }}// Chairman class: The processor class president extends approver {public President (string name) {super (name );} // specific request processing method public void processrequest (purchaserequest request) {If (request. getamount () <500000) {system. out. println ("Chairman" + this. name + "Approval purchase order:" + request. getnumber () + ", amount:" + request. getamount () + "RMB, procurement purpose:" + request. getpurpose () + ". "); // Process the request} else {This. successor. processrequest (request); // forward request }}// board class: The processor class Congress extends approver {public Congress (string name) {super (name );} // specific request processing method public void processrequest (purchaserequest request) {system. out. println ("convene the board of directors to approve the purchase order:" + request. getnumber () + ", amount:" + request. getamount () + "RMB, procurement purpose:" + request. getpurpose () + ". "); // Process the request }}

Write the following client test code:

Class client {public static void main (string [] ARGs) {approver wjzhang, gyang, jguo, meeting; wjzhang = new director ("Zhang Wuji "); gyang = new vicepresident ("Yang Guo"); jguo = new president ("Guo Jing"); meeting = new Congress ("board of directors"); // create a responsibility chain wjzhang. setsuccessor (gyang); gyang. setsuccessor (jguo); jguo. setsuccessor (meeting); // create a purchase order: purchaserequest PR1 = new purchaserequest (45000,10001, ""); wjzhang. processrequest (PR1); purchaserequest Pr2 = new purchaserequest (60000,10002, ""); wjzhang. processrequest (Pr2); purchaserequest Pr3 = new purchaserequest (16.0,10003, "Buy Diamond Sutra"); wjzhang. processrequest (Pr3); purchaserequest PR4 = new purchaserequest (800000,10004, "Buy Peach Blossom Island"); wjzhang. processrequest (PR4 );}}
Compile and run the program. The output result is as follows:

Director Zhang Wuji approves the purchase order: 10001; amount: 45000.0 yuan; purpose: to purchase Yi tianjian.

Vice Chairman Yang once approved the purchase order: 10002, amount: 60000.0 yuan. purpose: to purchase the "sunflower Collection".

Guo Jing, Chairman of the Board, approves a purchase order of 10003 Yuan and the purchase amount of 160000.0 yuan. purpose: to purchase the Diamond Sutra.

Hold the board of directors to approve the purchase order: 10004; amount: 800000.0 yuan; purpose: to purchase Peach Blossom Island.

If you need to add a new processor in the system, if you want to add a manager role, you can approve a purchase order from RMB 50 thousand to RMB 80 thousand (excluding RMB 80 thousand, you need to write a new handler class manager, which is a subclass of the abstract handler class approver to implement the abstract processing method defined in the approver class. If the purchase amount is greater than or equal to 80 thousand yuan, forward the request to the next house. The Code is as follows:

// Manager class: The specific handler class manager extends approver {Public Manager (string name) {super (name);} // the specific request processing method public void processrequest (purchaserequest request) {If (request. getamount () <80000) {system. out. println ("manager" + this. name + "Approval purchase order:" + request. getnumber () + ", amount:" + request. getamount () + "RMB, procurement purpose:" + request. getpurpose () + ". "); // Process the request} else {This. Successor. processrequest (request); // forward the request }}}

Because the creation process of the chain is the responsibility of the client, adding a new specific handler class has no impact on the original class library and does not need to modify the source code of the existing class to comply with the "Open and Close principle ".

In the client code, if you want to apply a new specific request handler to the system, you need to create a new specific handler object and then add the object to the responsibility chain. For example, add the following code to the client test code:

Approver rhuang; rhuang = new manager ("Huang Rong ");

Change the chain building code:

// Create the responsibility chain wjzhang. setsuccessor (rhuang); // use "Huang Rong" as the next rhuang of "Zhang Wuji. setsuccessor (gyang); // use Yang Guo as the next gyang of "Huang Rong. setsuccessor (jguo); jguo. setsuccessor (meeting );

Recompile and run the program. The output result is as follows:

Director Zhang Wuji approves the purchase order: 10001; amount: 45000.0 yuan; purpose: to purchase Yi tianjian.

Manager Huang Rong approves the purchase order: 10002, and the amount: 60000.0 yuan. purpose: to purchase the sunflower collection.

Guo Jing, Chairman of the Board, approves a purchase order of 10003 Yuan and the purchase amount of 160000.0 yuan. purpose: to purchase the Diamond Sutra.

Hold the board of directors to approve the purchase order: 10004; amount: 800000.0 yuan; purpose: to purchase Peach Blossom Island.

 

 

Thoughts

If the approval process is changed from "director --> vice chairman --> Chairman --> board of directors" to "director --> Chairman --> board of directors", what changes will the system make? Predict the output result of the client code after modification.

 

【Author】 Liu WeiHttp://blog.csdn.net/lovelion]

 

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.