1、2)重构过程中所使用的面向对象设计原则及简要说明:开闭原则:创建新图形只要新加入图形工厂和对应图形类,不修改源代码。依赖倒转原则:针对接口编程。单一职责原则:每个工厂只生产对应图形。2. 某销售管理系统支持多种支付方式,如现金支付、行用卡支付和代金券支付等,我们可能会像下面这么写,考虑用简单工厂模式对其进行重构。1)类图2)实现代码:public class Client public static void main(String args) IpayFactory iFactory = new IpayFactory(); Ipaymethon paymethon = iFactory.c
2、reatePaymethon(cash); if (paymethon != null) paymethon.pay(); else System.out.println(error public class IpayFactory public Ipaymethon createPaymethon(String paymethon) if (paymethon.equalsIgnoreCase() return new Cash(); else if (paymethon.equalsIgnoreCase(creditcard return new CreditCard();voucher
3、return new Voucher(); else return null;public interface Ipaymethon public void pay();public class Cash implements Ipaymethon public void pay() System.out.println(Cash paypublic class CreditCard implements Ipaymethon CreditCard paypublic class Voucher implements Ipaymethon Voucher pay3)实现结果:3使用简单工厂模式
4、设计一个可以创建不同几何形状(Shape),如圆形(Circle)、矩形(Rectangle)和三角形(Triangle)等的绘图工具类,每个几何图形均具有绘制Draw()和擦除Erase()两个方法,要求在绘制不支持的几何图形时,抛出一个UnsupportedShapeException异常,绘制类图并编程模拟实现。1)类图public interface Shape public void draw(); public void erase();public class Circle implements Shape public void draw()drawCircle public
5、void erase()erasepublic class Rectangle implements Shape Rectanglepublic class Triangle implements Shape Trianglepublic class ShapeFactory public static Shape produceShape(String shape) throws UnsupportedShapeException if (shape.equals( return new Circle(); else if (shape.equals( return new Triangle
6、(); return new Rectangle(); else throw new UnsupportedShapeException();public class UnsupportedShapeException extends Exception public UnsupportedShapeException () System.out.println(绘制图形异常,请确认输入图形。 public class Client ShapeFactory shapeFactory=new ShapeFactory(); try shapeFactory.produceShape().dra
7、w();).erase(); catch (UnsupportedShapeException e) e.printStackTrace();4. 现需要设计一个程序来读取多种不同类型的图片格式,针对每一种图片格式都设计一个图片读取器(ImageReader),如GIF图片读取器(GifReader)用于读取GIF格式的图片、JPG图片读取器(JpgReader)用于读取JPG格式的图片。图片读取器对象通过图片读取器工厂ImageReaderFactory来创建,ImageReaderFactory是一个抽象类,用于定义创建图片读取器的工厂方法,其子类GifReaderFactory和JpgR
8、eaderFactory用于创建具体的图片读取器对象。试使用工厂方法模式设计该程序,绘制类图并编程模拟实现。需充分考虑系统的灵活性和可扩展性。 JpgReaderFactory jFactory = new JpgReaderFactory(); jFactory.produceImageReader().readimage(); GifReaderFactory gFactory = new GifReaderFactory(); gFactory.produceImageReader().readimage();public interface ImageReader public voi
9、d readimage();public class GifReader implements ImageReader public void readimage()Read gif image.public class JpgReader implements ImageReader jpg readerRead jpg image.public abstract class ImageReaderFactory public abstract ImageReader produceImageReader();public class GifReaderFactory extends Ima
10、geReaderFactory public ImageReader produceImageReader()gif reader return new GifReader();public class JpgReaderFactory extends ImageReaderFactory return new JpgReader();5. 有一个OEM制造商代理做HP笔记本电脑(Laptop),后来该制造商得到了更多的品牌笔记本电脑的订单Acer,Lenovo,Dell,该OEM商发现,如果一次同时做很多个牌子的本本,有些不利于管理。利用工厂模式改善设计,绘制类图并编程模拟实现。 publi
11、c static void main(String args) IFactory lf = new HpFactory(); Laptop tp = lf.createLaptop(); tp.show(); lf = new AcerFactory(); tp = lf.createLaptop(); lf = new LenovoFactory(); lf = new DellFactory();public interface IFactory public Laptop createLaptop();public class AcerFactory implements IFactor
12、y public Laptop createLaptop() return new AcerLaptop();public class DellFactory implements IFactory return new DellLaptop();public class HpFactory implements IFactory return new HpLaptop();public class LenovoFactory implements IFactory return new LenovoLaptop();public abstract class Laptop public vo
13、id show();public class AcerLaptop extends Laptop public void show()AcerLaptoppublic class DellLaptop extends Laptop DellLaptoppublic class HpLaptop extends Laptop HpLaptoppublic class LenovoLaptop extends Laptop LenovoLaptop6. 某软件公司欲开发一套界面皮肤库,可以对桌面软件进行界面美化。不同的皮肤将提供视觉效果不同的按钮、文本框、组合框等界面元素,其结构如下图所示:该皮肤
14、库需要具备良好的灵活性和可扩展性,用户可以自由选择不同的皮肤,开发人员可以在不修改既有代码的基础上增加新的皮肤。试使用抽象工厂模式设计该皮肤库,绘制类图并编程模拟实现。 SpringSkinFactory skinFactory = new SpringSkinFactory(); skinFactory.createButton().action(); skinFactory.createTextbox().action(); skinFactory.createCombobox().action();public interface SkinFactory public AbstractB
15、utton createButton(); public AbstractTextbox createTextbox(); public AbstractCombobox createCombobox();public class SpringSkinFactory implements SkinFactory public AbstractButton createButton()生成 green button return new GreenButton(); public AbstractTextbox createTextbox()生成 green textbox return new
16、 GreenTextbox(); public AbstractCombobox createCombobox()生成 green combobox return new GreenCombobox();public class SummerSkinFactory implements SkinFactory 生成 blue button return new BlueButton();生成 blue textbox return new BlueTextbox();生成 blue combobox return new BlueCombobox();public interface Abst
17、ractButton public void action();public class GreenButton implements AbstractButton Override public void action() Green buttonpublic class BlueButton implements AbstractButton Blue buttonpublic interface AbstractTextbox public class GreenTextbox implements AbstractTextbox Green Textboxpublic class Bl
18、ueTextbox implements AbstractTextbox Blue Textboxpublic interface AbstractCombobox public class GreenCombobox implements AbstractCombobox Green Comboboxpublic class BlueCombobox implements AbstractCombobox Blu Combobox7. 麦当劳(McDonalds)和肯德基(KFC)快餐店都经营汉堡(Hamburg)和可乐(Cola),用控制台应用程序实现这两个快餐店经营产品的抽象工厂模式,并
19、绘制该模式的UML图。 Hamburg h; Cola c; AbstractFactory af = new MDNFactory(); h = af.createHamburg(); c = af.createCola(); h.getHumburg(); c.getCola(); af = new KDJFactory();public interface AbstractFactory public Hamburg createHamburg(); public Cola createCola();public class KDJFactory implements AbstractFactory public Hamburg createHamburg() return new KDJHamburg(); public Cola createCola() return new KDJCola();publ
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2