1、面向对象系统分析和设计综合实验报告实验名称:实验3 设计模型实验1 学期:2017-2018学年 第二学期 一、实验目的1熟练使用面向对象设计原则对系统进行重构;2熟练使用面向对象编程语言(JAVA或C+)实现几种常见的创建型设计模式和行为型模式,包括简单工厂模式、工厂方法模式、抽象工厂模式和模板方法,理解每一种设计模式的模式动机,掌握模式结构,学习如何使用代码实现这些模式。二、实验要求1. 选择合适的面向对象设计原则对系统进行重构,正确无误地绘制重构之后的类图;2. 结合实例,正确无误地绘制简单工厂模式、工厂方法模式、抽象工厂模式和模板方法的模式结构图;3. 实现简单工厂模式、工厂方法模式、
2、抽象工厂模式和模板方法,代码运行正确无误。三、实验内容1在某图形库API中提供了多种矢量图模板,用户可以基于这些矢量图创建不同的显示图形,图形库设计人员设计的初始类图如下所示:在该图形库中,每个图形类(如Circle、Triangle等)的init()方法用于初始化所创建的图形, setColor()方法用于给图形设置边框颜色,fill()方法用于给图形设置填充颜色,setSize()方法用于设置图形的大小,display()方法用于显示图形。客户类(Client)在使用该图形库时发现存在如下问题:由于在创建窗口时每次只需要使用图形库中的一种图形,因此在更换图形时需要修改客户类源代码;在图形库
3、中增加并使用新的图形时需要修改客户类源代码;客户类在每次使用图形对象之前需要先创建图形对象,有些图形的创建过程较为复杂,导致客户类代码冗长且难以维护。现需要根据面向对象设计原则对该系统进行重构,要求如下:隔离图形的创建和使用,将图形的创建过程封装在专门的类中,客户类在使用图形时无须直接创建图形对象,甚至不需要关心具体图形类类名;客户类能够方便地更换图形或使用新增图形,无须针对具体图形类编程,符合开闭原则。绘制重构之后的类图并说明在重构过程中所运用的面向对象设计原则。1)重构之后的类图2)重构过程中所使用的面向对象设计原则及简要说明:开闭原则:创建新图形只要新加入图形工厂和对应图形类,不修改源代
4、码。依赖倒转原则:针对接口编程。单一职责原则:每个工厂只生产对应图形。2. 某销售管理系统支持多种支付方式,如现金支付、行用卡支付和代金券支付等,我们可能会像下面这么写,考虑用简单工厂模式对其进行重构。1)类图2)实现代码:public class Client public static void main(String args) IpayFactory iFactory = new IpayFactory(); Ipaymethon paymethon = iFactory.createPaymethon(cash); if (paymethon != null) paymethon.p
5、ay(); else System.out.println(error); public class IpayFactory public Ipaymethon createPaymethon(String paymethon) if (paymethon.equalsIgnoreCase(cash) return new Cash(); else if (paymethon.equalsIgnoreCase(creditcard) return new CreditCard(); if (paymethon.equalsIgnoreCase(voucher) return new Vouch
6、er(); else return null; public interface Ipaymethon public void pay();public class Cash implements Ipaymethon public void pay() System.out.println(Cash pay); public class CreditCard implements Ipaymethon public void pay() System.out.println(CreditCard pay); public class Voucher implements Ipaymethon
7、 public void pay() System.out.println(Voucher pay); 3)实现结果:3使用简单工厂模式设计一个可以创建不同几何形状(Shape),如圆形(Circle)、矩形(Rectangle)和三角形(Triangle)等的绘图工具类,每个几何图形均具有绘制Draw()和擦除Erase()两个方法,要求在绘制不支持的几何图形时,抛出一个UnsupportedShapeException异常,绘制类图并编程模拟实现。1)类图2)实现代码:public interface Shape public void draw(); public void erase(
8、);public class Circle implements Shape public void draw() System.out.println(drawCircle); public void erase() System.out.println(eraseCircle); public class Rectangle implements Shape public void draw() System.out.println(drawRectangle); public void erase() System.out.println(eraseRectangle); public
9、class Triangle implements Shape public void draw() System.out.println(drawTriangle); public void erase() System.out.println(eraseTriangle); public class ShapeFactory public static Shape produceShape(String shape) throws UnsupportedShapeException if (shape.equals(Circle) System.out.println(Circle); r
10、eturn new Circle(); else if (shape.equals(Triangle) System.out.println(Triangle); return new Triangle(); else if (shape.equals(Rectangle) System.out.println(Rectangle); return new Rectangle(); else throw new UnsupportedShapeException(); public class UnsupportedShapeException extends Exception public
11、 UnsupportedShapeException () System.out.println(绘制图形异常,请确认输入图形。); public class Client public static void main(String args) ShapeFactory shapeFactory=new ShapeFactory(); try shapeFactory.produceShape(Circle).draw(); shapeFactory.produceShape(Circle).erase(); catch (UnsupportedShapeException e) e.pri
12、ntStackTrace(); 3)实现结果:4. 现需要设计一个程序来读取多种不同类型的图片格式,针对每一种图片格式都设计一个图片读取器(ImageReader),如GIF图片读取器(GifReader)用于读取GIF格式的图片、JPG图片读取器(JpgReader)用于读取JPG格式的图片。图片读取器对象通过图片读取器工厂ImageReaderFactory来创建,ImageReaderFactory是一个抽象类,用于定义创建图片读取器的工厂方法,其子类GifReaderFactory和JpgReaderFactory用于创建具体的图片读取器对象。试使用工厂方法模式设计该程序,绘制类图并编
13、程模拟实现。需充分考虑系统的灵活性和可扩展性。1)类图2)实现代码:public class Client public static void main(String args) JpgReaderFactory jFactory = new JpgReaderFactory(); jFactory.produceImageReader().readimage(); GifReaderFactory gFactory = new GifReaderFactory(); gFactory.produceImageReader().readimage(); public interface Im
14、ageReader public void readimage();public class GifReader implements ImageReader public void readimage() System.out.println(Read gif image.); public class JpgReader implements ImageReader public void readimage() System.out.println(jpg reader); System.out.println(Read jpg image.); public abstract clas
15、s ImageReaderFactory public abstract ImageReader produceImageReader();public class GifReaderFactory extends ImageReaderFactory public ImageReader produceImageReader() System.out.println(gif reader); return new GifReader(); public class JpgReaderFactory extends ImageReaderFactory public ImageReader p
16、roduceImageReader() return new JpgReader(); 3)实现结果:5. 有一个OEM制造商代理做HP笔记本电脑(Laptop),后来该制造商得到了更多的品牌笔记本电脑的订单Acer,Lenovo,Dell,该OEM商发现,如果一次同时做很多个牌子的本本,有些不利于管理。利用工厂模式改善设计,绘制类图并编程模拟实现。1)类图2)实现代码:public class Client public static void main(String args) IFactory lf = new HpFactory(); Laptop tp = lf.createLapt
17、op(); tp.show(); lf = new AcerFactory(); tp = lf.createLaptop(); tp.show(); lf = new LenovoFactory(); tp = lf.createLaptop(); tp.show(); lf = new DellFactory(); tp = lf.createLaptop(); tp.show(); public interface IFactory public Laptop createLaptop();public class AcerFactory implements IFactory publ
18、ic Laptop createLaptop() return new AcerLaptop(); public class DellFactory implements IFactory public Laptop createLaptop() return new DellLaptop(); public class HpFactory implements IFactory public Laptop createLaptop() return new HpLaptop(); public class LenovoFactory implements IFactory public La
19、ptop createLaptop() return new LenovoLaptop(); public abstract class Laptop public void show();public class AcerLaptop extends Laptop public void show() System.out.println(AcerLaptop); public class DellLaptop extends Laptop public void show() System.out.println(DellLaptop); public class HpLaptop ext
20、ends Laptop public void show() System.out.println(HpLaptop); public class LenovoLaptop extends Laptop public void show() System.out.println(LenovoLaptop); 3)实现结果:6. 某软件公司欲开发一套界面皮肤库,可以对桌面软件进行界面美化。不同的皮肤将提供视觉效果不同的按钮、文本框、组合框等界面元素,其结构如下图所示:该皮肤库需要具备良好的灵活性和可扩展性,用户可以自由选择不同的皮肤,开发人员可以在不修改既有代码的基础上增加新的皮肤。试使用抽象工
21、厂模式设计该皮肤库,绘制类图并编程模拟实现。1)类图2)实现代码:public class Client public static void main(String args) SpringSkinFactory skinFactory = new SpringSkinFactory(); skinFactory.createButton().action(); skinFactory.createTextbox().action(); skinFactory.createCombobox().action(); public interface SkinFactory public Abs
22、tractButton createButton(); public AbstractTextbox createTextbox(); public AbstractCombobox createCombobox();public class SpringSkinFactory implements SkinFactory public AbstractButton createButton() System.out.println(生成 green button); return new GreenButton(); public AbstractTextbox createTextbox(
23、) System.out.println(生成 green textbox); return new GreenTextbox(); public AbstractCombobox createCombobox() System.out.println(生成 green combobox); return new GreenCombobox(); public class SummerSkinFactory implements SkinFactory public AbstractButton createButton() System.out.println(生成 blue button)
24、; return new BlueButton(); public AbstractTextbox createTextbox() System.out.println(生成 blue textbox); return new BlueTextbox(); public AbstractCombobox createCombobox() System.out.println(生成 blue combobox); return new BlueCombobox(); public interface AbstractButton public void action();public class
25、 GreenButton implements AbstractButton Override public void action() System.out.println(Green button); public class BlueButton implements AbstractButton Override public void action() System.out.println(Blue button); public interface AbstractTextbox public void action();public class GreenTextbox impl
26、ements AbstractTextbox Override public void action() System.out.println(Green Textbox); public class BlueTextbox implements AbstractTextbox Override public void action() System.out.println(Blue Textbox); public interface AbstractCombobox public void action();public class GreenCombobox implements Abs
27、tractCombobox public void action() System.out.println(Green Combobox); public class BlueCombobox implements AbstractCombobox public void action() System.out.println(Blu Combobox); 3)实现结果:7. 麦当劳(McDonalds)和肯德基(KFC)快餐店都经营汉堡(Hamburg)和可乐(Cola),用控制台应用程序实现这两个快餐店经营产品的抽象工厂模式,并绘制该模式的UML图。1)类图2)实现代码:public cl
28、ass Client public static void main(String args) Hamburg h; Cola c; AbstractFactory af = new MDNFactory(); h = af.createHamburg(); c = af.createCola(); h.getHumburg(); c.getCola(); af = new KDJFactory(); h = af.createHamburg(); c = af.createCola(); h.getHumburg(); c.getCola(); public interface AbstractFactory public Hamburg createHamburg(); public Cola createCola();public class KDJFactory implements AbstractFactory Override public Hamburg createHamburg() return new KDJHamburg(); Override public Cola createCola() return new KDJCola(); public clas
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1