Responsibility chain mode of computer room cooperation

Source: Internet
Author: User

After a fun and fun soft test preparation and exams, after all, we have to be calm. We have to learn a lot of things, so in the soft test the end of the next day, the computer room cooperation immediately put on the agenda. At the beginning, it was to do the press release system while thinking about cooperation, and then, The gradual transition wholeheartedly into the computer room cooperation project to go to the stage, because I am the project leader, I have to do a lot of work, from the beginning of modeling to document writing, and then to knock code, are involved, today we discuss how to use the cost calculation of the computer room fee system using the responsibility chain model.

First we have to give a class diagram of the solution

The responsibility chain model is simply to link all the processing objects through the Setsuccessor () function to form a processing chain, and when we ask for a problem, the request is passed along the chain of responsibility, knowing that it is being processed. This is the workflow of the responsibility chain pattern.

I give the computer room charge system on the next machine when the consumption time, how to calculate the cost of the logical map


For this design, give a simple demo using C # as follows:

<span style= "FONT-SIZE:18PX;"
>using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Using System.Threading.Tasks; Namespace Test {class Program {static void Main (string[] args) {ConcreteHandler1 h
            andler1= New ConcreteHandler1 ("Processing object 1");
            ConcreteHandler2 handler2= New ConcreteHandler2 ("Processing object 2");
            ConcreteHandler3 handler3= New ConcreteHandler3 ("Processing Object 3"); Handler1.
            Setsuperior (Handler2); Handler2.

            Setsuperior (HANDLER3);
            Request Request = new request (); Request.
            Consumetime = 4; Handler1.

            Requestapplications (Request);
            Request Request1 = new request (); Request1.
            Consumetime = 20; Handler1.

            Requestapplications (REQUEST1);
            Request Request2 = new request (); Request2.
            Consumetime = 50; Handler1.

            Requestapplications (REQUEST2);
        Console.read ();
   } //The object to be processed, that is, the Internet time class Request {//Customer consumption time private int consumetime;
            public int Consumetime {get {return consumetime;}
        set {consumetime = value;}
        //Customer consumption amount private double consumecash;
            Public double Consumecash {get {return consumecash;}
        set {Consumecash = value;}
        }//Processor abstract class Handler {protected string name;
        protected Handler Superior;
        Public Handler (string name) {this.name = name;
        public void Setsuperior (Handler superior) {this.superior = superior;
    Abstract public void Requestapplications (Request request); //Process Class 1 classes Concretehandler1:handler {public ConcreteHandler1 (string name): Base (name ) {} public override void Requestapplications (RequestRequest) {if (request). Consumetime <=5) {request.
                Consumecash = 0.0; Console.WriteLine ("The customer's consumption amount is {0} yuan, salesman: {1}", request.
            Consumecash, name); else {if (Superior!= null) superior.
            Requestapplications (Request);
            {2 class Concretehandler2:handler {public ConcreteHandler2 (string name)}}// : base (name) {} public override void Requestapplications (Request request) {if ( Request. Consumetime >5 &&request. Consumetime <=30) {request.
                Consumecash = 3.0 * (30.0/60.0); Console.WriteLine ("The customer's consumption amount is {0} yuan, salesman: {1}", request.
            Consumecash,name); else {if (Superior!= null) superior.
            Requestapplications (Request);
   }     {public ConcreteHandler3 (string name)//Process Class 3 classes Concretehandler3:handler : base (name) {} public override void Requestapplications (Request request) {if (Reque St. Consumetime >30) {request. Consumecash = 3.0 * (double) request. 
                consumetime/60.0); Console.WriteLine ("The customer's consumption amount is {0} yuan, salesman: {1}", request.
            Consumecash,name); }}}}</span>

The results of the operation are as follows

Some people may feel that the chain of responsibilities is a little superfluous to use here, this is not unreasonable, we in the computer room toll system as far as possible to join the design model, the purpose is to learn and design patterns to practice, not to give the perfect solution to the problem, so the use of the combination is not appropriate also does not matter, the important thing is the process of learning.

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.