抽象工厂模式(Abstract Factory Pattern)

Posted by Jfson on 2017-07-24

抽象工厂模式相对于工厂模式来说,最大的不同是:将工厂的共同特征抽取出来,提供一个生成工厂类。

  • 1.举例制作果汁,首先提供两个接口类,果汁的种类和加冰多少。
1
2
3
4
5
6
7
public interface MakeIce {
void putIce();
}
public interface MakeJuice {
void juicing();
}
  • 2.提供水果类和冰多少的类,这里方便起见,我们都写成内部类。
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
// 水果类
public class Fruits {
static final String TAG = Fruits.class.getSimpleName();
public class Apple implements MakeJuice {
@Override
public void juicing() {
Log.d(TAG, "Apple juice");
}
}
public class Mango implements MakeJuice {
@Override
public void juicing() {
Log.d(TAG, "Mango juice");
}
}
public class Orange implements MakeJuice {
@Override
public void juicing() {
Log.d(TAG, "Orange juice");
}
}
}
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
// 冰类
public class Ice {
static final String TAG = Ice.class.getSimpleName();
public class LittleIce implements MakeIce {
@Override
public void putIce() {
Log.d(TAG, "little ice");
}
}
public class MoreIce implements MakeIce {
@Override
public void putIce() {
Log.d(TAG, "More ice");
}
}
public class NoeIce implements MakeIce {
@Override
public void putIce() {
Log.d(TAG, "No ice");
}
}
}
  • 3.对于水果类,生成一个水果类工厂,获取一种水果对应一种果汁。对于冰类,生成一个冰类工厂。那么两个工厂可以提取公同的抽象方法出来。

  • 3.1 所有工厂的共同的抽象方法

1
2
3
4
public abstract class FruitsFactorysAbstract {
abstract MakeJuice getJuice(String want);
abstract MakeIce getIce(String want);
}
  • 3.2 果汁工厂
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
public class FruitsAbstractFactory extends FruitsFactorysAbstract {
@Override
public MakeJuice getJuice(String want) {
MakeJuice ret;
switch (want) {
case "apple":
ret = new Fruits().new Apple();
break;
case "orange":
ret = new Fruits().new Orange();
break;
case "mango":
ret = new Fruits().new Mango();
break;
default:
ret = new Fruits().new Apple();
break;
}
return ret;
}
@Override
public MakeIce getIce(String want) {
return null;
}
}
  • 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
public class IceFactoryAbstract extends FruitsFactorysAbstract {
MakeIce ret;
@Override
public MakeJuice getJuice(String want) {
return null;
}
@Override
public MakeIce getIce(String want) {
switch (want) {
case "little":
ret = new Ice().new LittleIce();
break;
case "more":
ret = new Ice().new MoreIce();
break;
case "no":
ret = new Ice().new NoeIce();
break;
}
return ret;
}
}
  • 提供一个生成工厂类(抽象工厂模式跟工厂模式最大的区别)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class FactoryProducer {
static FruitsFactorysAbstract ret;
public static FruitsFactorysAbstract getFactory(String what) {
switch (what) {
case "Fruits":
ret = new FruitsFactory();
break;
case "Ice":
ret = new IceFactory();
break;
default:
break;
}
return ret;
}
}
  • 最后,获取到对应工厂后,调用相应get即可。
1
2
3
4
5
6
7
FruitsFactorysAbstract fruits = FactoryProducer.getFactory("Fruits");
MakeJuice orange = fruits.getJuice("orange");
Log.d("orange",orange.toString());
FruitsFactorysAbstract ice = FactoryProducer.getFactory("ice");
MakeIce makeIce= ice.getIce("orange");
Log.d("orange",makeIce.toString());

pv UV: