文章目录
  1. 1. 基本知识
  2. 2. 工厂模式

基本知识

开闭原则(Open Close Principle):

对扩展开放,对修改关闭。

在程序需要进行拓展的时候,不能去修改原有的代码,实现一个热插拔的效果。

简言之,是为了使程序的扩展性好,易于维护和升级。想要达到这样的效果,我们需要使用接口和抽象类,后面的具体设计中我们会提到这点。

创建型模式:

提供了一种在创建对象的同时隐藏创建逻辑的方式,而不是使用new的运算符直接实例化对象

目的:主要是用来创建对象,减少我们在使用某个对象时的new() 操作

工厂模式

为客户调用程序与具体的对象类型之间提供了一个解耦作用

简言之,应用程序不关心这个对象是怎么出来的,只关系如何使用这个对象,而且以后就算对象发生变化,那么也不需要修改用户应用

程序的代码。

提高了程序的可维护性和低耦合性。
  • 简单工厂模式(创建型模式)

    定义了一种工厂,该工厂负责所有的子类对象的创建,如果说我们的工厂要求能够满足增加新的对象时,我们必须修改工厂代码
    
    应用场景:适合对象类型不多,并且不会经常新增的情况下。
    
    涉及角色:
        1、具体工厂类角色:有一定的商业逻辑和判断逻辑。    
    
        2、抽象产品角色:它一般是具体产品继承的父类或者实现的接口。
    
        3、具体产品角色:工厂类所创建的对象就是此角色的实例。
    
  • 工厂方法模式(创建型模式)

    每个类型的对象都有与这个类型对应的工厂去创建,那么就算以后增加或者修改,只需要修改相应的工厂涉及的文件即可
    
    应用场景:适合对象类型较多,并且会经常新增的情况下。
    
    涉及角色:
        1、抽象工厂类角色:具体工厂角色必须实现的接口或者必须继承的父类。    
    
        2、具体工厂类角色
    
        3、抽象产品角色
    
        4、具体产品角色
    

简单工厂模式,不符合开闭原则,因为所有的对象的创建工作都放在一个工厂类的内部去完成,逻辑太复杂。

工厂方法模式,将每个工厂的职责进行了更细化,每个工厂只负责具体对象类型实例的创建。
  • 抽象工厂模式(创建型模式)
应用场景:支持动态的新增对象类型和新增工厂类型,实现多种依赖关系的对象或者组合关系的创建(产品族)

        简言之,具有相互依赖或者组合关系的对象类型实例的创建工作中使用。Eg:下面代码中绘制带颜色的图形。

涉及角色:
    1、抽象工厂类角色  

    2、具体工厂类角色

    3、抽象产品角色

    4、具体产品角色
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/****简单工厂****/
//1、创建产品接口
interface Shape{
public void draw();
}
//2、创建具体产品并实现产品接口
class Rectangle implements Shape{
public void draw(){
System.out.println("Rectangle implements Shape");
}
}
class Square implements Shape{
public void draw(){
System.out.println("Square implements Shape");
}
}
class Circle implements Shape{
public void draw(){
System.out.println("Circle implements Shape");
}
}
//3、创建工厂,通过工厂创建产品
class ShapeFactory{
static Shape getShape(String shapeName){
if(null == shapeName){
return null;
}
if("Rectangle".equals(shapeName)){
return new Rectangle();
}else if("Square".equals(shapeName)){
return new Square();
}else{
return new Circle();
}
}
}
/****工厂方法****/
//3、创建工厂接口
interface ShapeFactoryI{
public Shape getShapeFactoryDetail();
}
//4、创建具体工厂并实现工厂接口
class RectangleFactory implements ShapeFactoryI{
public Shape getShapeFactoryDetail(){
return new Rectangle();
}
}
class SquareFactory implements ShapeFactoryI{
public Shape getShapeFactoryDetail(){
return new Square();
}
}
class CircleFactory implements ShapeFactoryI{
public Shape getShapeFactoryDetail(){
return new Circle();
}
}
/****抽象工厂****/
/*
* 1、 定义两个产品接口
* 2、 定义具体产品
* 3、 定义抽象工厂
* 4、 定义具体工厂
*/
/*
* 1、创建产品1接口(上面的Shape接口)
* 创建产品2接口(这里的Color接口)
*/
interface Color{
//public Color getColor();
//well, in practice ,you should define like the above;
public void getColor();
}
/*
* 2、创建具体产品并实现产品1接口(上面的Shape接口)
* 创建具体产品并实现产品2接口(这里的Color接口)
*/
class Red implements Color{
public void getColor(){
System.out.println("Red implements Color");
}
}
class Green implements Color{
public void getColor(){
System.out.println("Green implements Color");
}
}
class Black implements Color{
public void getColor(){
System.out.println("Black implements Color");
}
}
/*
* 3、定义抽象工厂
*/
interface AbstractFactory{
public Color getColorDetail();
public Shape getShapeDetail();
}
/*
* 4、定义具体工厂
*/
class GreenSquareFactory implements AbstractFactory{
public Color getColorDetail(){
return new Green();
}
public Shape getShapeDetail(){
return new Square();
}
}
/********************测试程序******************/
public class MainTest{
public static void main(String[] args){
System.out.println("simple factory TEST");
Rectangle rectangle = (Rectangle)ShapeFactory.getShape("Rectangle");
rectangle.draw();
System.out.println("\nfactory pattern TEST");
ShapeFactoryI shapeFactory = new CircleFactory();
Circle circle = (Circle)shapeFactory.getShapeFactoryDetail();
circle.draw();
System.out.println("\nabstract factory TEST");
AbstractFactory abstractFactory = new GreenSquareFactory();
abstractFactory.getColorDetail().getColor();
abstractFactory.getShapeDetail().draw();
}
}
文章目录
  1. 1. 基本知识
  2. 2. 工厂模式