Source: https://blog.csdn.net/IT_charge
1, Brief description
In the king glory mall, players can participate in the treasure draw. There are two kinds of lucky draw, one is the integral lucky draw, the other is the diamond lucky draw; In ordinary times, the two winning lottery methods can draw once through 60 diamonds / points, or draw five times continuously through 270 diamonds / points; Among them, when the lucky value of diamond loot reaches 201, you can obtain rare King crystal, and when the lucky value of integral loot reaches 361, you can obtain rare glory crystal; Players can use King crystal or glory crystal in exchange for specific heroes, skin, equipment, etc.
In many cases, more than one object can process a request. Many objects can process applications, and they form a chain along which requests are passed. This chain is called the responsibility chain.
According to the above description, treasure taking in the glory of the king is an example of the application of the responsibility chain model in the glory of the king. In this problem, players can draw a lottery result by clicking 60 diamonds / points once or 270 diamonds / points five times.
Specifically, we first receive requests submitted by users through an interface, and then implement specific request processing operations through three class instances; Among them, the class "60 diamonds / points once" is used to obtain one prize randomly among 14 kinds of prizes, the class "270 diamonds / points five times" is used to obtain five prizes randomly among 14 kinds of prizes, and the class "after the lucky value reaches a certain value" is used to obtain the reward of rare crystals.
2, Responsibility chain model
Understanding of responsibility chain pattern: high generalization: multiple objects have the opportunity to process the request, so as to avoid the coupling relationship between the sender and receiver of the request, connect these objects into a chain, and pass the request along the chain until an object processes it.
When designing Java patterns, you may need to design many objects to meet user requests.
The responsibility chain model is a mature model that uses multiple objects to process user requests. The key of the responsibility chain model is to assign user requests to many objects. These objects are organized into a responsibility chain, that is, each object contains references to subsequent objects, and requires each object in the responsibility chain to handle user requests if it can, The user's request is no longer passed to the next object in the responsibility chain. If the user's request cannot be processed, the user's request must be passed to the next object in the responsibility chain.
There are two roles in the responsibility chain model structure:
❶ handler: it is an interface, which is responsible for specifying the method for a specific handler to process user requests and the method for a specific handler to set subsequent objects; Latest Java interview questions Share it with you.
❷ specific handler: a specific handler is an instance of a class that implements the handler interface. The specific processor processes the user's request by calling the method specified in the processor interface, that is, after receiving the user's request, the processor will call the method specified in the interface. During the execution of the method, if it is found that it can process the user's request, it will process the relevant data; Otherwise, it will feed back the information that cannot be processed to the user, and then pass the user's request to its successor object.
UML class diagram of responsibility chain pattern:
Advantages and disadvantages of the responsibility chain model:
advantage:
① The objects in the responsibility chain only have a low coupling relationship with their successors and have nothing to do with other objects;
② When assigning responsibilities among processors, the responsibility chain gives the application more flexibility;
③ Applications can dynamically add, delete or reassign the responsibilities of processors;
④ Applications can dynamically change the order of processors;
⑤ The user using the responsibility chain does not need to know the information of the handler, and the user does not know which object handled his request;
Disadvantages:
① There is no guarantee that the request will be received: there is no clear receiver, and there is no guarantee that the request will be processed;
② For too long responsibility chain, system performance will be affected. Improper chain building may lead to dead circulation.
Applicable scenarios of responsibility chain model:
① There are many objects that can handle the user's request. It is hoped that the program will automatically determine the object that handles the user during operation;
② It is hoped that the user can submit a request to one of multiple recipients without explicitly specifying the receiver;
③ The program wants to dynamically formulate a collection of objects that can handle user requests.
3, Structure diagram and code of responsibility chain mode from the perspective of King glory
UML class diagram implementing this responsibility chain pattern
eclipse structure diagram
Main function [application]
Application.java
package angle_responsibility; /* * Test application class */ 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; //Objects in the responsibility chain public void createChain(){ //Establish responsibility chain diamond60=new Diamond60DrawAPrize(); diamond270=new Diamond270DrawFivePrizes(); rareDiamond=new RareCrystalOfKings(); diamond60.setNextHandler(diamond270); diamond270.setNextHandler(rareDiamond); } public void reponseClient(int number){ //Respond to user requests diamond60.handleRequest(number); } public static void main(String args[]){ Application application=new Application(); application.createChain(); System.out.println("When you click "60 diamonds" to draw once:"); System.out.print("[Purchase successful]"); application.reponseClient(60); System.out.println("---------------------------"); System.out.println("When you click "270 diamond" to draw five times:"); System.out.print("[Purchase successful]"); application.reponseClient(270); System.out.println("---------------------------"); System.out.println("When the "lucky value" of diamond lottery reaches 201:"); System.out.print("[Purchase successful]"); application.reponseClient(201); } }
Handler
Handler.java
package angle_responsibility; /* * Role 1: handler: it is an interface, which is responsible for specifying the specific handler's method of processing user requests and the specific handler's method of setting subsequent objects */ import angle_responsibility.Handler; public interface Handler { public abstract void handleRequest(int number); //Specifically handle the user's request to draw 60 diamonds once or 270 diamonds five times public abstract void setNextHandler(Handler handler); }
Concrete handler
Diamond60DrawAPrize .java
package angle_responsibility; /* * Role 2.1: specific handler: a specific handler is an instance of a class that implements the handler interface * The specific handler processes the user's request by calling the method specified in the handler interface * That is, after receiving the user's request, the handler will call the method specified in the interface, and in the process of executing the method * If it is found that the user's request can be processed, the relevant data will be processed * Otherwise, it will feed back the information that cannot be processed to the user, and then pass the user's request to its successor object */ import angle_responsibility.Handler; public class Diamond60DrawAPrize implements Handler{ private Handler handler; //Store the subsequent handler interface variables of the current handler public void handleRequest(int number){ if(number==60){ //60 diamonds once String random = ""; String[] doc = {"Bai Qi", "Xia Houdun", "Zhen Ji", "Gold 288","Small horn 5","Inscription fragment 1600", "Inscription fragment 400","Inscription fragment 100", "Inscription fragment 25", "Love balloon (3 days)", "Intimate rose","Diamond 48","Dragon domain Lord experience card"}; int index = (int) (Math.random() * doc.length); //Select one of the outputs at random random = doc[index]; System.out.println(random); } else handler.handleRequest(number); //Pass the request to the next handler } public void setNextHandler(Handler handler){ this.handler=handler; } }
Diamond270DrawFivePrizes,java
package angle_responsibility; /* * Role 2.2: specific handler: the specific handler is an instance of a class that implements the handler interface * The specific handler processes the user's request by calling the method specified in the handler interface * That is, after receiving the user's request, the handler will call the method specified in the interface, and in the process of executing the method * If it is found that the user's request can be processed, the relevant data will be processed * Otherwise, it will feed back the information that cannot be processed to the user, and then pass the user's request to its successor object */ public class Diamond270DrawFivePrizes implements Handler{ private Handler handler; //Store the subsequent handler interface variables of the current handler public void handleRequest(int number){ if(number==270){ //270 diamonds drawn five times String random,random1,random2,random3,random4 = ""; String[] doc = {"Bai Qi", "Xia Houdun", "Zhen Ji", "Gold 288","Small horn 5","Inscription fragment 1600", "Inscription fragment 400","Inscription fragment 100", "Inscription fragment 25", "Love balloon (3 days)", "Intimate rose","Diamond 48","Dragon domain Lord experience card"}; int index = (int) (Math.random() * doc.length); //Five outputs are selected randomly 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); //Pass the request to the next handler } public void setNextHandler(Handler handler){ this.handler=handler; } }
RareCrystalOfKings.java
package angle_responsibility; /* * Role 2.3: specific handler: a specific handler is an instance of a class that implements the handler interface * The specific handler processes the user's request by calling the method specified in the handler interface * That is, after receiving the user's request, the handler will call the method specified in the interface, and in the process of executing the method * If it is found that the user's request can be processed, the relevant data will be processed * Otherwise, it will feed back the information that cannot be processed to the user, and then pass the user's request to its successor object */ public class RareCrystalOfKings implements Handler{ private Handler handler; //Store the subsequent handler interface variables of the current handler public void handleRequest(int number){ if(number==201){ //When the lucky value reaches 201, a rare crystal will appear System.out.println("[[rare] king crystal"); } else handler.handleRequest(number); //Pass the request to the next handler } public void setNextHandler(Handler handler){ this.handler=handler; } }
Screenshot of operation results
Because the prize is random, take more pictures as a test. Latest Java interview questions Share it with you.
First pumping:
Second pumping:
......
N th pumping:
Recent hot article recommendations:
1.1000 + Java interview questions and answers (2021 latest version)
2.Stop playing if/ else on the full screen. Try the strategy mode. It's really fragrant!!
3.what the fuck! What is the new syntax of xx ≠ null in Java?
4.Spring Boot 2.5 heavy release, dark mode is too explosive!
5.Java development manual (Songshan version) is the latest release. Download it quickly!
Feel good, don't forget to like + forward!