博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式-工厂模式
阅读量:6605 次
发布时间:2019-06-24

本文共 3877 字,大约阅读时间需要 12 分钟。

原文地址:

设计模式实验设计

这一篇文章主要实现设计模式的工厂模式

首先实现菜鸟教程上面的demo 这个问题比较简单

首先设计内部的Shape类 这个类是为其他类实现接口

package 工厂模式;// 实现接口 方便下面的圆形 方形 长方形使用public interface Shape {	void draw();}复制代码

然后是实现下面的圆形 方形和长方形的类 直接实现接口即可

package 工厂模式;public class Circle implements Shape{	@Override	public void draw() {		// TODO Auto-generated method stub		System.out.println("Inside Circle::draw() method.");	}}复制代码
package 工厂模式;public class Square implements Shape{	@Override	public void draw() {		// TODO Auto-generated method stub		System.out.println("Inside Square::draw() method.");	}}复制代码
package 工厂模式;public class Rectangle implements Shape {	@Override	public void draw() {		// TODO Auto-generated method stub		System.out.println("Inside Rectangle::draw() method.");	} }复制代码

创建工厂类可以创建各个新的图形

package 工厂模式;// this is the shape Factory public class ShapeFactory {//	首先设置获取的方法	public Shape getShape(String shapeType) {		if (shapeType==null) {			return null;		}		if (shapeType=="CIRCLE") {			return new Circle();		}else if (shapeType=="RECTANGLE") {			return new Rectangle();		}else if(shapeType=="SQUARE")		{			return new Square();		}		return null;	}}复制代码

最后是实现相应的demo来调用

package 工厂模式;public class FactoryPatternDemo {public static void main(String[] args){	ShapeFactory shapeFactory=new ShapeFactory();	Shape shape1=shapeFactory.getShape("CIRCLE");	shape1.draw();	Shape  shape2=shapeFactory.getShape("RECTANGLE");	shape2.draw();	Shape shape3 = shapeFactory.getShape("SQUARE");	shape3.draw();}}复制代码

作业要求

作业要求:The following class diagram represents a design in factory method pattern to query the features of different types of auto insurances. See the source code for the implementation of the following class diagram.

  1. First in the following design, in the class hierarchy named AutoInsurance, add a class LuxeryCarInsurance
  2. The class LuxeryCarInsurance has the same interface and methods as every class in this class hierarchy
  3. You write a word description of the new insurance
  4. Then you need to add a sub class in the class hierarchy PolicyProducer to create object of class LuxeryCarInsurance
  5. Finally you need to modify the client class FactoryMethodGUI to allow the newly added insurance policy Luxery Car Insurance to be displayed as other insurance policies.

作业答案

1. Describe your finished homework, including i. What classes have been added to the existing class hierarchy? ii. The relationship between the existing classes and the newly added classes. iii. What code has been added to what class? etc.

2. Run the ClientGUI class and you choose “Body Injur Liability”, and then click on “show Info” button. List all the methods called in correct order here.

3. Draw your new class diagram here

4. After you add the new class LuxeryCarInsurance, what other classes have you changed? Does this design follow the open-closed principle? Why?

5. (Testing) Typical input and output from running your program

解答

这个题目挺简单的,这个题目属于基础题目,这个题目的做法是添加两个.java 文件 一个实现使用的政策接口来开启保险的文件,一个返回这个保险的描述。QAQ 添加代码如下 LuxeryCarInsurance.java

public class LuxeryCarInsurance implements AutoInsurance {	private String description;	   public String getInfo() {		   description = " LuxeryCarInsurance: \n\nBodily injury coverage pays for medical bills" +		                 " lost wages, rehabilitation, treatment 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;	   }}复制代码

这个题目主要是实现其中的接口比较简单 返回getInfo ()即可 LuxeryCarPolicy.java

public class LuxeryCarPolicy implements PolicyProducer {	 public AutoInsurance getInsurObj() {	      return new LuxeryCarInsurance();	   }}复制代码

这个个软件也比较的简单, implements PolicyProducer 是主要的问题,这就比较的简单了。没什么难度。 下面是GUI界面的选择

标红的是添加的部分 OK 结束

转载于:https://juejin.im/post/5c943fa6e51d453c7e141a5b

你可能感兴趣的文章
AWS SDK Python
查看>>
局域网通信工具 飞秋
查看>>
等差数列
查看>>
System Center Operation Manager 2012(八)DELL_MD_3200 Monintoring
查看>>
操作系统(四)---MS-DOS微软磁盘操作系统
查看>>
ajax提交表单
查看>>
FTP服务
查看>>
我的友情链接
查看>>
xcode8运行后后台打印网络相关的日志
查看>>
python语言中函数的传参与基本练习
查看>>
Java集合框架面试题
查看>>
Django1.7中注册、登陆、以及cookie的使用
查看>>
实现Lync Server 2010企业版前端服务器部署
查看>>
Java的主要就业方向
查看>>
关于使用mac进行文件远程操作
查看>>
Ubuntu Server 14.04 配置VNC
查看>>
我的友情链接
查看>>
Tcl命令操作实验-----(6)---case foreach
查看>>
Tcl命令操作实验-----(12) ---字典
查看>>
akka的Hello
查看>>