您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 其它文档 > 3.简单工厂模式-工厂方法模式答案
(2)简单工厂模式使用简单工厂模式设计一个可以创建不同几何形状(Shape)的绘图工具类,如可创建圆形(Circle)、方形(Rectangle)和三角形(Triangle)对象,每个几何图形都要有绘制draw()和擦除erase()两个方法,要求在绘制不支持的几何图形时,提示一个UnsupportedShapeException,绘制类图并编程实现。ShapeFactory+produceShape(StringshapeName):ShapeCircle++ImplementImplementdraw()erase():void:voidTriangle++ImplementImplementdraw()erase():void:voidRectangle++ImplementImplementdraw()erase():void:voidShape++draw()erase():void:void//抽象图形interfaceShape{voiddraw();voiderase();}classCircleimplementsShape{publicvoiddraw(){System.out.println(Circledrawing!);}publicvoiderase(){System.out.println(Circleeraseing!);}}classRectangleimplementsShape{publicvoiddraw(){System.out.println(Rectangledrawing!);}publicvoiderase(){System.out.println(Rectangleeraseing!);}}classTriangleimplementsShape{publicvoiddraw(){System.out.println(Triangledrawing!);}publicvoiderase(){System.out.println(Triangleeraseing!);}}//工厂classShapeFactory{//工厂方法factorymethodpublicstaticShapeproduceShape(StringshapeName)throwsUnsupportedShapeException{if(shapeName.equalsIgnoreCase(circle)){returnnewCircle();}elseif(shapeName.equalsIgnoreCase(rectangle)){returnnewRectangle();}elseif(shapeName.equalsIgnoreCase(triangle)){returnnewTriangle();}else{thrownewUnsupportedShapeException();}}}classUnsupportedShapeExceptionextendsException{publicStringtoString(){return绘制不支持该几何图形!;}}classShapeClient{publicstaticvoidmain(Stringa[]){Shapecircle=null;try{circle=ShapeFactory.produceShape(circle);}catch(UnsupportedShapeExceptione){e.printStackTrace();}circle.draw();circle.erase();Shapetriangle=null;try{triangle=ShapeFactory.produceShape(triangle);}catch(UnsupportedShapeExceptione){e.printStackTrace();}triangle.draw();triangle.erase();}}(3)工厂方法模式海尔工厂(Haier)生产海尔空调(HaierAirCondition),美的工厂(Midea)生产美的空调(MideaAirCondition)。使用工厂方法模式描述该场景,绘制类图并编程实现。HaierAirCondition+Implementwork():voidMideaAirCondition+Implementwork():voidHaierAirConditionFactory+ImplementproduceAirCondition():AirConditionMideaAirConditionFactory+ImplementproduceAirCondition():AirConditionAirCondition+work():voidAirConditionFactory+produceAirCondition():AirConditioninterfaceAirCondition{voidwork();}interfaceAirConditionFactory{AirConditionproduceAirCondition();}classHaierAirConditionimplementsAirCondition{publicvoidwork(){System.out.println(HaierAirConditionworking!);}}classMideaAirConditionimplementsAirCondition{publicvoidwork(){System.out.println(MideaAirConditionworking!);}}classHaierAirConditionFactoryimplementsAirConditionFactory{publicHaierAirConditionproduceAirCondition(){System.out.println(HaierAirConditionproduced!);returnnewHaierAirCondition();}}classMideaAirConditionFactoryimplementsAirConditionFactory{publicMideaAirConditionproduceAirCondition(){System.out.println(MideaAirConditionproduced!);returnnewMideaAirCondition();}}classAirConditionClient{publicstaticvoidmain(Stringa[]){AirConditionFactorycf;AirConditionairCondition;cf=(AirConditionFactory)XMLUtil.getBean();//这句代码中调用XML配置文件中的getBean函数,实现从XML配置文件中读取类的名字,创建该类的对象,最后返回给调用getBean()方法的Java程序中。airCondition=cf.produceAirCondition();airCondition.work();}}
本文标题:3.简单工厂模式-工厂方法模式答案
链接地址:https://www.777doc.com/doc-5272978 .html