- 现在需要开发一款游戏软件,请以单例模式来设计其中的 Boss 角色。角色的属性和动作可以任意设计。 要求:该 Boss 类可以在多线程中使用。( 8 分)
Public class Boss{
Private static Boss instance; //(2 分)
Private Boss(){ }//(2 分)
Public static Boss getInstance(){ // ( 2 分)
If(instance == null){
Synchronized(Boss.Class){ // ( synchronized 关键字, 2 分)
If(instance == null)
Instance = new Boss();
}
}
return instance;
}
}
或者
Public class Boss{
Private static Boss instance = new Boss();//(4 分)
Private Boss(){}// ( 2 分)
Public static Boss getInstance(){//2 分
Return instance;
}
}
-
一个农场公司,专门负责培育各种水果,有葡萄,草莓和苹果,请使用工厂方法,编写工厂类和主程序,并在主程序中来完成草莓生长状态的描述。( 8 分)
Public interface Factory{ Fruit build(); } Public class AppleFactory implements Factory{ Public Fruit build(){ Return new Apple(); } } Public class GrapeFactory implements Factory{ Public Fruit build(){ Return new Grape(); } } Public class StrawberryFactory implements Factory{ Public Fruit build(){ Return new Strawberry(); } } Public class MainUI{ Public static void main(string[] str){ Factory fac = new StrawberryFactory(); Fruit ft = fac.build(); ft.plant(); Ft.grow(); Ft.harvest(); } }
-
给定如图所示的树形结构,请应用组合模式,在客户端完成数据的展示。具体要求如下:
绘制组合模式的类图。( 4 分)
编写简单元素和复杂元素的代码。( 4 分)
在客户端构造出树形数据并输出。( 4 分)
提示:程序运行后,输出信息应为Root Leaf A Comp B Leaf BX Leaf BY Leaf C
- 类图,类名不限,但必须将抽象的概念,以及Composite和Component之间的关系用正确的连线表示。(4分)
2) 简单元素、复杂元素(4分):
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){} public void remove(Component c){} } 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);} }
客户端( 4 分):
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(); } }
- 类图,类名不限,但必须将抽象的概念,以及Composite和Component之间的关系用正确的连线表示。(4分)
-
迭代器模式: 提供一种方法来访问聚合对象,而不用暴露这个对象的内部表示,其别名为游标 (Cursor) 。
CustomerManager 类
public class CustomerManager {
private ArrayList<String> list = new ArrayList<String>();
public void add(String str) {
list .add(str); }
protected ArrayList<String> getList() {
return list ; }
public Itertator getASItertator() {
return new AscIterator( this ); }
public Itertator getCharItertator(String s) {
return new CharIterator( this , s); }}
Itertator 类
public abstract class Itertator
{ public abstract boolean hasNext();
public abstract String next();}
AscIterator 类
public class AscIterator extends Itertator {
ArrayList<String> list = new ArrayList<String>();
Iterator iter ;
public AscIterator(CustomerManager cm) {
ArrayList<String> ls = cm.getList();
for (String s : ls) {
list .add(s);
Collections. sort ( list );
iter = list .iterator(); } }
public boolean hasNext() {
return iter .hasNext(); }
public String next() {
return (String) iter .next(); }}
CharIterator 类
public class CharIterator extends Itertator {
ArrayList<String> list = new ArrayList<String>();
Iterator iter ;
public CharIterator(CustomerManager cm, String str) {
ArrayList<String> ls = cm.getList();
for (String s : ls) {
if (s.startsWith(str)) {
list .add(s); }
iter = list .iterator(); } }
public boolean hasNext() {
return iter .hasNext();}
public String next() {
return (String) iter .next(); }}
主类
public class Client {
public static void main(String[] args) {
CustomerManager cm= new CustomerManager();
cm.add( "zhangsan" );
cm.add( "lisi" );
cm.add( "brown" );
cm.add( "alien" );
Itertator iter;
iter=cm.getCharItertator( "a" );
while (iter.hasNext()){
String str=iter.next();
System. out .println(str); } }}