软件体系结构与设计模式期末复习(4)

2019-01-07 19:18

1) 在客户端构造出树形数据并输出。(4分) 提示:程序运行后,输出信息应为 Root Leaf A Comp B Leaf BX Leaf BY Leaf C

答案:

1. 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; } } 2.

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(); } }

3.

1)类图,类名不限,但必须将抽象的概念,以及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 list = new ArrayList(); 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.add(new Leaf(\ Component comp = new Composite(\ root.add(comp); comp.add(new Leaf(\ comp.add(new Leaf(\ root.add(new Leaf(\ root.display(); } }

(二)

1.现在需要开发一款打印机管理软件,请以单例模式来设计其中的打印池PrintSpooler。要求:该PrintSpooler类可以在多线程中使用。(10分)

2.一个农场公司,专门负责培育各种水果,有葡萄,草莓和苹果,请使用简单工厂模式,编

写简单工厂类和主程序,并在主程序中来完成苹果生长状态的描述。(10分)

3. 给定如图所示的树形结构,请应用组合模式,在客户端完成数据的展示。具体要求如下: 2. 绘制组合模式的类图。(4分) 3. 编写简单元素和复杂元素的代码。(4分)

4. 在客户端构造出树形数据并输出。(4分) 提示:程序运行后,输出信息应为 Dir1 File1 Dir2 File3 Dir3 File4

答案:

1. Public class PrintSpooler{

Private static PrintSpooler instance; //(2分)

Private PrintSpooler(){} //(2分)

Public static PrintSpooler getInstance(){ //(2分)

If(instance == null){

Synchronized(PrintSpooler.Class){ //(synchronized关键字,2分)

If(instance == null)

Instance = new PrintSpooler();//2分

} }

return instance; }

}或者

2. Public class PrintSpooler{

Private static PrintSpooler instance = new PrintSpooler(); //(4分) Private PrintSpooler(){ //(2分) }

Public static PrintSpooler getInstance(){ //(2分)

return instance; //(2分) } } 2.

Public class FruitFactory{ //6分,要有静态方法,返回fruit

Public static Fruit creatFruit(String type){

Fruit ft = null ;

If(type.equals(“Apple”)

Ft = new Apple();

Else if(type.equals(“Strawberry”)

Ft = new Strawberry(); Else if(type.equals(“Grape”)

Ft = new Grape(); Return ft;

}

}

Public class MainUI{ //(4分)

Public static void main(string[] str){

Fruit ft = FruitFactory.creatFruit(“Apple”); ft.plant(); Ft.grow(); Ft.harvest(); } }

3.

1)类图,类名不限,但必须将抽象的概念,以及Composite和Component之间的关系用正确的连线表示。

5. 简单元素、复杂元素(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 list = new ArrayList(); public Composite(String name){ this.name = name; }


软件体系结构与设计模式期末复习(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:新教材苏教版二年级下册第六单元两三位数的加法和减法

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: