1.下图是某系统的数据部分的类图。因为该层次结构中的操作需要经常变化,所以需要用访问者模式对其进行重构,请按以下要求完成题目:
(1)绘制重构后系统完整类图。(4分)
(2)给出重构后ElementA类的代码。(4分)
(3)在客户端运用访问者模式,对ElementA的实例,完成CalPrice操作。(2分)
package 题库.访问者模式;// 元素抽象类
public abstract class Element {
public abstract void accept(Visitor v);
}// 具体元素类A
public class ElementA extends Element {
public void accept(Visitor v) {
v.visit(this);
}
}// 具体元素类B
public class ElementB extends Element {
public void accept(Visitor v) {
v.visit(this);
}
}// 访问者抽象类
public abstract class Visitor {
public abstract void visit(ElementA element);
public abstract void visit(ElementB element);
}// 具体访问者类
public class CalPriceVistor extends Visitor {
public void visit(ElementA element) {
// 访问元素A,并执行相关业务逻辑
}public void visit(ElementB element) {
// 访问元素B,并执行相关业务逻辑
}
}// 客户端
public class Client {
public static void main(String[] args) {
Visitor v = new CalPriceVistor();
ElementA a = new ElementA();
ElementB b = new ElementB();
a.accept(v);
b.accept(v);
}
}
- 给定如图所示的树形结构,请应用组合模式,在客户端完成数据的展示。具体要求如下:
1)绘制组合模式的类图。(4分)
2)编写简单元素和复杂元素的代码。(4分)
3)在客户端构造出树形数据并输出。(4分)
提示:程序运行后,输出信息应为
Root
Leaf A
Comp B
Leaf BX
Leaf BY
Leaf C
//节点接口
interface Component {void display();void add(Component c);void remove(Component c);
}
//叶子节点
class Leaf implements Component {String name;public Leaf(String name) {this.name = name;}public void display() {System.out.println(name);}public void add(Component c) {// Leaf节点无法添加子节点,不做处理}public void remove(Component c) {// Leaf节点无法移除子节点,不做处理}
}
//父母节点
class Composite implements Component {String name;ArrayList<Component> list = new ArrayList<Component>();public Composite(String name) {this.name = name;}public void display() {System.out.println(name);for (int i = 0; i < list.size(); i++) {list.get(i).display();}}public void add(Component c) {list.add(c);}public void remove(Component c) {list.remove(c);}
}public class Test {public static void main(String[] args) {Component root = new Composite("Root");root.add(new Leaf("Leaf A"));Component comp = new Composite("Comp B");root.add(comp);comp.add(new Leaf("Leaf BX"));comp.add(new Leaf("Leaf BY"));root.add(new Leaf("Leaf C"));root.display();}
}
- 现在需要开发一款游戏软件,请以单例模式来设计其中的Boss角色。角色的属性和动作可以任意设计。 要求:该Boss类可以在多线程中使用。(8分)
package 题库;/*** @Description: 单例模式* @ClassName: Boss* @Author: weisn* @Date: 2023/5/22 20:24* @Version: 1.0*/
public class Boss {private static Boss instance;private Boss() {}//synchronized(Boss.class) 表示 Boss 类的 class 对象作为锁对象,用于使用同步代码块。// 当线程获取到该锁对象时,才能够执行被锁定的代码块。//在单例模式中,由于只有一个实例存在,我们需要保证在多线程情况下获取该实例的过程中,不能创建多个对象。// 因此,在getInstance()方法中需要使用同步代码块来进行线程同步,使用双重检查(lock with double check)保证线程安全,// 这里的 synchronized(Boss.class) 就是保证线程安全的关键。public static Boss getInstance() {if (instance == null) {synchronized(Boss.class) {if (instance == null) {instance = new Boss();}}}return instance;}public void attack() {System.out.println("The boss is attacking!");}public void defend() {System.out.println("The boss is defending!");}public void specialAttack() {System.out.println("The boss is using a special attack!");}// 再添加几个属性和方法用于示例private int health = 100;private int damage = 10;private int defense = 5;public int getHealth() {return health;}public void setHealth(int health) {this.health = health;}public int getDamage() {return damage;}public void setDamage(int damage) {this.damage = damage;}public int getDefense() {return defense;}public void setDefense(int defense) {this.defense = defense;}
}
- 一个农场公司,专门负责培育各种水果,有葡萄,草莓和苹果,请使用工厂方法,编写工厂类和主程序,并在主程序中来完成草莓生长状态的描述。(8分)—工厂模式
package 题库.水果工厂;
/*** @Description: 抽象工厂类* @ClassName: Fruit* @Author: weisn* @Date: 2023/5/22 20:38* @Version: 1.0*/
public abstract class Fruit {// 种植public abstract void plant();// 生长public abstract void grow();// 收获public abstract void harvest();
}
package 题库.水果工厂;public class Apple extends Fruit {@Overridepublic void plant() {System.out.println("种植苹果树");}@Overridepublic void grow() {System.out.println("苹果正在生长");}@Overridepublic void harvest() {System.out.println("收获苹果");}
}
```java
package 题库.水果工厂;public class Grape extends Fruit {@Overridepublic void plant() {System.out.println("种植葡萄树");}@Overridepublic void grow() {System.out.println("葡萄正在生长");}@Overridepublic void harvest() {System.out.println("收获葡萄");}
}
```java
package 题库.水果工厂;public class Strawberry extends Fruit {@Overridepublic void plant() {System.out.println("种植草莓");}@Overridepublic void grow() {System.out.println("草莓正在生长");}@Overridepublic void harvest() {System.out.println("收获草莓");}
}
package 题库.水果工厂;public interface Factory {Fruit build();
}package 题库.水果工厂;public class AppleFactory implements Factory {public Fruit build() {return new Apple();}
}
package 题库.水果工厂;public class GrapeFactory implements Factory {public Fruit build() {return new Grape();}
}
package 题库.水果工厂;public class StrawberryFactory implements Factory {public Fruit build() {return new Strawberry();}
}
package 题库.水果工厂;public class MainUI {public static void main(String[] args) {Factory fac = new StrawberryFactory();Fruit ft = fac.build();ft.plant();ft.grow();ft.harvest();}
}
5.第四题用简单工厂模式实现:
package 题库.水果简单工厂;public abstract class Fruit {public abstract void plant();public abstract void grow();public abstract void harvest();
}
package 题库.水果简单工厂;public class Apple extends Fruit {@Overridepublic void plant() {System.out.println("种植苹果树");}@Overridepublic void grow() {System.out.println("苹果正在生长");}@Overridepublic void harvest() {System.out.println("收获苹果");}
}
package 题库.水果简单工厂;public class Grape extends Fruit {@Overridepublic void plant() {System.out.println("种植葡萄树");}@Overridepublic void grow() {System.out.println("葡萄正在生长");}@Overridepublic void harvest() {System.out.println("收获葡萄");}
}
package 题库.水果简单工厂;public class Strawberry extends Fruit {@Overridepublic void plant() {System.out.println("种植草莓");}@Overridepublic void grow() {System.out.println("草莓正在生长");}@Overridepublic void harvest() {System.out.println("收获草莓");}
}
package 题库.水果简单工厂;public class FruitFactory {public static Fruit createFruit(String type) {Fruit ft = null;if (type.equals("Apple")) {ft = new Apple();} else if (type.equals("Grape")) {ft = new Grape();} else if (type.equals("Strawberry")) {ft = new Strawberry();}return ft;}
}
package 题库.水果简单工厂;public class MainUI {public static void main(String[] args) {Fruit ft = FruitFactory.createFruit("Apple");ft.plant();ft.grow();ft.harvest();}
}