设计模式-工厂模式

设计模式实验设计

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

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

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

1
2
3
4
5
package 工厂模式;
// 实现接口 方便下面的圆形 方形 长方形使用
public interface Shape {
void draw();
}

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

1
2
3
4
5
6
7
8
9
10
11
package 工厂模式;

public class Circle implements Shape{

@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("Inside Circle::draw() method.");
}

}
1
2
3
4
5
6
7
8
9
10
11
package 工厂模式;

public class Square implements Shape{

@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("Inside Square::draw() method.");
}

}
1
2
3
4
5
6
7
8
9
10
11
package 工厂模式;

public class Rectangle implements Shape {

@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("Inside Rectangle::draw() method.");
}

}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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来调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.
我在LuxeryCarInsurance.java 中实现了autoInsurance 的接口 在LuxeryCarPolicy.java中实现了相应的接口

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14

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

1
2
3
4
5
6

public class LuxeryCarPolicy implements PolicyProducer {
public AutoInsurance getInsurObj() {
return new LuxeryCarInsurance();
}
}

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



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