使用Java语言编写程序,使用五种设计模式,不使用简单工厂和单例模式,可使用装饰模式或者其他模式,必须使用五种模式。题目:XX游戏抽卡,需要有概率公⽰说明(屏幕输出查看),抽卡物品可以是⼈物或装备。要求⾄少有三个不同等级的物品(⽐如3星,4星,5星),要在代码中写出不同星级物品的名字 ⽰例:抽卡有单抽和10连,⼀般为3星,4星的概率8%,5星概率1%,10连必出4星,抽80 次没出5星必出5星。要使用命令行输入抽奖次数,输出抽卡结果。用java代码展示结果
时间: 2023-09-07 11:12:46 浏览: 177
Java面试 java设计模式整理 单例、工厂、策略等 面试使用,学习使用
在这个游戏中,我们将使用以下五种设计模式:工厂方法模式、策略模式、装饰者模式、观察者模式和模板方法模式。以下是代码示例:
首先,我们需要定义一个抽卡物品的接口,包括获取名称和星级的方法:
```java
public interface GachaItem {
String getName();
int getStars();
}
```
然后,我们可以实现这个接口来创建不同星级的物品:
```java
public class ThreeStarItem implements GachaItem {
@Override
public String getName() {
return "三星物品";
}
@Override
public int getStars() {
return 3;
}
}
public class FourStarItem implements GachaItem {
@Override
public String getName() {
return "四星物品";
}
@Override
public int getStars() {
return 4;
}
}
public class FiveStarItem implements GachaItem {
@Override
public String getName() {
return "五星物品";
}
@Override
public int getStars() {
return 5;
}
}
```
接下来,我们将使用工厂方法模式来创建不同星级的物品。我们需要定义一个抽象的抽卡工厂类,并在其中声明一个创建抽卡物品的方法:
```java
public abstract class GachaFactory {
public abstract GachaItem createGachaItem();
}
```
然后,我们可以实现这个工厂类来创建不同星级的物品:
```java
public class ThreeStarItemFactory extends GachaFactory {
@Override
public GachaItem createGachaItem() {
return new ThreeStarItem();
}
}
public class FourStarItemFactory extends GachaFactory {
@Override
public GachaItem createGachaItem() {
return new FourStarItem();
}
}
public class FiveStarItemFactory extends GachaFactory {
@Override
public GachaItem createGachaItem() {
return new FiveStarItem();
}
}
```
现在,我们可以使用工厂方法模式来创建不同星级的物品了:
```java
GachaFactory factory = new ThreeStarItemFactory();
GachaItem item = factory.createGachaItem();
System.out.println("抽到了:" + item.getName() + ",星级为:" + item.getStars());
```
下一步,我们将使用策略模式来定义不同抽卡方式的概率。我们需要定义一个抽卡概率接口,并在其中声明一个获取概率的方法:
```java
public interface GachaProbability {
double getProbability();
}
```
然后,我们可以实现这个接口来创建不同抽卡方式的概率:
```java
public class SingleGachaProbability implements GachaProbability {
@Override
public double getProbability() {
return 0.08;
}
}
public class TenGachaProbability implements GachaProbability {
@Override
public double getProbability() {
return 1.0;
}
}
```
现在,我们可以使用策略模式来计算不同抽卡方式的概率了:
```java
GachaProbability probability = new SingleGachaProbability();
double p = probability.getProbability();
```
接下来,我们将使用装饰者模式来实现抽卡结果的输出。我们需要定义一个抽卡结果接口,并在其中声明一个输出结果的方法:
```java
public interface GachaResult {
void printResult();
}
```
然后,我们可以实现这个接口来输出不同的抽卡结果:
```java
public class ThreeStarGachaResult implements GachaResult {
private GachaResult result;
public ThreeStarGachaResult(GachaResult result) {
this.result = result;
}
@Override
public void printResult() {
result.printResult();
System.out.println("获得了一个三星物品!");
}
}
public class FourStarGachaResult implements GachaResult {
private GachaResult result;
public FourStarGachaResult(GachaResult result) {
this.result = result;
}
@Override
public void printResult() {
result.printResult();
System.out.println("获得了一个四星物品!");
}
}
public class FiveStarGachaResult implements GachaResult {
private GachaResult result;
public FiveStarGachaResult(GachaResult result) {
this.result = result;
}
@Override
public void printResult() {
result.printResult();
System.out.println("获得了一个五星物品!");
}
}
```
现在,我们可以使用装饰者模式来输出抽卡结果了:
```java
GachaResult result = new ThreeStarGachaResult(new FourStarGachaResult(new FiveStarGachaResult(null)));
result.printResult();
```
接下来,我们将使用观察者模式来实现概率公示的输出。我们需要定义一个观察者接口,并在其中声明一个更新概率的方法:
```java
public interface GachaObserver {
void update(double probability);
}
```
然后,我们可以实现这个接口来创建不同的观察者:
```java
public class SingleGachaObserver implements GachaObserver {
@Override
public void update(double probability) {
System.out.println("单抽概率为:" + probability);
}
}
public class TenGachaObserver implements GachaObserver {
@Override
public void update(double probability) {
System.out.println("十连概率为:" + probability);
}
}
```
现在,我们可以使用观察者模式来输出概率公示了:
```java
GachaObserver singleObserver = new SingleGachaObserver();
GachaObserver tenObserver = new TenGachaObserver();
singleProbability.addObserver(singleObserver);
tenProbability.addObserver(tenObserver);
```
最后,我们将使用模板方法模式来实现抽卡流程的控制。我们需要定义一个抽卡模板类,并在其中声明一个抽卡方法:
```java
public abstract class GachaTemplate {
private int times;
public GachaTemplate(int times) {
this.times = times;
}
public void gacha() {
GachaProbability probability = getGachaProbability();
GachaFactory factory = getGachaFactory();
for (int i = 0; i < times; i++) {
double p = probability.getProbability();
GachaItem item = factory.createGachaItem();
GachaResult result = getGachaResult(item);
if (p >= 0.01) {
result.printResult();
} else {
GachaResult fiveStarResult = new FiveStarGachaResult(result);
fiveStarResult.printResult();
break;
}
}
}
public abstract GachaProbability getGachaProbability();
public abstract GachaFactory getGachaFactory();
public abstract GachaResult getGachaResult(GachaItem item);
}
```
然后,我们可以实现这个模板类来控制不同的抽卡流程:
```java
public class SingleGachaTemplate extends GachaTemplate {
public SingleGachaTemplate(int times) {
super(times);
}
@Override
public GachaProbability getGachaProbability() {
return new SingleGachaProbability();
}
@Override
public GachaFactory getGachaFactory() {
double p = Math.random();
if (p < 0.6) {
return new ThreeStarItemFactory();
} else if (p < 0.96) {
return new FourStarItemFactory();
} else {
return new FiveStarItemFactory();
}
}
@Override
public GachaResult getGachaResult(GachaItem item) {
if (item.getStars() == 3) {
return new ThreeStarGachaResult(null);
} else if (item.getStars() == 4) {
return new FourStarGachaResult(null);
} else {
return new FiveStarGachaResult(null);
}
}
}
public class TenGachaTemplate extends GachaTemplate {
public TenGachaTemplate(int times) {
super(times);
}
@Override
public GachaProbability getGachaProbability() {
return new TenGachaProbability();
}
@Override
public GachaFactory getGachaFactory() {
return new FourStarItemFactory();
}
@Override
public GachaResult getGachaResult(GachaItem item) {
if (item.getStars() == 4) {
return new FourStarGachaResult(null);
} else {
return new FiveStarGachaResult(null);
}
}
}
```
现在,我们可以使用模板方法模式来控制抽卡流程了:
```java
GachaTemplate template = new SingleGachaTemplate(10);
template.gacha();
```
阅读全文