Detailed explanation of the application of chain of responsibility pattern in C++ design pattern programming

高洛峰
Release: 2017-01-19 17:01:36
Original
1389 people have browsed it

Chain of Responsibility Pattern: Allows multiple objects to have the opportunity to process requests, thereby avoiding the coupling relationship between the sender and receiver of the request. These objects are connected into a chain and the request is passed along the chain until an object handles it.

The idea is very simple, for example, consider employees’ requests for a salary increase. There are three levels of managers in the company, general manager, director, and manager. If an employee requires a salary increase, he should apply to the manager in charge. If the amount of salary increase is within the manager's authority, the manager can directly approve it, otherwise the application will be made. Submit to the Director. The same goes for the Director, the General Manager can handle all requests. This is a typical chain of responsibility model. The processing of requests forms a chain until an object handles the request. A UML diagram is given for this example.

UML structure diagram:

Detailed explanation of the application of chain of responsibility pattern in C++ design pattern programming

##An example of the chain of responsibility model

*This example is the situation of three types of salespersons processing orders
*If the order If the amount is less than 1,000, the first-level salesperson can process the order
*If the order amount is less than 10,000, the second-level salesperson can process the order
*If the order amount is less than 100,000, the third-level salesperson can process the order

using System; 
  
/**//// <summary> 
///售货员接口,所有类型的售货员必须实现该接口 
/// </summary> 
interface ISalesMan 
{ 
  string Name {set;get;} //售货员名字 
  void SetNext(ISalesMan nextSalesMan); //设置下一级售货员 
  void Process(Order order); //处理订单 
} 
  
/**//// <summary> 
/// 订单类 
/// </summary> 
class Order 
{ 
  private int orderAmount; 
  
  public int Amount 
  { 
    set{ this.orderAmount = value;} 
    get{ return this.orderAmount; } 
  } 
} 
  
/**//// <summary> 
/// 一类售货员 
/// </summary> 
class FirstSalesMan : ISalesMan 
{ 
  private ISalesMan nextSalesMan = null; 
  private string name = string.Empty; 
  
  ISalesMan 成员ISalesMan 成员 
} 
  
/**//// <summary> 
/// 二类售货员 
/// </summary> 
class SecondSalesMan : ISalesMan 
{ 
  private ISalesMan nextSalesMan = null; 
  private string name = string.Empty; 
  
  ISalesMan 成员ISalesMan 成员 
} 
  
/**//// <summary> 
/// 三类售货员 
/// </summary> 
class ThirdSalesMan : ISalesMan 
{ 
  private ISalesMan nextSalesMan = null; 
  private string name = string.Empty; 
  
  ISalesMan 成员ISalesMan 成员 
} 
  
class Client 
{ 
  public static void Main(string[] args) 
  { 
    FirstSalesMan first = new FirstSalesMan(); 
    first.Name = "firstMan"; 
  
    SecondSalesMan second = new SecondSalesMan(); 
    second.Name = "secondMan"; 
  
    ThirdSalesMan third = new ThirdSalesMan(); 
    third.Name = "thirdMan"; 
  
    first.SetNext(second); 
    second.SetNext(third); 
  
    Order o = new Order(); 
    o.Amount = 300; 
    first.Process(o); 
  
    o = new Order(); 
    o.Amount = 1300; 
    first.Process(o); 
  
    o = new Order(); 
    o.Amount = 11300; 
    first.Process(o); 
  
    Console.Read(); 
  } 
}
Copy after login

Applicable scenarios of chain of responsibility model


There are multiple objects that can handle a request, and which object handles the request is automatically determined at runtime.

You want to submit a request to one of multiple objects without explicitly specifying the recipient.

The collection of objects that can handle a request should be dynamically specified.

For more detailed articles on the application of the chain of responsibility pattern in C++ design pattern programming, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template