Design patterns are the basis of object-oriented programming and are used to guide programming. In the actual project development process, it is not blindly to apply the design pattern, nor is it a large number of design patterns introduced in the function design. Appropriate design patterns should be applied according to the specific needs and requirements. Design pattern is an old topic, because recently in the design of the "Gateway API" component (follow-up), the "responsibility chain design mode" for the design, so first review records.
I. Introduction to the mode of responsibility chain
The chain of responsibility model refers to a process that involves multiple processes or roles involved in processing, and a chain based on a convention in which each process or role has its own responsibility to handle. The responsibility chain model effectively organizes a process processing, while the sub-process between the responsibilities clear. This mode application is common in the. NET platform. For example, the pipeline method can be implemented by the responsibility chain model.
Second, the responsibility chain model structure chart
As shown, this type of diagram is the core of the chain of responsibility structure diagram, the common definition of an interface of each sub-process based on this interface implementation, through the interface to decouple the process between the dependent.
Three, the responsibility chain pattern realization
The implementation of the responsibility chain is demonstrated by the following simple demo. Defines an interface (Ihandler), three implementation classes (Ahandler, Bhandler, CHandler), and a context class (Pipelcontext).
Public Interface ihandler{ pipelcontext DoAction (Pipelcontext pipelcontext);}
Public classAhandler:ihandler {PrivateIhandler Nexthandler {Get;Set; } PublicAhandler (Ihandler nexthandler) {Nexthandler=Nexthandler; } Publicpipelcontext DoAction (Pipelcontext pipelcontext) {BOOLIsnext = (Pipelcontext.flag < -) ?true:false; Pipelcontext.request="AA"; Console.WriteLine ("my is"+pipelcontext.request); if(Nexthandler! =NULL&&Isnext) {nexthandler.doaction (Pipelcontext); } returnPipelcontext; } } Public classBhandler:ihandler {PrivateIhandler Nexthandler {Get;Set; } PublicBhandler (Ihandler nexthandler) {Nexthandler=Nexthandler; } Publicpipelcontext DoAction (Pipelcontext pipelcontext) {BOOLIsnext = (Pipelcontext.flag <Ten) ?true:false; Pipelcontext.request="BB"; Console.WriteLine ("my is"+pipelcontext.request); if(Nexthandler! =NULL&&Isnext) {nexthandler.doaction (Pipelcontext); } returnPipelcontext; } } Public classChandler:ihandler {PrivateIhandler Nexthandler {Get;Set; } PublicCHandler (Ihandler nexthandler) {Nexthandler=Nexthandler; } Publicpipelcontext DoAction (Pipelcontext pipelcontext) {BOOLIsnext = (Pipelcontext.flag <5) ?true:false; Pipelcontext.request="CC"; Console.WriteLine ("my is"+pipelcontext.request); if(Nexthandler! =NULL&&Isnext) {nexthandler.doaction (Pipelcontext); } returnPipelcontext; } }
Public classPipelcontext { PublicPipelcontext () {Key=Guid.NewGuid (); } PublicGuid Key {Get;Set; } Public intFlag {Get;Set; } Public stringRequest {Get;Set; } Public stringRespone {Get;Set; } }
Static voidMain (string[] args) {Ihandler Modulec=NewCHandler (NULL); Ihandler Moduleb=NewBhandler (Modulec); Ihandler Modulea=NewAhandler (Moduleb); Pipelcontext P=NewPipelcontext (); while(true) {P.flag=Convert.ToInt32 (Console.ReadLine ()); Modulea.doaction (P); } }
Iv. Summary
The above is just a very simple chain of responsibility model, the actual application according to demand, sub-process can be configured to achieve hot plug-in mode, improve flexibility, expansion and maintainability.
Author: Liu Zaitao
Source: Http://www.cnblogs.com/Andon_liu
About the focus on Microsoft Platform Project architecture, management. Familiar with design patterns, domain drivers, architecture design, agile development and project management. is mainly engaged in ASP, Wcf/web API, SOA, MSSQL, Redis aspects of project development, architecture, management work. If you have questions or suggestions, please learn to discuss together!
This article is copyrighted by the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link.
If you have questions, you can email: [Email protected] contact me, thank you.
C # Design pattern: Responsibility chain mode