Skip to content

Commit ba1852a

Browse files
committed
add factory pattern
1 parent c10c1f8 commit ba1852a

26 files changed

+442
-0
lines changed

factory-pattern/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 简介
2+
简单工厂模式是一种创建型设计模式, 由工厂类提供统一的创建对象的方法,将对象实例化与使用分离开。
3+
4+
# 实现步骤
5+
1. 建立一个产品接口类或抽象类。
6+
2. 新建多个具体产品类,均实现该产品接口类。
7+
3. 建立一个工厂类,提供按类型实例化具体产品类的方法,返回抽象接口类型。
8+
4. 想要获取具体产品对象时,调用工厂方法来实例化,并由调用方来确定对象的类型。
9+
10+
# 作用
11+
- 对象创建和调用解耦,便于实例化时进行统一干预和修改。
12+
- 屏蔽复杂的对象创建逻辑,交由统一的工厂方法。
13+
- 采用统一的方式来实例化,还可以防止内存中实例对象不断增多。

factory-pattern/java/src/Bus.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package src;
2+
public class Bus implements Vehicle {
3+
4+
@Override
5+
public void run() {
6+
System.out.println("Bus::run().");
7+
}
8+
}

factory-pattern/java/src/Car.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package src;
2+
public class Car implements Vehicle {
3+
4+
@Override
5+
public void run() {
6+
System.out.println("Car::run().");
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package src;
2+
public class Motorcycle implements Vehicle {
3+
4+
@Override
5+
public void run() {
6+
System.out.println("Motorcycle::run().");
7+
}
8+
}

factory-pattern/java/src/Van.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package src;
2+
public class Van implements Vehicle {
3+
4+
@Override
5+
public void run() {
6+
System.out.println("Van::run().");
7+
}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package src;
2+
public interface Vehicle {
3+
void run();
4+
// void horn();
5+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package src;
2+
3+
/*
4+
意图:定义基础接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。
5+
主要作用:解决接口选择的问题。统一实例化,用接口来实例化类更加抽象。
6+
何时使用:大量构造函数的地方,调用了大量的new来实例化类。
7+
如何解决:让其子类实现工厂接口,返回的也是一个抽象的产品。
8+
*/
9+
public class VehicleFactory {
10+
public Vehicle getVehicle(VehicleType type) {
11+
switch (type) {
12+
case BUS:
13+
return new Bus();
14+
case CAR:
15+
return new Car();
16+
case MOTORCYCLE:
17+
return new Motorcycle();
18+
case VAN:
19+
return new Van();
20+
default:
21+
return null;
22+
}
23+
24+
}
25+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package src;
2+
3+
public enum VehicleType {
4+
BUS("bus", 1),
5+
CAR("car", 2),
6+
MOTORCYCLE("motorcycle", 3),
7+
VAN("van", 4);
8+
9+
private String name;
10+
private int index;
11+
12+
public static String getName(int index) {
13+
VehicleType values[] = VehicleType.values();
14+
for (int i = 0; i < values.length; i++) {
15+
if (values[i].getIndex() == index) {
16+
return values[i].getName();
17+
}
18+
}
19+
return null;
20+
}
21+
22+
private VehicleType(String name, int index) {
23+
this.name = name;
24+
this.index = index;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
public int getIndex() {
36+
return index;
37+
}
38+
39+
public void setIndex(int index) {
40+
this.index = index;
41+
}
42+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package test;
2+
3+
import src.*;
4+
5+
public class Test {
6+
7+
public static void start() {
8+
VehicleFactory vehicleFactory = new VehicleFactory();
9+
10+
// 获取Bus对象,并调用它的 run 方法
11+
Vehicle bus = vehicleFactory.getVehicle(VehicleType.BUS);
12+
bus.run();
13+
14+
// 获取Car对象,并调用它的 run 方法
15+
Vehicle car = vehicleFactory.getVehicle(VehicleType.CAR);
16+
// 类型转为Car
17+
Car car1 = (Car)car;
18+
car1.run();
19+
20+
// 获取Motorcycle对象,并调用它的 run 方法
21+
// 类型直接转为Motorcycle
22+
Motorcycle motorcycle = (Motorcycle)vehicleFactory.getVehicle(VehicleType.MOTORCYCLE);
23+
motorcycle.run();
24+
25+
// 获取Motorcycle对象,并调用它的 run 方法
26+
Vehicle van = vehicleFactory.getVehicle(VehicleType.VAN);
27+
van.run();
28+
29+
}
30+
public static void main(String[] args) {
31+
System.out.println("test start:");
32+
start();
33+
}
34+
35+
}
36+
37+
/**
38+
* 测试
39+
jarry@jarrys-MacBook-Pro java % java --version
40+
java 14.0.1 2020-04-14
41+
Java(TM) SE Runtime Environment (build 14.0.1+7)
42+
Java HotSpot(TM) 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)
43+
44+
jarry@jarrys-MacBook-Pro java % javac test/Test.java
45+
jarry@jarrys-MacBook-Pro java % java test/Test
46+
test start:
47+
Bus::run().
48+
Car::run().
49+
Motorcycle::run().
50+
Van::run().
51+
*/

factory-pattern/js/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "esm-project",
3+
"version": "1.0.0",
4+
"main": "test/test.js",
5+
"scripts": {
6+
"test": "node test/test.js"
7+
},
8+
"type": "module"
9+
}

0 commit comments

Comments
 (0)