Object-oriented programming analysis of 23 B chain of responsibility design pattern mode glory at an angle king

·

Application Chain of Responsibility pattern in the glory of the King

·

Here Insert Picture Description

A brief

In the glory of the King Mall, players can participate in the Indiana lottery. Indiana draw two types, one is the integral lottery, lottery another diamond; in ordinary two Indiana lottery mode can be 60 by Diamond / integral lottery once, or by diamond 270 / point five consecutive draw; wherein when the value reaches 201 diamonds Indiana lucky, you can get rare crystal king, when the integral value reaches 361 Indiana lucky, you can get the rare honor of crystal; the player can use the king of crystal or crystal glory in exchange for a particular hero, skin, and equipment Wait.
In many cases, a request can be processed more than one. Many applications can handle objects, and they constitute a chain, passing the request along the chain, this chain is called a chain of responsibility.
For the above description, the king of glory in Indiana is an example of the application of the chain of responsibility pattern in the glory of the King. In this problem, the player by clicking on the 60 diamond / diamond integral pumping once or 270 / integration lottery draw five times to achieve results.

Specifically, we first achieved by an interface receiving a request submitted by a user, and then implement the requested operation by way of example three specific processing class; wherein, by class "Diamond 60 / integral pumping a" prize achieve 14 kinds at random to give a prize, like "270 diamond / five points pumping" achieved in 14 kinds of prizes in five randomized to receive prizes, "Fortunately, the value reaches a certain value after" class that implements the rewards of rare crystals.

Second, the chain of responsibility pattern

Chain of Responsibility pattern appreciated:
highly summarized: a plurality of object a chance to handle the request, in order to avoid the coupling between the sender and recipient of the request, these objects together into a chain, and pass the request along the chain until an object handles it so far.
In the design of Java mode, you may need a lot of design objects to meet the user's request.
Chain of Responsibility pattern using a plurality of maturity model object processing user requests, the key chain of responsibility pattern is assigned to a user request to many objects, which are organized into a chain of responsibility, i.e., each object containing a reference to a subsequent object, and requires each object in the chain of responsibility, if they can handle the user's request, to make the processing, the user no longer passes the request to the next object in the chain of responsibility, if the user's request can not be processed, it is necessary to pass the request of the user to the next object on the chain of responsibility.

Chain of Responsibility pattern structure in two roles:
handler : is an interface, the user is responsible for requesting a predetermined method and a method of setting a specific processing by subsequent processing by a specific processing object;
❷ The specific process by : a specific processing is implemented by handlers examples of the class interface. The method defined by a specific process by invoking the interface handler, the user request processing, i.e., after receiving the user's request, the interface handler calls a predetermined method, during the execution of the method, if found to handle the user's request , on the handling of related data; otherwise it can not process information feedback to the user, and then pass the user's request to his successor object.

Chain of Responsibility pattern UML class diagram:

Here Insert Picture Description

Chain of Responsibility pattern of the advantages and disadvantages:
advantages:
① only objects in the chain of responsibility and his successor is unrelated low coupling relations and other objects;
② When allocating responsibilities in dealing with those in the chain of responsibility to the application more flexible sex;
③ application can dynamically add, delete or reassign duties handlers handlers;
④ application can dynamically change the order between the handler;
⑤ users do not have to know the chain of responsibility of information processing by the user will not know in the end is the object which deal with his request;
shortcomings:
① no guarantee that the request must be received: the recipient is not clear, can not be processed to ensure certain;
② chain of responsibility for too long, affecting system performance. Improper chain may build an infinite loop.

Chain of Responsibility pattern of applicable scenarios:
① there are many objects can handle the user's request, hoping that object processing program automatically determines the user during operation;
② hope that users do not have to explicitly specify the recipient of the case, submitted to more than one recipient request;
③ program developed by a dynamic object can process user requests a set.

Third, the Chain of Responsibility pattern configuration diagram of the code and the king of glory achieved angle

Chain of Responsibility pattern achieve this UML class diagram

Here Insert Picture Description

FIG structure eclipse

Here Insert Picture Description
[Main function application (Application)]
Application.java

package angle_responsibility;

/*
          测试应用类 
*/

import angle_responsibility.Application;
import angle_responsibility.Diamond60DrawAPrize;
import angle_responsibility.Handler;
import angle_responsibility.Diamond270DrawFivePrizes;
import angle_responsibility.RareCrystalOfKings;

public class Application {
	    private Handler diamond60,diamond270,rareDiamond;    //责任链上的对象
	    public void createChain(){       //建立责任链
	    	diamond60=new Diamond60DrawAPrize();
	    	diamond270=new Diamond270DrawFivePrizes();
	    	rareDiamond=new RareCrystalOfKings();
	    	diamond60.setNextHandler(diamond270);
	    	diamond270.setNextHandler(rareDiamond);
	    }
	    public void reponseClient(int number){  //响应用户的请求
	    	diamond60.handleRequest(number);
	    }
	    public static void main(String args[]){
	       Application  application=new  Application();
	       application.createChain();
	       System.out.println("当点击“60钻石”抽一次时:");
	       System.out.print("[购买成功]");
	       application.reponseClient(60);
	       System.out.println("---------------------------");
	       System.out.println("当点击“270钻石”抽五次时:");
	       System.out.print("[购买成功]");
	       application.reponseClient(270);
	       System.out.println("---------------------------");
	       System.out.println("当钻石抽奖“幸运值达201时”:");
	       System.out.print("[购买成功]");
	       application.reponseClient(201);
	    }

	}

Handler (Handler)
Handler.java

package angle_responsibility;

/*
            角色1:处理者 :是一个接口,负责规定具体处理者处理用户请求的方法以及具体处理者设置后继对象的方法
*/

import angle_responsibility.Handler;

public interface Handler {
	   public abstract void handleRequest(int number);   //具体处理用户请求60钻石抽一次还是270钻石抽五次
	   public abstract void setNextHandler(Handler handler);  

}

DETAILED handler (ConcreteHandler)
Diamond60DrawAPrize .java

package angle_responsibility;

/*
              角色2.1:具体处理者 :具体处理者是实现处理者接口的类的实例
                              具体处理者通过调用处理者接口规定的方法,处理用户的请求
                              即在接到用户的请求后,处理者将调用接口规定的方法,在执行该方法的过程中
                              如果发现能处理用户的请求,就处理有关数据
                              否则就反馈无法处理的信息给用户,然后将用户的请求传递给自己的后继对象
*/

import angle_responsibility.Handler;

public class Diamond60DrawAPrize implements Handler{
	 private Handler handler;      //存放当前处理者后继的Hander接口变量
     public void handleRequest(int number){
    	if(number==60){              //60钻石抽一次
    		String random = "";
            String[] doc = {"白起", "夏侯惇", "甄姬", "金币288","小喇叭5","铭文碎片1600", "铭文碎片400","铭文碎片100", "铭文碎片25", "爱心气球(3日)", "亲密玫瑰","钻石48","龙域领主体验卡"};
            int index = (int) (Math.random() * doc.length);         //随机选取其一输出
            random = doc[index];
            System.out.println(random);
    	}
    	else
    		handler.handleRequest(number);     //将请求传递给下一个处理者
        }
      public void setNextHandler(Handler handler){
         this.handler=handler;
    }

}

Diamond270DrawFivePrizes,java

package angle_responsibility;

/*
              角色2.2:具体处理者 :具体处理者是实现处理者接口的类的实例
                              具体处理者通过调用处理者接口规定的方法,处理用户的请求
                              即在接到用户的请求后,处理者将调用接口规定的方法,在执行该方法的过程中
                              如果发现能处理用户的请求,就处理有关数据
                              否则就反馈无法处理的信息给用户,然后将用户的请求传递给自己的后继对象
*/

public class Diamond270DrawFivePrizes implements Handler{
	private Handler handler;      //存放当前处理者后继的Hander接口变量
    public void handleRequest(int number){
   	if(number==270){               //270钻石抽五次
   		   String random,random1,random2,random3,random4 = "";
           String[] doc = {"白起", "夏侯惇", "甄姬", "金币288","小喇叭5","铭文碎片1600", "铭文碎片400","铭文碎片100", "铭文碎片25", "爱心气球(3日)", "亲密玫瑰","钻石48","龙域领主体验卡"};
           int index = (int) (Math.random() * doc.length);      //随机选取其五输出
           random = doc[index];
           System.out.print(random+"、");
           int index1 = (int) (Math.random() * doc.length);
           random1 = doc[index1];
           System.out.print(random1+"、");
           int index2 = (int) (Math.random() * doc.length);
           random2 = doc[index2];
           System.out.print(random2+"、");
           int index3 = (int) (Math.random() * doc.length);
           random3 = doc[index3];
           System.out.print(random3+"、");
           int index4 = (int) (Math.random() * doc.length);
           random4 = doc[index4];
           System.out.println(random4);
   	}
   	else
            handler.handleRequest(number);      //将请求传递给下一个处理者
       }
     public void setNextHandler(Handler handler){
        this.handler=handler;
   }

}

RareCrystalOfKings.java

package angle_responsibility;

/*
             角色2.3:具体处理者 :具体处理者是实现处理者接口的类的实例
                             具体处理者通过调用处理者接口规定的方法,处理用户的请求
                             即在接到用户的请求后,处理者将调用接口规定的方法,在执行该方法的过程中
                             如果发现能处理用户的请求,就处理有关数据
                             否则就反馈无法处理的信息给用户,然后将用户的请求传递给自己的后继对象
*/

public class RareCrystalOfKings implements Handler{
	private Handler handler;      //存放当前处理者后继的Hander接口变量
    public void handleRequest(int number){
   	if(number==201){               //当幸运值满201时,出稀有水晶
           System.out.println("【稀有】王者水晶");
   	}
   	else
            handler.handleRequest(number);      //将请求传递给下一个处理者
       }
     public void setNextHandler(Handler handler){
        this.handler=handler;
   }

}

Screenshot operation results
because the prize is random, so much as a few pictures taken Test

The first pumping:
Here Insert Picture Description
Second pumping:
Here Insert Picture Description
......

N-th pumping:

Here Insert Picture Description
More application design patterns in the glory of the King in → Click My Home

Welcome message, along with the exchange of learning

Thanks for reading

END

Released nine original articles · won praise 10 · views 7068

Guess you like

Origin blog.csdn.net/IT_charge/article/details/105011204