Dependence Inversion Principle,DIP
。
design-1.6-0
|——src
|——main
|--java
|--com.lino.design
|--BetUser.java
|--DrawControl.java
|——test
|--java
|--com.lino.design.test
|--ApiTest.java
BetUser.java
package com.lino.design;
/**
* @description: 投注用户
*/
public class BetUser {
/**
* 用户姓名
*/
private String userName;
/**
* 用户权重
*/
private int userWeight;
public BetUser() {
}
public BetUser(String userName, int userWeight) {
this.userName = userName;
this.userWeight = userWeight;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getUserWeight() {
return userWeight;
}
public void setUserWeight(int userWeight) {
this.userWeight = userWeight;
}
}
DrawControl.java
package com.lino.design;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @description: 抽奖控制
*/
public class DrawControl {
/**
* 随机抽取指定数量的用户,作为中奖用户
*
* @param list 用户列表
* @param count 中奖用户数量
* @return 中奖用户列表
*/
public List<BetUser> doDrawRandom(List<BetUser> list, int count) {
// 集合数量很小直接返回
if (list.size() <= count) {
return list;
}
// 乱序集合
Collections.shuffle(list);
// 取出指定数量的中奖用户
List<BetUser> prizeList = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
prizeList.add(list.get(i));
}
return prizeList;
}
public List<BetUser> doDrawWeight(List<BetUser> list, int count) {
// 按照权重排序
list.sort(((o1, o2) -> {
int e = o2.getUserWeight() - o1.getUserWeight();
if (0 == e) {
return 0;
}
return e > 0 ? 1 : -1;
}));
// 取出指定数量的中奖用户
List<BetUser> prizeList = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
prizeList.add(list.get(i));
}
return prizeList;
}
}
ApiTest.java
@Test
public void test_DrawControl() {
List<BetUser> betUserList = new ArrayList<>();
betUserList.add(new BetUser("花花", 65));
betUserList.add(new BetUser("豆豆", 43));
betUserList.add(new BetUser("小白", 72));
betUserList.add(new BetUser("笨笨", 89));
betUserList.add(new BetUser("丑蛋", 10));
DrawControl drawControl = new DrawControl();
List<BetUser> prizeRandomUserList = drawControl.doDrawRandom(betUserList, 3);
logger.info("随机抽奖,中奖用户名单:{}", JSON.toJSON(prizeRandomUserList));
List<BetUser> prizeWeightUserList = drawControl.doDrawWeight(betUserList, 3);
logger.info("权重抽奖,中奖用户名单:{}", JSON.toJSON(prizeWeightUserList));
}
测试结果
14:50:54.496 [main] INFO com.lino.design.test.ApiTest - 随机抽奖,中奖用户名单:[{"userWeight":89,"userName":"笨笨"},{"userWeight":10,"userName":"丑蛋"},{"userWeight":65,"userName":"花花"}]
14:50:54.527 [main] INFO com.lino.design.test.ApiTest - 权重抽奖,中奖用户名单:[{"userWeight":89,"userName":"笨笨"},{"userWeight":72,"userName":"小白"},{"userWeight":65,"userName":"花花"}]
design-1.6-1
|——src
|——main
|--java
|--com.lino.design
|--BetUser.java
|--DrawControl.java
|--DrawRandom.java
|--DrawWeightRank.java
|--IDraw.java
|——test
|--java
|--com.lino.design.test
|--ApiTest.java
BetUser.java
package com.lino.design;
/**
* @description: 投注用户
*/
public class BetUser {
/**
* 用户姓名
*/
private String userName;
/**
* 用户权重
*/
private int userWeight;
public BetUser() {
}
public BetUser(String userName, int userWeight) {
this.userName = userName;
this.userWeight = userWeight;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getUserWeight() {
return userWeight;
}
public void setUserWeight(int userWeight) {
this.userWeight = userWeight;
}
}
IDraw.java
package com.lino.design;
import java.util.List;
/**
* @description: 抽奖接口
*/
public interface IDraw {
/**
* 抽奖
*
* @param list 用户列表
* @param count 中奖数量
* @return 中奖用户列表
*/
List<BetUser> prize(List<BetUser> list, int count);
}
list
集合,以及中奖用户数量。DrawRandom.java
package com.lino.design;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @description: 随机抽奖
*/
public class DrawRandom implements IDraw {
@Override
public List<BetUser> prize(List<BetUser> list, int count) {
// 集合数量很小直接返回
if (list.size() <= count) {
return list;
}
// 乱序集合
Collections.shuffle(list);
// 取出指定数量的中奖用户
List<BetUser> prizeList = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
prizeList.add(list.get(i));
}
return prizeList;
}
}
DrawWeightRank.java
package com.lino.design;
import java.util.ArrayList;
import java.util.List;
/**
* @description: 权重抽奖
*/
public class DrawWeightRank implements IDraw {
@Override
public List<BetUser> prize(List<BetUser> list, int count) {
// 按照权重排序
list.sort(((o1, o2) -> {
int e = o2.getUserWeight() - o1.getUserWeight();
if (0 == e) {
return 0;
}
return e > 0 ? 1 : -1;
}));
// 取出指定数量的中奖用户
List<BetUser> prizeList = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
prizeList.add(list.get(i));
}
return prizeList;
}
}
DrawControl.java
package com.lino.design;
import java.util.List;
/**
* @description: 抽奖控制
*/
public class DrawControl {
/**
* 抽奖接口
*/
private IDraw draw;
/**
* 发起抽奖
*
* @param draw 抽奖类型
* @param betUserList 用户列表
* @param count 中奖数量
* @return 中奖的用户列表
*/
public List<BetUser> doDraw(IDraw draw, List<BetUser> betUserList, int count) {
return draw.prize(betUserList, count);
}
}
ApiTest.java
@Test
public void test_DrawControl() {
List<BetUser> betUserList = new ArrayList<>();
betUserList.add(new BetUser("花花", 65));
betUserList.add(new BetUser("豆豆", 43));
betUserList.add(new BetUser("小白", 72));
betUserList.add(new BetUser("笨笨", 89));
betUserList.add(new BetUser("丑蛋", 10));
DrawControl drawControl = new DrawControl();
List<BetUser> prizeRandomUserList = drawControl.doDraw(new DrawRandom(), betUserList, 3);
logger.info("随机抽奖,中奖用户名单:{}", JSON.toJSON(prizeRandomUserList));
List<BetUser> prizeWeightUserList = drawControl.doDraw(new DrawWeightRank(), betUserList, 3);
logger.info("权重抽奖,中奖用户名单:{}", JSON.toJSON(prizeWeightUserList));
}
测试结果
15:06:03.242 [main] INFO com.lino.design.test.ApiTest - 随机抽奖,中奖用户名单:[{"userWeight":72,"userName":"小白"},{"userWeight":43,"userName":"豆豆"},{"userWeight":10,"userName":"丑蛋"}]
15:06:03.273 [main] INFO com.lino.design.test.ApiTest - 权重抽奖,中奖用户名单:[{"userWeight":89,"userName":"笨笨"},{"userWeight":72,"userName":"小白"},{"userWeight":65,"userName":"花花"}]
new DrawRandom()
、new DrawWeightRank()
。