收藏 分销(赏)

软件设计模式与体系结构样本.doc

上传人:精*** 文档编号:3902953 上传时间:2024-07-23 格式:DOC 页数:38 大小:292KB
下载 相关 举报
软件设计模式与体系结构样本.doc_第1页
第1页 / 共38页
软件设计模式与体系结构样本.doc_第2页
第2页 / 共38页
软件设计模式与体系结构样本.doc_第3页
第3页 / 共38页
软件设计模式与体系结构样本.doc_第4页
第4页 / 共38页
软件设计模式与体系结构样本.doc_第5页
第5页 / 共38页
点击查看更多>>
资源描述

1、 计算机科学与技术学院软件设计模式与体系结构课程作业( 一) ( / 第二学期) 学生姓名: 雷 君学生专业: 软件工程学生班级: 142601学生学号: 26170108指导教师: 王 飞 目 录实验一1工厂方法模式汽车保险1抽象工厂方法模式房屋信息3实验二6组合模式空军指挥系统6适配器模式客户信息验证8实验三11桥接模式几何立体体积11访问者模式计算机部件销售软件14实验四17策略模式整数排序17状态模式交通信号灯19实验五21MVC软件体系结构21实验一工厂方法模式汽车保险【实验内容】 在例2.3的汽车保险管理应用程序实例上添加一个名为LuxuryCarInsurance的类, 而且,

2、该类要与其它的类一样能执行相应的功能。【添加代码】1、 添加LuxuryCarInsurance类: public class LuxuryCarInsurance implements AutoInsurance private String description; public String getInsuranceDescription() description = LuxuryCarInsurance: nnLuxuryCarInsurance coverage pays for medical bills + lost wages, rehabilitation, treatme

3、nt and/or + funeral costs for anyone injured or killed + by your car. Such coverage will also pay for + pain and suffering damages when a third + party successfully sues. ; return description; 2、 添加LuxuryCarPolicyProducer类: public class LuxuryCarPolicyProducer implements PolicyProducer public AutoIn

4、surance getPolicyObj() /Fruit factory() return new LuxuryCarInsurance(); 3、 添加GUI: ( 1) public static final String LUXURYCAR = LuxuryCar Insurance; ( 2) cmbInsuranceType.addItem(LUXURYCAR); ( 3) if (type.equals(LUXURYCAR) pp=new LuxuryCarPolicyProducer(); 【实验结果】 【实验小结】使用工厂方法访问而且初始化合适的类的对象, 简化了应用程序,

5、应用程序本身不再含有大量的条件语句判定何时选取哪个类。其次, 工厂方法实现了一些特殊的某个类的机制, 特别是层次结构不同的类需要不同的初始化方法的时候。抽象工厂方法模式房屋信息【实验内容】 在例2.4中设计而且实现了豪华( Super) 和中等( Medum) 别墅( House) 和公寓( Condo) 的查询。要求在该设计的基础上, 增加一个新的类SemiDetacher( 半独立式楼宇) , 而且编写代码实现相应的查询功能。【添加代码】1、 添加SemiDetacher类: public interface SemiDetacher public String getSemiDetach

6、erInfo(); public String getSemiDetacherFeatures(); 2、 添加SuperSemiDetacher类: public class SuperSemiDetacher implements SemiDetacher private String name; public SuperSemiDetacher(String cName) name = cName; public String getSemiDetacherInfo() return superSemiDetacher.html; public String getSemiDetache

7、rFeatures() return Super SemiDetacher ; 3、 添加MediumSemiDetacher类: public class MediumSemiDetacher implements SemiDetacher private String name; public MediumSemiDetacher(String cName) name = cName; public String getSemiDetacherInfo() return MediumSemiDetacher.html; public String getSemiDetacherFeatur

8、es()return Medium SemiDetacher ; 4、 添加BuildingFactory: public abstract SemiDetacher getSemiDetacher();5、 添加MediumBuildingFactory: public SemiDetacher getSemiDetacher() return new MediumSemiDetacher(Medium SemiDetacher); 6、 添加SuperBuildingFactory: public SemiDetacher getSemiDetacher() return new Supe

9、rSemiDetacher(Super SemiDetacher); 7、 添加GUI: ( 1) public static final String SEMIDETACHER = SemiDetacher; ( 2) cmbHouseType.addItem(SEMIDETACHER); ( 3) if (type.equals(AbstractFactoryGUI.SEMIDETACHER) SemiDetacher cd = bf.getSemiDetacher(); String fileNm = cd.getSemiDetacherInfo(); putHouseInfoToScr

10、een(fileNm); 【实验结果】 【实验小结】 当客户对象要从一个相关的产品组中创立一个对象, 而没有必要知道到底要创立哪个对象时, 能够使用抽象工厂模式。如果不使用抽象工厂模式, 创立对象的条件语句将会出现在客户程序的许多地方, 程序的克维护性差。抽象工厂模式帮助程序员避免了以上所述的重复的、 复杂的条件语句, 提供必要的创立对象的接口。实验二组合模式空军指挥系统【实验内容】 在例3.3的设计中, 添加一个空军大队( Wing) 类, 该类与Squadron、 Group类是平行的, 因此应该继承了AirUnit类。该类的写法与Squadron或者Group类是类似的, 所不同的是一个

11、Wing有216中类型的飞机。【添加代码】1、 添加Wing类: public class Wing extends AirUnit public static final String FEATURES = A Wing with 216 aircrafts; Airforce fighters = new Airforce162; Airforce bombers = new Airforce18; Airforce transporters= new Airforce18; Airforce eAircrafts = new Airforce18; public Wing() for(i

12、nt k=0;k162;k+) / need 162 fighters for(int k=0;k18;k+) / need 18 bombers for(int k=0;k18;k+) / need 18 transporters for(int k=0;k=A)&(nStr.charAt(0) =Z)| (nStr.charAt(0) =a)&(nStr.charAt(0) =z) & (len=5) ) for(int m=0; m=0&nStr.charAt(m)=9) b+; if(m=0)&(Character.isLetter(nStr.charAt(m)=false) isVa

13、lid=false; if(a!=1) isValid=false; if(b=0) isValid=false; return isValid; else return false; 2、 添加CusInfoValidator: public abstract boolean isValidEmailAddr(String EmailAddr);3、 添加GUI: ( 1) private JTextField txtCustomerName, txtAddress,txtZip,txtCellPhone,txtSSN,txtEmailAddr; ( 2) private JLabel lb

14、lCustomerName, lblAddress,lblZip, lblCellphone, lblSSN, lblEmailAddr; ( 3) txtEmailAddr=new JTextField(20); ( 4) lblEmailAddr= new JLabel(EmailAddr :); ( 5) UIPanel.add(lblEmailAddr); UIPanel.add(txtEmailAddr); ( 6) gridbag.setConstraints(lblEmailAddr, gbc); gbc.gridx = 1; gbc.gridy = 5; gridbag.set

15、Constraints(txtEmailAddr, gbc); gbc.gridx = 0; gbc.gridy = 6; ( 7) public String getEmailAddr() return txtEmailAddr.getText(); ( 8) String emailaddr = getEmailAddr(); ( 9) if(cusInfo.isValidEmailAddr(emailaddr)=false) dataTextArea.append(nWrong format of EmailAddr.); else dataTextArea.append(nCorrec

16、t format of EmailAddr.); 【实验结果】 【实验小结】 经过适配器, 客户端能够调用同一接口, 因而对客户端来说是透明的。这样做更简单、 更直接、 更紧凑; 复用了现存的类, 解决了现存类和复用环境要求不一致的问题; 将目标类和适配者类解耦, 经过引入一个适配器类重用现有的适配者类, 而无需修改原有代码; 一个对象适配器能够把多个不同的适配者类适配到同一个目标, 也就是说, 同一个适配器能够把适配者类和它的子类都适配到目标接口。对于对象适配器来说, 更换适配器的实现过程比较复杂。实验三桥接模式几何立体体积【实验内容】在例3.14中的设计实验层次类部分中, 添加Ellips

17、oid( 椭球) 类, 而且实现针对椭球体积的计算。【添加代码】1、 添加椭球类: public class Ellipsoid implements GeoForm private double aRadius; private double bRadius; private double cRadius; public Ellipsoid (double aRadius, double bRadius, double cRadius) this.aRadius = aRadius; this.bRadius = bRadius; this.cRadius = cRadius; public

18、 double computeVolume() double volume = 1.3333333*3.1415926* aRadius* bRadius* cRadius; return volume; 2、 添加GUI: ( 1) private JTextField txtEllipsoidRadius_a; private JTextField txtEllipsoidRadius_b; private JTextField txtEllipsoidRadius_c; ( 2) public static final String ELLIPSOID = Ellipsoid;( 3)

19、cmbGeoForm.addItem(ELLIPSOID);( 4) else if(selection.equals(ELLIPSOID) ) String a_radius = txtEllipsoidRadius_a.getText(); String b_radius = txtEllipsoidRadius_b.getText(); String c_radius = txtEllipsoidRadius_c.getText(); double a = Double.valueOf(a_radius); double b =Double.valueOf(b_radius); doub

20、le c = Double.valueOf(c_radius); form = new Ellipsoid(a, b, c);( 5) else if(selection.equals(ELLIPSOID) ) displayNewGUI( getTypePanel(ELLIPSOID);( 6) else if(type.equals(ELLIPSOID) JLabel lblRadius_a = new JLabel(Input Radius a); JLabel lblRadius_b = new JLabel(Input Radius b); JLabel lblRadius_c =

21、new JLabel(Input Radius c); txtEllipsoidRadius_a = new JTextField(8); txtEllipsoidRadius_b = new JTextField(8); txtEllipsoidRadius_c = new JTextField(8); GridBagLayout gridbag = new GridBagLayout(); typePanel.setLayout(gridbag); GridBagConstraints gbc = new GridBagConstraints(); typePanel.add(lblRad

22、ius_a); typePanel.add(lblRadius_b); typePanel.add(lblRadius_c); typePanel.add(txtEllipsoidRadius_a); typePanel.add(txtEllipsoidRadius_b); typePanel.add(txtEllipsoidRadius_c); typePanel.add(lblMeasure); typePanel.add(cmbMeasure); gbc.insets.top = 5; gbc.insets.bottom = 5; gbc.insets.left = 1; gbc.ins

23、ets.right = 8; gbc.anchor = GridBagConstraints.WEST; gbc.gridx = 0; gbc.gridy = 0; gridbag.setConstraints(lblRadius_a, gbc); gbc.gridx = 1; gbc.gridy = 0; gridbag.setConstraints(txtEllipsoidRadius_a, gbc); gbc.gridx = 0; gbc.gridy = 1; gridbag.setConstraints(lblRadius_b, gbc); gbc.gridx = 1; gbc.gri

24、dy = 1; gridbag.setConstraints(txtEllipsoidRadius_b, gbc); gbc.gridx = 0; gbc.gridy = 2; gridbag.setConstraints(lblRadius_c, gbc); gbc.gridx = 1; gbc.gridy = 2; gridbag.setConstraints(txtEllipsoidRadius_c, gbc); gbc.gridx = 0; gbc.gridy = 3; gridbag.setConstraints(lblMeasure, gbc); gbc.gridx = 1; gb

25、c.gridy = 3; gridbag.setConstraints(cmbMeasure, gbc); 【实验结果】【实验小结】 经过这次实验我们大概理解了桥接模式, 经过关联”抽象层次类”与”具体层次类”这一桥梁, 将表示两个维度的层类( 数据结构) 粘贴在一起, 形成更大的数据结构, 而这种变化又不会对现有的类产生影响, 这种思路的终极想法是将软件设计的抽象部分与实现部分分离, 使它们都能够独立的变化。访问者模式计算机部件销售软件【实验内容】 在例4.5的设计中添加一个类SoundBox。该类实现接口ComputerParts, 而且其它的计算机部件的类的结构类似。【添加代码】1、 添

26、加SoundBox类: public class SoundBox implements ComputerParts public static final String NAME = SoundBox;private final double PRICE = 127.00;public static final String FEATURES = SoundBox. X3K;public String getName()return NAME;public double getPrice() return PRICE; public String getDescription() retur

27、n FEATURES;public void accept(Visitor v) System.out.println(SoundBox has been visited.); v.visitSoundBox (this); 2、 添加GUI: ( 1) String compParts= Case,Motherboard,Microprocessor,Memory, DriveController,VideoCard,Fan,PowerSupply, HardDiskDrive,CDDrive,DVDDevice,Monitor, Keyboard,Mouse,SoundBox,Assemb

28、ly,WholePC; ( 2) for(int k=11; k17; k+) add(0, k, cPartsk); ( 3) else if (source = cParts14) states14 = state; else if (source = cParts15) if(state = SELECTED) cParts1.setSelected(true); cParts8.setSelected(true); else if (state = DESELECTED) cParts1.setSelected(false); cParts8.setSelected(false); s

29、tates15=state; else if (source = cParts16) if(state = SELECTED) cParts0.setSelected(true); for(int k=11; k15; k+) cPartsk.setSelected(true); else if (state = DESELECTED) cParts0.setSelected(false); for(int k=11; k15; k+) cPartsk.setSelected(false); states16=state; ( 4) else if (m=14) & (states14 = S

30、ELECTED) part = new SoundBox(); msg.add(SoundBox); 3、 添加Visitor: public abstract void visitSoundBox (SoundBox e);4、 添加PriceVisitor: public void visitSoundBox (SoundBox e)price = e.getPrice();partsPrices.add(new Double(price);total += price; 5、 添加PartsInfoVisitor: public void visitSoundBox (SoundBox

31、e)partInfo = e.getDescription();allOders = allOders + n + partInfo;【实验结果】【实验小结】 经过此次实验让我们更加深刻的理解了访问者模式, 它在处理数据结构较稳定, 可是作用于其上的操作需要经常变化的问题时时非常有效的, 因此能够在不改变该结构体中的类的基础上定义一个新的操作, 这个实验就是如此, 我们只需要添加新的类, 然后调用accept(pv)方法就能够。实验四策略模式整数排序【实验内容】在例4.13的设计中的排序策略部分, 添加一个类BidirectionaBubbleSort, 进行”双向冒泡法排序”, 以便扩展该排

32、序系统的功能。为了实现扩展功能, 需要相应地修改Context类与客户图形界面类StrategyGUI。【添加代码】1、 添加BidirBubbleSort类: public class BidirBubbleSort implements SortAlgorithm public int sort(int nums,Context ct) ct.startExecution(); int j; int limit = nums.length; int st = -1; while(st limit) boolean flipped = false; st+; limit-; for(j = st; j numsj + 1) int T = numsj; numsj = numsj + 1; numsj + 1 = T; flipped = true; if(!flipped) ct.endExecution(); return nums; for(j = limit; -j = st;) if(numsj nu

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服