标签:sys bsp print 引用 各类 根据 直接 系统 流程
责任链模式实现为请求创建了一个接收者对象的链。当请求的直接执行者无法实现请求时,会将请求传递给链的上一级进行处理。这种传递往往可以通过让接收者包含另一个接收者的引用的方式实现。
责任链模式是一种对象行为型模式,其主要优点如下。
其主要缺点如下。
UML:
举例:当在公司中请假,公司规定根据请假的天数和事由调整批复对象,首先是项目经理Project Manager, 不能批复找Department Manager, 再不能上升到总经理President。
C++实现:
1 #include <iostream> 2 3 using namespace std; 4 5 class Request{ 6 public: 7 int level; 8 }; 9 10 class Manager{ 11 public: 12 Manager(string _name){name = _name;} 13 void setSuccessor(Manager* manager){nextLevel = manager;} 14 virtual void GetRequest(Request* request)=0; 15 protected: 16 Manager* nextLevel; 17 string name; 18 }; 19 20 class ProjectManager:public Manager{ 21 public: 22 ProjectManager(string _name):Manager(_name){} 23 virtual void GetRequest(Request *request){ 24 if (request->level <= 1){ 25 cout << name << " handle it" << endl; 26 } else { 27 nextLevel->GetRequest(request); 28 } 29 } 30 }; 31 32 class DepartmentManager:public Manager{ 33 public: 34 DepartmentManager(string _name):Manager(_name){} 35 virtual void GetRequest(Request *request){ 36 if (request->level <= 2){ 37 cout << name << " handle it" << endl; 38 } else { 39 nextLevel->GetRequest(request); 40 } 41 } 42 }; 43 44 class President:public Manager{ 45 public: 46 President(string _name):Manager(_name){} 47 virtual void GetRequest(Request *request){ 48 cout << name << " handle it" << endl; 49 } 50 }; 51 52 int main() 53 { 54 Request *request = new Request(); 55 request->level = 3; 56 Manager *projectManager = new ProjectManager("Project Manager: Alice"); 57 Manager *departmentManager = new DepartmentManager("Department manager: Helen"); 58 Manager *president = new President("President: Chalice"); 59 projectManager->setSuccessor(departmentManager); 60 departmentManager->setSuccessor(president); 61 projectManager->GetRequest(request); 62 return 0; 63 }
Java:
1 public class Request { 2 int level; 3 } 4 5 public abstract class Manager { 6 7 private Manager next; 8 9 public void setNext(Manager next){ 10 this.next = next; 11 } 12 13 public Manager getNext(){ 14 return next; 15 } 16 17 public abstract void handleRequest(Request request); 18 } 19 20 public class ProjectManager extends Manager { 21 22 @Override 23 public void handleRequest(Request request) { 24 if (request.level <= 1){ 25 System.out.println("Project Manager handle request"); 26 } else { 27 this.getNext().handleRequest(request); 28 } 29 } 30 } 31 32 public class DepartmentManager extends Manager { 33 34 @Override 35 public void handleRequest(Request request) { 36 if (request.level <= 2){ 37 System.out.println("Department manager handle request"); 38 } else { 39 this.getNext().handleRequest(request); 40 } 41 } 42 } 43 44 public class President extends Manager { 45 46 @Override 47 public void handleRequest(Request request) { 48 System.out.println("President handle request"); 49 } 50 } 51 52 public class Main { 53 54 public static void main(String[] args) { 55 Manager projectManager = new ProjectManager(); 56 Manager departmentManager = new DepartmentManager(); 57 Manager president = new President(); 58 projectManager.setNext(departmentManager); 59 departmentManager.setNext(president); 60 Request request = new Request(); 61 request.level = 3; 62 projectManager.handleRequest(request); 63 } 64 }
两种语言实现设计模式(C++和Java)(十四:责任链模式)
标签:sys bsp print 引用 各类 根据 直接 系统 流程
原文地址:https://www.cnblogs.com/Asp1rant/p/11278606.html