Chain Of Responsibility Pattern Of my read Design Patterns

Source: Internet
Author: User
Tags class manager
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

/// Enable multiple objects to process requests, so as to avoid coupling between request senders and recipients.
/// Train this object into a chain and pass the request along this chain until an object processes it.
///
/// Example:
/// The employee sends the application documents directly to his/her supervisor;
/// As to who has the permission to audit, the sender does not have to worry about it.
///

Namespace ChainOfResponsibilityPattern
{
Class Program
{
Static void Main (string [] args)
{
Manager commonManager = new CommonManager ("Spike ");
Manager majorManager = new MajorManager ("kenny ");
Manager generalManager = new GeneralManager ("John ");

CommonManager. SetSuperior (majorManager );
MajorManager. SetSuperior (generalManager );

Request request = new Request ();
Request. RequestType = "leave ";
CommonManager. RequestApplications (request );

Request = new Request ();
Request. RequestType = "salary increase ";
CommonManager. RequestApplications (request );

Request = new Request ();
Request. RequestType = "resign ";
CommonManager. RequestApplications (request );

Console. Read ();
}
}

Public abstract class Manager
{
Public string name;

// Superior Manager
Protected Manager superior;

Public Manager (string name)
{
This. name = name;
}

// Set the manager superior
Public void SetSuperior (Manager superior)
{
This. superior = superior;
}

// Request
Public abstract void RequestApplications (Request request );
}

// Common Manager
Public class CommonManager: Manager
{
Public CommonManager (string name): base (name ){}

Public override void RequestApplications (Request request)
{
If (request. RequestType = "leave ")
{
Console. WriteLine ("your application form has been approved by Common Manager [" + name + ");
}
Else
{
If (superior! = Null)
{
Superior. RequestApplications (request );
}
Else
{
Console. WriteLine ("no one has permission to approve [" + request. RequestType + ");
}
}
}
}

// Major Manager
Public class MajorManager: Manager
{
Public MajorManager (string name): base (name ){}

Public override void RequestApplications (Request request)
{
If (request. RequestType = "Please take a long vacation ")
{
Console. WriteLine ("your long-term application form has been approved by Major Manager [" + name + ");
}
Else
{
If (superior! = Null)
{
Superior. RequestApplications (request );
}
Else
{
Console. WriteLine ("no one has permission to approve [" + request. RequestType + ");
}
}
}
}

// General Manager
Public class GeneralManager: Manager
{
Public GeneralManager (string name): base (name ){}

Public override void RequestApplications (Request request)
{
If (request. RequestType = "raise salary ")
{
Console. WriteLine ("your salary increase application has been approved by general Manager [" + name + ");
}
Else
{
If (superior! = Null)
{
Superior. RequestApplications (request );
}
Else
{
Console. WriteLine ("no one has permission to approve [" + request. RequestType + ");
}
}
}
}

// Apply
Public class Request
{
// Application Type
Public string RequestType {set; get ;}

// Application content
Public string RequestContent {set; get ;}

// Number
Public string Number {set; get ;}
}
}

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.