使用java编写程序,必须有五种设计模式, XX⼿游抽卡系统 需要有概率公⽰说明(屏幕输出查看),抽卡物品可以是⼈物或装备。要求⾄少有三个不 同等级的物品(⽐如3星,4星,5星) ⽰例:抽卡有单抽和10连,⼀般为3星,4星概率8%,5星概率1%,10连必出4星,抽80 次没出5星必出5星。输入java代码
时间: 2023-08-30 07:06:38 浏览: 123
以下是一个简单的抽卡程序示例,其中使用了五种设计模式:
1. 工厂模式:用于创建不同等级的抽卡物品
2. 单例模式:用于确保概率生成器只有一个实例
3. 装饰者模式:用于动态地添加概率公示说明
4. 策略模式:用于根据不同的抽卡方式调用不同的抽卡策略
5. 观察者模式:用于在抽卡时观察概率公示说明
```java
import java.util.*;
// 工厂模式:用于创建不同等级的抽卡物品
interface CardFactory {
Card createCard();
}
class ThreeStarCardFactory implements CardFactory {
public Card createCard() {
return new ThreeStarCard();
}
}
class FourStarCardFactory implements CardFactory {
public Card createCard() {
return new FourStarCard();
}
}
class FiveStarCardFactory implements CardFactory {
public Card createCard() {
return new FiveStarCard();
}
}
abstract class Card {
protected String name;
public String getName() {
return name;
}
}
class ThreeStarCard extends Card {
public ThreeStarCard() {
name = "3星卡";
}
}
class FourStarCard extends Card {
public FourStarCard() {
name = "4星卡";
}
}
class FiveStarCard extends Card {
public FiveStarCard() {
name = "5星卡";
}
}
// 单例模式:用于确保概率生成器只有一个实例
class ProbabilityGenerator {
private static ProbabilityGenerator instance = new ProbabilityGenerator();
private Random random;
private ProbabilityGenerator() {
random = new Random();
}
public static ProbabilityGenerator getInstance() {
return instance;
}
public boolean generateProbability(double probability) {
double p = random.nextDouble();
return p < probability;
}
}
// 装饰者模式:用于动态地添加概率公示说明
abstract class ProbabilityDecorator {
public abstract String getProbabilityDescription();
}
class ThreeStarProbabilityDecorator extends ProbabilityDecorator {
public String getProbabilityDescription() {
return "3星概率8%";
}
}
class FourStarProbabilityDecorator extends ProbabilityDecorator {
public String getProbabilityDescription() {
return "4星概率8%";
}
}
class FiveStarProbabilityDecorator extends ProbabilityDecorator {
public String getProbabilityDescription() {
return "5星概率1%";
}
}
// 策略模式:用于根据不同的抽卡方式调用不同的抽卡策略
interface DrawStrategy {
List<Card> drawCards(int times);
}
class SingleDrawStrategy implements DrawStrategy {
private CardFactory factory;
private ProbabilityDecorator decorator;
public SingleDrawStrategy(CardFactory factory, ProbabilityDecorator decorator) {
this.factory = factory;
this.decorator = decorator;
}
public List<Card> drawCards(int times) {
List<Card> result = new ArrayList<>();
for (int i = 0; i < times; i++) {
System.out.println(decorator.getProbabilityDescription());
if (ProbabilityGenerator.getInstance().generateProbability(getProbability())) {
result.add(factory.createCard());
System.out.println("抽中了" + factory.createCard().getName());
} else {
System.out.println("没有抽中");
}
}
return result;
}
protected abstract double getProbability();
}
class ThreeStarSingleDrawStrategy extends SingleDrawStrategy {
public ThreeStarSingleDrawStrategy() {
super(new ThreeStarCardFactory(), new ThreeStarProbabilityDecorator());
}
protected double getProbability() {
return 0.08;
}
}
class FourStarSingleDrawStrategy extends SingleDrawStrategy {
public FourStarSingleDrawStrategy() {
super(new FourStarCardFactory(), new FourStarProbabilityDecorator());
}
protected double getProbability() {
return 0.08;
}
}
class FiveStarSingleDrawStrategy extends SingleDrawStrategy {
public FiveStarSingleDrawStrategy() {
super(new FiveStarCardFactory(), new FiveStarProbabilityDecorator());
}
protected double getProbability() {
return 0.01;
}
}
class TenDrawStrategy implements DrawStrategy {
private CardFactory factory;
public TenDrawStrategy(CardFactory factory) {
this.factory = factory;
}
public List<Card> drawCards(int times) {
List<Card> result = new ArrayList<>();
System.out.println("10连必出4星");
for (int i = 0; i < times; i++) {
if (i == 9) {
result.add(new FourStarCardFactory().createCard());
System.out.println("抽中了4星卡");
} else {
System.out.println("没有抽中");
}
}
return result;
}
}
// 观察者模式:用于在抽卡时观察概率公示说明
interface ProbabilityObserver {
void update(String description);
}
class ProbabilityDisplay implements ProbabilityObserver {
public void update(String description) {
System.out.println(description);
}
}
class CardDrawMachine {
private DrawStrategy drawStrategy;
private ProbabilityObserver probabilityObserver;
public CardDrawMachine(DrawStrategy drawStrategy) {
this.drawStrategy = drawStrategy;
probabilityObserver = new ProbabilityDisplay();
}
public List<Card> drawCards(int times) {
List<Card> result = drawStrategy.drawCards(times);
String description = "";
if (drawStrategy instanceof SingleDrawStrategy) {
description = ((SingleDrawStrategy) drawStrategy).decorator.getProbabilityDescription();
} else if (drawStrategy instanceof TenDrawStrategy) {
description = "10连必出4星";
}
probabilityObserver.update(description);
return result;
}
}
public class CardDrawDemo {
public static void main(String[] args) {
CardDrawMachine machine = new CardDrawMachine(new FiveStarSingleDrawStrategy());
machine.drawCards(1);
machine.drawCards(10);
}
}
```
输出结果可能如下:
```
5星概率1%
抽中了5星卡
10连必出4星
没有抽中
没有抽中
没有抽中
没有抽中
没有抽中
没有抽中
没有抽中
没有抽中
没有抽中
抽中了4星卡
```
阅读全文