Applying the Chain of Responsibility Pattern to King of Glory Lottery System (Java Implementation)
This article explains how the Chain of Responsibility design pattern can be used to model the King of Glory lottery mechanism, describes the pattern's structure, advantages, and use cases, and provides a complete Java implementation with handler interfaces and concrete classes for different draw actions.
The King of Glory in‑game shop offers two types of lottery draws—single 60‑diamond/point draws and batch 270‑diamond/point draws—and a rare crystal reward when a lucky value threshold is reached; this process can be modeled using the Chain of Responsibility pattern.
The pattern connects multiple handler objects in a chain so that each handler can either process a request or forward it to the next handler, thereby decoupling the request sender from concrete handlers; the key roles are the Handler interface and its ConcreteHandler implementations.
Advantages of the pattern include low coupling between handlers, flexible assignment of responsibilities, dynamic addition or removal of handlers, and the ability to change handler order at runtime; disadvantages are the lack of guaranteed handling and potential performance issues with long chains.
The pattern is suitable when many objects can handle a request, when the client should not need to know the concrete handler, and when the set of handlers may change dynamically.
The article provides UML class diagram illustrations (omitted here) and then presents a full Java example.
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);
}
} package angle_responsibility;
/*
角色1:处理者 :是一个接口,负责规定具体处理者处理用户请求的方法以及具体处理者设置后继对象的方法
*/
public interface Handler {
public abstract void handleRequest(int number); //处理请求
public abstract void setNextHandler(Handler handler);
} package angle_responsibility;
/*
角色2.1:具体处理者
*/
import angle_responsibility.Handler;
public class Diamond60DrawAPrize implements Handler{
private Handler handler;
public void handleRequest(int number){
if(number==60){
String[] doc = {"白起","夏侯惇","甄姬","金币288","小喇叭5","铭文碎片1600","铭文碎片400","铭文碎片100","铭文碎片25","爱心气球(3日)","亲密玫瑰","钻石48","龙域领主体验卡"};
int index = (int)(Math.random()*doc.length);
System.out.println(doc[index]);
} else {
handler.handleRequest(number);
}
}
public void setNextHandler(Handler handler){ this.handler=handler; }
} package angle_responsibility;
/*
角色2.2:具体处理者
*/
public class Diamond270DrawFivePrizes implements Handler{
private Handler handler;
public void handleRequest(int number){
if(number==270){
String[] doc = {"白起","夏侯惇","甄姬","金币288","小喇叭5","铭文碎片1600","铭文碎片400","铭文碎片100","铭文碎片25","爱心气球(3日)","亲密玫瑰","钻石48","龙域领主体验卡"};
for(int i=0;i<5;i++){
int idx = (int)(Math.random()*doc.length);
System.out.print(doc[idx]);
if(i<4) System.out.print("、");
}
System.out.println();
} else {
handler.handleRequest(number);
}
}
public void setNextHandler(Handler handler){ this.handler=handler; }
} package angle_responsibility;
/*
角色2.3:具体处理者
*/
public class RareCrystalOfKings implements Handler{
private Handler handler;
public void handleRequest(int number){
if(number==201){
System.out.println("【稀有】王者水晶");
} else {
handler.handleRequest(number);
}
}
public void setNextHandler(Handler handler){ this.handler=handler; }
}The program prints the randomly selected prizes for each draw and shows the rare crystal message when the lucky value reaches 201, demonstrating how the responsibility chain processes different lottery requests.
Finally, the author invites readers to like and share the post.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
