软件设计与体系结构复习整理题目及答案(7)

2019-08-17 13:02

} }

3.抽象工厂(AbstractFactory):ClothesFactory.java public abstract class ClothesFactory{

public abstract UpperClothes createUpperClothes(int chestSize,int height); public abstract Trousers createTrousers(int waistSize,int height); }

4.具体工厂(ConcreteFactory): BeijingClothesFactory.java

public class BeijingClothesFactory extends ClothesFactory {

public UpperClothes createUpperClothes(int chestSize,int height){

return new WesternUpperClothes(\北京牌西服上衣\ }

public Trousers createTrousers(int waistSize,int height){

return new WesternTrousers(\北京牌西服裤子\ } }

ShanghaiClothesFactory.java

public class ShanghaiClothesFactory extends ClothesFactory {

public UpperClothes createUpperClothes(int chestSize,int height){

return new WesternUpperClothes(\上海牌牛仔上衣\ }

public Trousers createTrousers(int waistSize,int height){

return new WesternTrousers(\上海牌牛仔裤\ } }

5.应用_1: Shop.java public class Shop{

UpperClothes cloth; Trousers trouser;

public void giveSuit(ClothesFactory factory,int chestSize,int waistSize,int height){ cloth=factory.createUpperClothes(chestSize,height); trouser=factory.createTrousers(waistSize,height); showMess(); }

private void showMess(){

System.out.println(\套装信息>\

System.out.println(cloth.getName()+\

System.out.print(\胸围:\ System.out.println(\身高:\ System.out.println(trouser.getName()+\

System.out.print(\腰围:\ System.out.println(\身高:\ }

}

5.应用_2: Application.java public class Application{

public static void main(String args[]){ Shop shop=new Shop();

ClothesFactory factory=new BeijingClothesFactory(); shop.giveSuit(factory,110,82,170);

factory=new ShanghaiClothesFactory(); shop.giveSuit(factory,120,88,180);}

}

抽象工厂模式的优点 :

抽象工厂模式可以为用户创建一系列相关的对象,使得用户和创建这些对象的类脱耦。 使用抽象工厂模式可以方便的为用户配置一系列对象。用户使用不同的具体工厂就能得到一组相关的对象,同时也能避免用户混用不同系列中的对象。

在抽象工厂模式中,可以随时增加“具体工厂”为用户提供一组相关的对象。

组合模式:

模式的结构中包括三种角色: 抽象组件(Component)

Composite节点(Composite Node) Leaf节点(Leaf Node)

组合模式是关于怎样将对象形成树形结构来表现整体和部分的层次结构的成熟模式。使用组合模式,可以让用户以一致的方式处理个体对象和组合对象,组合模式的关键在于无论是个体对象还是组合对象都实现了相同的接口或都是同一个抽象类的子类。

1.抽象组件(Component) : MilitaryPerson.java import java.util.*;

public interface MilitaryPerson{

public void add(MilitaryPerson person) ; public void remove(MilitaryPerson person) ; public MilitaryPerson getChild(int index);

public Iterator getAllChildren() ; public boolean isLeaf(); public double getSalary();

public void setSalary(double salary); }

2. Composite节点(Composite Node): MilitaryOfficer.java import java.util.*;

public class MilitaryOfficer implements MilitaryPerson{ LinkedList list; String name; double salary;

MilitaryOfficer(String name,double salary){ this.name=name; this.salary=salary;

list=new LinkedList(); }

public void add(MilitaryPerson person) { list.add(person); }

public void remove(MilitaryPerson person){ list.remove(person); }

public MilitaryPerson getChild(int index) { return list.get(index); }

public Iterator getAllChildren() { return list.iterator(); }

public boolean isLeaf(){ return false; }

public double getSalary(){ return salary; }

public void setSalary(double salary){ this.salary=salary; }

}

3.Leaf节点(Leaf Node):MilitarySoldier.java import java.util.*;

public class MilitarySoldier implements MilitaryPerson{ double salary; String name;

MilitarySoldier(String name,double salary){ this.name=name; this.salary=salary; }

public void add(MilitaryPerson person) {} public void remove (MilitaryPerson person){} public MilitaryPerson getChild(int index) { return null; }

public Iterator getAllChildren() { return null; }

public boolean isLeaf(){ return true; }

public double getSalary(){ return salary; }

public void setSalary(double salary){ this.salary=salary; } }

4.应用_1:ComputerSalary.java import java.util.*;

public class ComputerSalary{

public static double computerSalary(MilitaryPerson person){ double sum=0;

if(person.isLeaf()==true){

sum=sum+person.getSalary(); }

if(person.isLeaf()==false){

sum=sum+person.getSalary();

Iterator iterator=person.getAllChildren(); while(iterator.hasNext()){

MilitaryPerson p= iterator.next(); sum=sum+computerSalary(p);; } }

return sum; } }

4.应用_2: Application.java public class Application{

public static void main(String args[]) {

MilitaryPerson 连长=new MilitaryOfficer(\连长\

MilitaryPerson 排长1=new MilitaryOfficer(\一排长\ MilitaryPerson 排长2=new MilitaryOfficer(\二排长\ MilitaryPerson 班长11=new MilitaryOfficer(\一班长\ MilitaryPerson 班长12=new MilitaryOfficer(\二班长\ MilitaryPerson 班长13=new MilitaryOfficer(\三班长\ MilitaryPerson 班长21=new MilitaryOfficer(\一班长\ MilitaryPerson 班长22=new MilitaryOfficer(\二班长\ MilitaryPerson 班长23=new MilitaryOfficer(\三班长\ MilitaryPerson 班长31=new MilitaryOfficer(\一班长\ MilitaryPerson 班长32=new MilitaryOfficer(\二班长\ MilitaryPerson 班长33=new MilitaryOfficer(\三班长\ MilitaryPerson []士兵=new MilitarySoldier[60]; for(int i=0;i<士兵.length;i++){

士兵[i]=new MilitarySoldier(\小兵\ }

连长.add(排长1); 连长.add(排长2); 排长1.add(班长11); 排长1.add(班长12); 排长1.add(班长13); 排长2.add(班长21); 排长2.add(班长22); 排长2.add(班长23); for(int i=0;i<=9;i++){

班长11.add(士兵[i]); 班长12.add(士兵[i+10]); 班长13.add(士兵[i+20]); 班长21.add(士兵[i+30]); 班长22.add(士兵[i+40]); 班长23.add(士兵[i+50]); 班长31.add(士兵[i+60]); 班长32.add(士兵[i+70]); 班长33.add(士兵[i+80]); }

System.out.println(\一排的军饷:\排长1)); System.out.println(\一班的军饷:\班长11)); System.out.println(\全连的军饷:\连长)); } }

———————————————————————————————————————

适配器模式


软件设计与体系结构复习整理题目及答案(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:Dwocbw冉万祥同志在2011年全州经济工作会议上的讲话

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

马上注册会员

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