Design Patterns as seen (Chain of Responsibility Chain of Responsibility pattern)

2010-03-29  来源:本站原创  分类:Java  人气:310 

Design Patterns writes

Object-oriented developers often want to state clearly and reduce the responsibility between objects, thereby reducing the coupling between the object program. So that our systems are more easily modified, but also can reduce the risk of defects. To some extent, java language itself can help reduce the coupling between objects. The client can only access the object's interface, rather than the relationship between their specific implementation details. With interfaces in this way, the client needs to know only the methods of feature. If we follow a hierarchical structure of organizations, such as the organization chain of responsibility for the class, client-side code, there may not need to know in advance what he would use a class. In this case, the chain, each object has a method, when the client-side code calls the method, these objects are either the implementation of the method, either along the chain forwards the method call request.

Chain of Responsibility pattern allows each object has an opportunity to decide whether to process the request in order to avoid the request of the sender and its receiver coupling between.

Example:

One time I went to the company about ticket reimbursement, co-workers just told me that the 15 floor, but the specific areas in which he was also unclear. So I got on the 15th floor, a door began to ask about 15 front, front-MM She said she also was not clear, you can ask the insurance next group, the insurance group is responsible for our insurance claims, and other fare reimbursement, he unclear. So he Jiugen we have to move forward to ask welfare groups, welfare groups to know told me that after the financial group should know that reimbursement location was behind me. So I walked over, handed down the forms filled out tickets left.

This thing looks simple is actually a Chain of Responsibility pattern, and each member of the group only responsible for their own areas of work. Outside the scope of work content for possible without the knowledge of, or only know a little bit. So when you find the insurance group, he saw the ticket was not in his scope of work hours. He will introduce you to his side of the Welfare Section. Here we will use the code to explain what they have it:

First, the definition of a request class, the request type is what I need to be reimbursed Tickets

public class Request {
        private String type;// Request type
        private boolean finished = false;// Do you have completed the task
        public Request(String type){
                this.type = type;
        }
        public String getType() {
                return type;
        }
        public void setType(String type) {
                this.type = type;
        }
        public boolean isFinished() {
                return finished;
        }
        public void setFinished(boolean finished) {
                this.finished = finished;
        }
}

At the same time the establishment of a processing class, this class is that each member of the group must implement the interface, which is his job content and working methods:

public interface Handle {
        public void handleRequest(Request request);
}

So we were to achieve well-being group, insurance group with the Finance Section

/**
 *  Welfare Group
 * @author Administrator
 *
 */
public class HandleBoon implements Handle{
        private Handle otherHandle;
        public void handleRequest(Request request) {
                System.out.println("............com to HandleBoon");
                if(request.getType().indexOf(" Welfare  ")>=0){
                        System.out.println("HandleBoon handle");
                        request.setFinished(true);
                }else{
                        System.out.println("............HandleBoon don't know,go to other");
                        if(otherHandle!=null)
                         otherHandle.handleRequest(request);
                }
        }
        public void setOtherHandle(Handle otherHandle) {
                this.otherHandle = otherHandle;
        }

}
/**
 *  Insurance medical claim group
 * @author Administrator
 *
 */
public class HandleInsure implements Handle{
        private Handle otherHandle;
        public void handleRequest(Request request) {
                System.out.println("............com to HandleInsure");
                if(request.getType().indexOf(" Ticket  ")>=0){
                        System.out.println("HandleInsure handle");
                        request.setFinished(true);
                }else{
                        System.out.println("............HandleInsure don't know,go to other");
                        if(otherHandle!=null)
                        otherHandle.handleRequest(request);
                }
        }
        public void setOtherHandle(Handle otherHandle) {
                this.otherHandle = otherHandle;
        }

}
/**
 *  Financial Group
 * @author Administrator
 *
 */
public class HandleFinance implements Handle{
        private Handle otherHandle;
        public void handleRequest(Request request) {
                System.out.println("............com to HandleFinance");
                if(request.getType().indexOf(" Financial  ")>=0){
                        System.out.println("HandleFinance handle");
                        request.setFinished(true);
                }else{
                        System.out.println("............HandleFinance don't know,go to other");
                        if(otherHandle!=null)
                        otherHandle.handleRequest(request);
                }
        }
        public void setOtherHandle(Handle otherHandle) {
                this.otherHandle = otherHandle;
        }

}

Implementation is complete we write a test to achieve my reimbursement process it:

public class Test {
        public static void main(String[] args) {
                HandleBoon hb = new HandleBoon();
                HandleFinance hf = new HandleFinance();
                HandleInsure hi = new HandleInsure();

                hb.setOtherHandle(hf);
                hf.setOtherHandle(hi);

                List<Handle> list  = new ArrayList<Handle>();
                list.add(hb);
                list.add(hf);
                list.add(hi);
                Request request =  new Request(" Ticket  ");
                for(Handle handle:list){
                        if(!request.isFinished()){
                                handle.handleRequest(request);
                        }else{
                                break;
                        }
                }
        }
}

First of all I know is 15 F, if the members of the group they did not know each other, then I have traveled 15 floors to find the financial group (assuming there was I do not know) to reimbursement of my invoice.

Summary:

When applied Chain of Responsibility pattern, the customers do not know in advance what the object in the collection can provide the services they need. When the client sends a request after the call, the request will be forwarded along the chain of responsibility, until you find an object to provide the service so far. This can greatly reduce the client-side code with the provision of services to the coupling between the object program.

相关文章
  • Java design patterns with beginner in mind - the factory method (Factory Pattern) Mode 2010-11-17

    Factory method (Factory Method) model, also known as virtual constructor mode or factory pattern polymorphism. 1. Intended to: define an interface for creating objects, let subclasses decide which class to instantiate. Factory Method lets a class def

  • Java design patterns with beginner in mind - the factory model (Factory Pattern) 2010-11-10

    Factory Mode (Factory Pattern) divided into the following several forms: (see Yan Wang "JAVA and Design Patterns") 1. Simple factory (Simple Factory) mode: also known as a static factory method model (Static Factory Method Pattern). 2. Factory m

  • Design patterns, today you used it? 2010-05-13

    Recently read a lot about design patterns of the East, should sum up the next, and Oh, a purely personal point of view, some larger questions, just talk about my views on it, if inappropriate, please tile light point. The growth of programmers to use

  • Preview article: Java design patterns with beginner in mind - the builder (BUILDER) mode 2010-11-30

    Builder (BUILDER) mode is also called generator (BUILDER) mode. It is defined as follows: (see GoF design patterns and Yan Wang's "Java and the Model") 1. Purpose: Construction of a complex object separated from its representation so that the sa

  • Design Patterns as seen (Chain of Responsibility Chain of Responsibility pattern) 2010-03-29

    Design Patterns writes Object-oriented developers often want to state clearly and reduce the responsibility between objects, thereby reducing the coupling between the object program. So that our systems are more easily modified, but also can reduce t

  • Responsibilities of design patterns study notes chain model (chain of responsibility) 2010-10-28

    Chain of Responsibility pattern: When a request is likely to be dealing with multiple objects, these objects will be connected into a chain, and pass the request along the chain until the request is processed. One took a fancy map, you must see Chain

  • Their understanding of the J2EE three-tier architecture (design patterns and software differences between the contact) 2009-06-18

    As Figure 3 layer 1.J2EE points: Server-side business logic (Business Logic Tier, and there is persistent data layers, Businness Tier and the EIS Tier), server-side presentation layer (Web Tier) and express the client layer (Client Tier) J2EE design

  • Their understanding of the J2EE three-tier architecture (design patterns and software differences between the links) 2009-06-21

    As Figure 3 layer 1.J2EE points: Server-side business logic (with Business Logic Tier and data layers of durable, Businness Tier and the EIS Tier), server-side presentation layer (Web Tier), and the client presentation layer (Client Tier) J2EE design

  • Analysis of Design Patterns 2009-09-20

    To reprint article, source http://www.javaeye.com/topic/372233?page=10 1, FACTORY-up MM and ultimately invited to dine, and McDonald's and Kentucky Fried Chicken wings are the wings MM favorite things, although the taste may be different, but no matt

  • Common design patterns java 2010-03-29

    Java design patterns in the 23 2009-04-13 21:31 1, the factory pattern: client classes and factory class separately. Consumers need a product at any time, simply a request to the factory can be. Consumers will be accepted without modification on the

  • Chasing MM with 23 kinds of Design Patterns Java 2010-01-04

    In the Java forum, I saw this article, the author relaxed java language analogy 23 kinds of patterns, there is a good stimulating, but unfortunately did not give a specific meaning, I was in the rear added. These are the most brief, to learn, then I

  • Design patterns Daquan 2010-03-21

    Bitter about the recent study of several open-source framework for source code, to be interested in design mode to see the contents of the following, all the design patterns (I think I should not be all, can only say that we normally come into contac

  • design patterns java study notes 2010-05-10

    Today take some time to learn the system under design mode, from the Internet to find a point of information and the learning experience of predecessors, 1: Learning goals and methods of design patterns (master's recommendation summary references) De

  • MM from the chase about 23 kinds of Design Patterns Java 2010-06-22

    MM from the recovery of 23 species on Java design patterns 1, FACTORY-chase MM missed, please eat, McDonald's and KFC's wings are chicken wings MM favorites, although tastes vary, but whether you go to McDonald's or with MM KFC, simply said to the wa

  • Utilize design patterns 2010-07-15

    The article discusses the following issues: 1, 2, design patterns metaphor, why use design patterns 3, 4, when to use design patterns to avoid excessive use of design patterns 5 to discuss a few specific models A design pattern is a martial arts meta

  • Design patterns and reusable object-oriented software 2010-07-23

    Seen before in the forum a good blog about design patterns. The most progress also strike while the iron is hot. Learning about design patterns reusable object-oriented software foundation. First give a brief summary of the definition of design patte

  • 23 kinds of design patterns interesting insights 2010-07-28

    23 kinds of interesting insights design patterns Posts by Li Yang editor at 2010-3-17 08:57 1, FACTORY-chase MM missed, please eat, McDonald's and KFC's wings are chicken wings MM favorites, although tastes vary, but whether you go to McDonald's or K

  • [Transferred] to recover the 23 MM and Java design patterns 2010-09-19

    Chase the 23 MM and Java design patterns Create schema 1, FACTORY-chase MM missed, please eat, McDonald's and KFC's wings are chicken wings MM favorites, although tastes vary, but whether you go to McDonald's or KFC with MM, simply said to the waiter

  • GoF Design Patterns Costume 2010-09-21

    Create schema 1, FACTORY-chase MM missed, please eat, McDonald's and KFC's wings are chicken wings MM favorites, although tastes vary, but whether you go to McDonald's or KFC with MM, simply said to the waiter "to 4 wings "on the line. McDonald'

  • Some common reasons for leading the redesign, and design patterns to solve these problems 2010-09-24

    Redesign will affect many aspects of software systems and is not Unexpected changes have always costly. Design patterns can ensure that the system can change in a specific way to help you avoid re-design system. Each design Mode allows the system to