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

+13
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

+8
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

+8
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+
}
+8
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

+8
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+
}

factory-pattern/java/src/Vehicle.java

+5
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+
}
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+
}
+42
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+
}

factory-pattern/java/test/Test.java

+51
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

+9
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+
}

factory-pattern/js/src/Bus.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Vehicle } from './Vehicle.js'
2+
export class Bus extends Vehicle {
3+
constructor(options) {
4+
super(options)
5+
}
6+
run() {
7+
console.log('Bus::run().')
8+
}
9+
}

factory-pattern/js/src/Car.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Vehicle } from './Vehicle.js'
2+
export class Car extends Vehicle {
3+
constructor(options) {
4+
super(options)
5+
}
6+
run() {
7+
console.log('Car::run().')
8+
}
9+
}

factory-pattern/js/src/Motorcycle.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Vehicle } from './Vehicle.js'
2+
export class Motorcycle extends Vehicle {
3+
constructor(options) {
4+
super(options)
5+
}
6+
run() {
7+
console.log('Motorcycle::run().')
8+
}
9+
}

factory-pattern/js/src/Van.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Vehicle } from './Vehicle.js'
2+
export class Van extends Vehicle {
3+
constructor(options) {
4+
super(options)
5+
}
6+
run() {
7+
console.log('Van::run().')
8+
}
9+
}

factory-pattern/js/src/Vehicle.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class Vehicle {
2+
constructor(options) {
3+
// console.log('Vehicle::init()')
4+
}
5+
run() {
6+
console.log('Vehicle::run()')
7+
}
8+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Bus } from './Bus.js'
2+
import { Car } from './Car.js'
3+
import { Motorcycle } from './Motorcycle.js'
4+
import { Van } from './Van.js'
5+
export const VehicleType = {
6+
BUS: 'bus',
7+
CAR: 'car',
8+
MOTORCYCLE: 'motorcycle',
9+
VAN: 'van',
10+
}
11+
export class VehicleFactory {
12+
getVehicle(type, options) {
13+
switch (type) {
14+
case 'bus':
15+
return new Bus(options)
16+
case 'car':
17+
return new Car(options)
18+
case 'motorcycle':
19+
return new Motorcycle(options)
20+
case 'van':
21+
return new Van(options)
22+
default:
23+
return undefined
24+
}
25+
}
26+
}

factory-pattern/js/test/test.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script type="module">
2+
// module need run in http server
3+
import { test } from './test.js'
4+
alert('pls look console info.' + '\n\r' + test)
5+
</script>

factory-pattern/js/test/test.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { VehicleFactory, VehicleType } from '../src/VehicleFactory.js'
2+
3+
export function test() {
4+
const vehicleFactory = new VehicleFactory()
5+
6+
// 获取Bus对象,并调用它的 run 方法
7+
const bus = vehicleFactory.getVehicle(VehicleType.BUS)
8+
bus.run()
9+
10+
// 获取Car对象,并调用它的 run 方法
11+
const car = vehicleFactory.getVehicle(VehicleType.CAR)
12+
car.run()
13+
14+
// 获取Motorcycle对象,并调用它的 run 方法
15+
const motorcycle = vehicleFactory.getVehicle(VehicleType.MOTORCYCLE)
16+
motorcycle.run()
17+
18+
// 获取Motorcycle对象,并调用它的 run 方法
19+
const van = vehicleFactory.getVehicle(VehicleType.VAN)
20+
van.run()
21+
}
22+
23+
// 执行测试
24+
;(function () {
25+
console.log('test start:')
26+
test()
27+
})()
28+
/**
29+
jarry@jarrys-MacBook-Pro js % node --version
30+
v16.15.1
31+
*/
32+
/*
33+
// node run
34+
jarry@jarrys-MacBook-Pro js % node -v
35+
v16.15.1
36+
jarry@jarrys-MacBook-Pro js % node test/test.js
37+
test start:
38+
Bus::run().
39+
Car::run().
40+
Motorcycle::run().
41+
Van::run().
42+
*/
43+
44+
/*
45+
// npm run
46+
jarry@jarrys-MacBook-Pro js % npm run test
47+
48+
49+
> node test/test.js
50+
51+
test start:
52+
Bus::run().
53+
Car::run().
54+
Motorcycle::run().
55+
Van::run().
56+
*/
57+
58+
/**
59+
// n run
60+
jarry@jarrys-MacBook-Pro js % n run 14.17.0 test/test.js
61+
test start:
62+
Bus::run().
63+
Car::run().
64+
Motorcycle::run().
65+
Van::run().
66+
*/
67+
68+
/*
69+
// node 10.24.1 isn't support es module
70+
// Node 13.2.0 can support ES Modules
71+
jarry@jarrys-MacBook-Pro js % n run 10.24.1 test/test.js
72+
/Users/jarry/github/design-pattern/factory-pattern/js/test/test.js:1
73+
import { VehicleFactory, VehicleType } from '../src/VehicleFactroy.js'
74+
^
75+
76+
SyntaxError: Unexpected token {
77+
at Module._compile (internal/modules/cjs/loader.js:723:23)
78+
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
79+
at Module.load (internal/modules/cjs/loader.js:653:32)
80+
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
81+
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
82+
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
83+
at startup (internal/bootstrap/node.js:283:19)
84+
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
85+
jarry@jarrys-MacBook-Pro js %
86+
*/

factory-pattern/python/src/Bus.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from Vehicle import Vehicle
2+
class Bus(Vehicle):
3+
def run(self):
4+
print("Bus::run()")
5+

factory-pattern/python/src/Car.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from Vehicle import Vehicle
2+
class Car(Vehicle):
3+
def run(self):
4+
print("Car::run()")
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from Vehicle import Vehicle
2+
class Motorcycle(Vehicle):
3+
def run(self):
4+
print("Motorcycle::run()")

factory-pattern/python/src/Van.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from Vehicle import Vehicle
2+
class Van(Vehicle):
3+
def run(self):
4+
print("Van::run()")

0 commit comments

Comments
 (0)