Skip to content

Commit dfd9179

Browse files
committed
add AbstractFactory
1 parent 3f40906 commit dfd9179

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+2443
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Compiled class file
22
*.class
33
*.pyc
4+
.idea
45

56
# Log file
67
*.log

abstract-factory/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 简介
2+
抽象工厂模式(Abstract Factory Pattern)是一个超级工厂,用来创建其他的工厂。
3+
工厂方法是一个具体工厂,用来创建对象,而抽象工厂则是用来创建工厂的类。
4+
5+
抽象工厂属于创建型模式,它为访问类提供一个创建一组相互依赖对象的接口,且访问类无须指定具体类就能得到同类下不同等级的对象的模式。在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
6+
7+
# 实现步骤
8+
1. 建立抽象工厂类,用于创建产品工厂类。
9+
2. 新建多个同步系列的具体产品类,均实现该产品接口类。
10+
3. 建立产品工厂类,继承该抽象工厂类,由产品工厂类创建产品对象。
11+
4. 想要获取具体产品对象时,先生成工厂,再实例化对象。
12+
13+
# 作用
14+
- 工厂的创建和调用解耦,便于不同系列产品之间的关联调用。
15+
- 屏蔽复杂的对象创建逻辑,交由统一的工厂方法,工厂本身也由工厂创建。
16+
- 采用统一的方式来实例化,还可以防止内存中实例对象不断增多。
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package src;
2+
3+
import src.vehicle.*;
4+
import src.shop.*;
5+
6+
/*
7+
意图:提供一个创建一系列相互依赖的接口,而无需指定它们具体的类。
8+
主要作用:主要解决接口选择的问题。在一个对象家族系统里面里面,抽象定义多个产品对象。
9+
何时使用:产品很多,形成产品系列,而系统只需要处理其中某一类的产品。
10+
如何解决:在一个产品系列里面,定义多个产品对象。由抽象类来负责创建产品工厂。
11+
*/
12+
public abstract class AbstractFactory {
13+
public abstract Vehicle getVehicle(VehicleType type);
14+
public abstract Shop getShop(String name);
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package src;
2+
3+
/*
4+
工厂创建者,用来创建不同的工厂
5+
*/
6+
public class FactoryCreator {
7+
public static AbstractFactory getFactory(String name) {
8+
switch (name) {
9+
// 汽车工厂
10+
case "vehicle":
11+
return new VehicleFactory();
12+
// 商店工厂
13+
case "shop":
14+
return new ShopFactory();
15+
default:
16+
return null;
17+
}
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package src;
2+
import src.vehicle.*;
3+
import src.shop.*;
4+
/*
5+
商店创建类,继承自抽象工厂
6+
*/
7+
public class ShopFactory extends AbstractFactory {
8+
@Override
9+
public Shop getShop(String name) {
10+
if (name.equals("AgencyShop")) {
11+
return new AgencyShop();
12+
} else if (name.equals("DirectSaleShop")) {
13+
return new DirectSaleShop();
14+
} else if (name.equals("SupermarketShop")) {
15+
return new SupermarketShop();
16+
} else {
17+
return null;
18+
}
19+
}
20+
21+
@Override
22+
public Vehicle getVehicle(VehicleType type) {
23+
return null;
24+
}
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package src;
2+
import src.vehicle.*;
3+
import src.shop.*;
4+
/*
5+
意图:定义基础接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。
6+
主要作用:解决接口选择的问题。统一实例化,用接口来实例化类更加抽象。
7+
何时使用:大量构造函数的地方,调用了大量的new来实例化类。
8+
如何解决:让其子类实现工厂接口,返回的也是一个抽象的产品。
9+
*/
10+
public class VehicleFactory extends AbstractFactory {
11+
@Override
12+
public Vehicle getVehicle(VehicleType type) {
13+
switch (type) {
14+
case BUS:
15+
return new Bus();
16+
case CAR:
17+
return new Car();
18+
case MOTORCYCLE:
19+
return new Motorcycle();
20+
case VAN:
21+
return new Van();
22+
default:
23+
return null;
24+
}
25+
26+
}
27+
28+
@Override
29+
public Shop getShop(String name) {
30+
return null;
31+
}
32+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package src.shop;
2+
3+
public class AgencyShop implements Shop {
4+
@Override
5+
public void greetings() {
6+
System.out.println("AgencyShop::greetings().");
7+
}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package src.shop;
2+
public class DirectSaleShop implements Shop {
3+
@Override
4+
public void greetings() {
5+
System.out.println("DirectSaleShop::greetings().");
6+
}
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package src.shop;
2+
public interface Shop {
3+
void greetings();
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package src.shop;
2+
public class SupermarketShop implements Shop {
3+
@Override
4+
public void greetings() {
5+
System.out.println("SupermarketShop::greetings().");
6+
}
7+
}

0 commit comments

Comments
 (0)