Skip to content

Commit 3f40906

Browse files
committed
add strategy pattern
1 parent baddb7d commit 3f40906

File tree

21 files changed

+245
-5
lines changed

21 files changed

+245
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Compiled class file
22
*.class
3+
*.pyc
34

45
# Log file
56
*.log

factory-pattern/python/test/test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111
from src.VehicleFactory import VehicleType
1212

1313
def test():
14-
vehicleFactory = VehicleFactory()
14+
vehicle_factory = VehicleFactory()
1515

1616
# 获取Bus对象,并调用它的 run 方法
17-
bus = vehicleFactory.get_vehicle(VehicleType.BUS)
17+
bus = vehicle_factory.get_vehicle(VehicleType.BUS)
1818
bus.run()
1919

2020
# 获取Car对象,并调用它的 run 方法
21-
car = vehicleFactory.get_vehicle(VehicleType.CAR)
21+
car = vehicle_factory.get_vehicle(VehicleType.CAR)
2222
car.run()
2323

2424
# 获取Motorcycle对象,并调用它的 run 方法
25-
motorcycle = vehicleFactory.get_vehicle(VehicleType.MOTORCYCLE)
25+
motorcycle = vehicle_factory.get_vehicle(VehicleType.MOTORCYCLE)
2626
motorcycle.run()
2727

2828
# 获取Motorcycle对象,并调用它的 run 方法
29-
van = vehicleFactory.get_vehicle(VehicleType.VAN)
29+
van = vehicle_factory.get_vehicle(VehicleType.VAN)
3030
van.run()
3131

3232
if __name__=='__main__':

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

strategy-pattern/js/src/Context.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export class Context {
2+
strategy = null
3+
4+
constructor(options) {
5+
// console.log('Context::init()')
6+
}
7+
8+
setStrategy(strategy) {
9+
this.strategy = strategy
10+
}
11+
12+
action() {
13+
this.strategy.run()
14+
}
15+
16+
}

strategy-pattern/js/src/ContextCat.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Context } from './Context.js'
2+
import { StrategyC } from './StrategyC.js'
3+
export class ContextCat extends Context {
4+
constructor() {
5+
// 使用某个策略
6+
super()
7+
console.log('ContextCat::setStrategy(StrategyC).')
8+
this.setStrategy(new StrategyC())
9+
}
10+
}

strategy-pattern/js/src/ContextDog.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Context } from './Context.js'
2+
import { StrategyB } from './StrategyB.js'
3+
export class ContextDog extends Context {
4+
constructor() {
5+
// 使用某个策略
6+
super()
7+
console.log('ContextDog::setStrategy(StrategyB).')
8+
this.setStrategy(new StrategyB())
9+
}
10+
}

strategy-pattern/js/src/Strategy.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class Strategy {
2+
run() {
3+
console.log('Strategy::run().')
4+
}
5+
}

strategy-pattern/js/src/StrategyA.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Strategy } from './Strategy.js'
2+
export class StrategyA extends Strategy {
3+
run() {
4+
console.log('StrategyA::run().')
5+
}
6+
}

strategy-pattern/js/src/StrategyB.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Strategy } from './Strategy.js'
2+
export class StrategyB extends Strategy {
3+
run() {
4+
console.log('StrategyB::run().')
5+
}
6+
}

strategy-pattern/js/src/StrategyC.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Strategy } from './Strategy.js'
2+
export class StrategyC extends Strategy {
3+
run() {
4+
console.log('StrategyC::run().')
5+
}
6+
}

0 commit comments

Comments
 (0)