Skip to content

Commit 3f40906

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

21 files changed

+245
-5
lines changed

.gitignore

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

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

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

strategy-pattern/js/src/Context.js

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

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

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

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

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

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

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

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

strategy-pattern/js/test/test.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { ContextCat } from '../src/ContextCat.js'
2+
import { ContextDog } from '../src/ContextDog.js'
3+
import { StrategyA } from '../src/StrategyA.js'
4+
5+
export function test() {
6+
// 注意,JS本身有bind函数,可以用动态给函数绑定调用者,达到策略模式效果,而无需这种方式来实现。
7+
8+
// 实例化某个内容,策略已经绑定上
9+
const contextCat = new ContextCat()
10+
contextCat.action()
11+
12+
// 重新设置策略
13+
console.log("reset contextCat'strategy to StrategyA.")
14+
contextCat.setStrategy(new StrategyA())
15+
contextCat.action()
16+
17+
// 实例化某个内容,策略已经绑定上
18+
const contextGog = new ContextDog()
19+
contextGog.action()
20+
}
21+
22+
// 执行测试
23+
;(function () {
24+
console.log('test start:')
25+
test()
26+
})()
27+
/**
28+
jarry@jarrys-MacBook-Pro js % node --version
29+
v16.15.1
30+
*/
31+
/*
32+
// node run
33+
jarry@jarrys-MacBook-Pro js % node -v
34+
v16.15.1
35+
jarry@jarrys-MacBook-Pro js % node test/test.js
36+
test start:
37+
ContextCat::setStrategy(StrategyC).
38+
StrategyC::run().
39+
reset contextCat'strategy to StrategyA.
40+
StrategyA::run().
41+
ContextDog::setStrategy(StrategyB).
42+
StrategyB::run().
43+
*/
44+
45+
/*
46+
// npm run
47+
jarry@jarrys-MacBook-Pro js % npm run test
48+
49+
50+
> node test/test.js
51+
52+
test start:
53+
ContextCat::setStrategy(StrategyC).
54+
StrategyC::run().
55+
reset contextCat'strategy to StrategyA.
56+
StrategyA::run().
57+
ContextDog::setStrategy(StrategyB).
58+
StrategyB::run().
59+
*/
60+
61+
/**
62+
// n run
63+
jarry@jarrys-MacBook-Pro js % n run 14.17.0 test/test.js
64+
test start:
65+
ContextCat::setStrategy(StrategyC).
66+
StrategyC::run().
67+
reset contextCat'strategy to StrategyA.
68+
StrategyA::run().
69+
ContextDog::setStrategy(StrategyB).
70+
StrategyB::run().
71+
*/
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Context:
2+
strategy_a = None
3+
def __init__(self,):
4+
print('Context::init()')
5+
return
6+
7+
def set_strategy(self, strategy):
8+
self.strategy = strategy
9+
10+
def action(self):
11+
self.strategy.run()
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author: jarry
4+
"""
5+
from Context import Context
6+
from StrategyC import StrategyC
7+
class ContextCat(Context):
8+
def __init__(self,):
9+
print('ContextCat::setStrategy(StrategyC).')
10+
self.set_strategy(StrategyC())
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author: jarry
4+
"""
5+
from Context import Context
6+
from StrategyB import StrategyB
7+
class ContextDog(Context):
8+
def __init__(self,):
9+
print('ContextDog::setStrategy(StrategyB).')
10+
self.set_strategy(StrategyB())
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Strategy:
2+
# def __init__(self):
3+
# return
4+
def run(self):
5+
print('Strategy::run()')
6+
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from Strategy import Strategy
2+
class StrategyA(Strategy):
3+
def run(self):
4+
print('StrategyA::run()')
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from Strategy import Strategy
2+
class StrategyB(Strategy):
3+
def run(self):
4+
print('StrategyB::run()')
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from Strategy import Strategy
2+
class StrategyC(Strategy):
3+
def run(self):
4+
print('StrategyC::run()')
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# src module

strategy-pattern/python/test/test.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author: jarry
4+
"""
5+
import sys
6+
import os
7+
os_path = os.getcwd()
8+
sys.path.append(os_path)
9+
10+
from src.StrategyA import StrategyA
11+
from src.ContextDog import ContextDog
12+
from src.ContextCat import ContextCat
13+
14+
def test():
15+
# 实例化某个内容,策略已经绑定上
16+
context_cat = ContextCat()
17+
context_cat.action()
18+
19+
# 重新设置策略
20+
print("reset contextCat'strategy to StrategyA.")
21+
context_cat.set_strategy(StrategyA())
22+
context_cat.action()
23+
24+
# 实例化某个内容,策略已经绑定上
25+
context_dog = ContextDog()
26+
context_dog.action()
27+
28+
if __name__=='__main__':
29+
print(__file__)
30+
print("test start:")
31+
test()
32+
33+
'''
34+
jarry@jarrys-MacBook-Pro python % python -V
35+
Python 2.7.16
36+
jarry@jarrys-MacBook-Pro python % python test/test.py
37+
test/test.py
38+
test start:
39+
ContextCat::setStrategy(StrategyC).
40+
StrategyC::run()
41+
reset contextCat'strategy to StrategyA.
42+
StrategyA::run()
43+
ContextDog::setStrategy(StrategyB).
44+
StrategyB::run()
45+
'''

0 commit comments

Comments
 (0)