File tree 21 files changed +245
-5
lines changed
factory-pattern/python/test
21 files changed +245
-5
lines changed Original file line number Diff line number Diff line change 1
1
# Compiled class file
2
2
* .class
3
+ * .pyc
3
4
4
5
# Log file
5
6
* .log
Original file line number Diff line number Diff line change 11
11
from src .VehicleFactory import VehicleType
12
12
13
13
def test ():
14
- vehicleFactory = VehicleFactory ()
14
+ vehicle_factory = VehicleFactory ()
15
15
16
16
# 获取Bus对象,并调用它的 run 方法
17
- bus = vehicleFactory .get_vehicle (VehicleType .BUS )
17
+ bus = vehicle_factory .get_vehicle (VehicleType .BUS )
18
18
bus .run ()
19
19
20
20
# 获取Car对象,并调用它的 run 方法
21
- car = vehicleFactory .get_vehicle (VehicleType .CAR )
21
+ car = vehicle_factory .get_vehicle (VehicleType .CAR )
22
22
car .run ()
23
23
24
24
# 获取Motorcycle对象,并调用它的 run 方法
25
- motorcycle = vehicleFactory .get_vehicle (VehicleType .MOTORCYCLE )
25
+ motorcycle = vehicle_factory .get_vehicle (VehicleType .MOTORCYCLE )
26
26
motorcycle .run ()
27
27
28
28
# 获取Motorcycle对象,并调用它的 run 方法
29
- van = vehicleFactory .get_vehicle (VehicleType .VAN )
29
+ van = vehicle_factory .get_vehicle (VehicleType .VAN )
30
30
van .run ()
31
31
32
32
if __name__ == '__main__' :
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ export class Strategy {
2
+ run ( ) {
3
+ console . log ( 'Strategy::run().' )
4
+ }
5
+ }
Original file line number Diff line number Diff line change
1
+ import { Strategy } from './Strategy.js'
2
+ export class StrategyA extends Strategy {
3
+ run ( ) {
4
+ console . log ( 'StrategyA::run().' )
5
+ }
6
+ }
Original file line number Diff line number Diff line change
1
+ import { Strategy } from './Strategy.js'
2
+ export class StrategyB extends Strategy {
3
+ run ( ) {
4
+ console . log ( 'StrategyB::run().' )
5
+ }
6
+ }
Original file line number Diff line number Diff line change
1
+ import { Strategy } from './Strategy.js'
2
+ export class StrategyC extends Strategy {
3
+ run ( ) {
4
+ console . log ( 'StrategyC::run().' )
5
+ }
6
+ }
Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
+ */
Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change
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 ())
Original file line number Diff line number Diff line change
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 ())
Original file line number Diff line number Diff line change
1
+ class Strategy :
2
+ # def __init__(self):
3
+ # return
4
+ def run (self ):
5
+ print ('Strategy::run()' )
6
+
Original file line number Diff line number Diff line change
1
+ from Strategy import Strategy
2
+ class StrategyA (Strategy ):
3
+ def run (self ):
4
+ print ('StrategyA::run()' )
Original file line number Diff line number Diff line change
1
+ from Strategy import Strategy
2
+ class StrategyB (Strategy ):
3
+ def run (self ):
4
+ print ('StrategyB::run()' )
Original file line number Diff line number Diff line change
1
+ from Strategy import Strategy
2
+ class StrategyC (Strategy ):
3
+ def run (self ):
4
+ print ('StrategyC::run()' )
Original file line number Diff line number Diff line change
1
+ # src module
Original file line number Diff line number Diff line change
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
+ '''
You can’t perform that action at this time.
0 commit comments