File tree Expand file tree Collapse file tree 5 files changed +3877
-5
lines changed
src/creational/factory/simple-factory Expand file tree Collapse file tree 5 files changed +3877
-5
lines changed Original file line number Diff line number Diff line change
1
+ /node_modules
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " javascript-design-patterns" ,
3
+ "version" : " 1.0.0" ,
4
+ "main" : " index.js" ,
5
+ "repository" : " https://github.com/tangweikun/javascript-design-pattern.git" ,
6
+ "author" : " tangweikun <819573105@qq.com>" ,
7
+ "license" : " MIT" ,
8
+ "scripts" : {
9
+ "watch" : " jest --watch"
10
+ },
11
+ "babel" : {
12
+ "presets" : [" es2015" , " stage-2" ]
13
+ },
14
+ "dependencies" : {
15
+ "babel-cli" : " ^6.26.0" ,
16
+ "babel-core" : " ^6.26.0" ,
17
+ "babel-jest" : " ^22.4.0" ,
18
+ "babel-preset-env" : " ^1.6.1" ,
19
+ "babel-preset-es2015" : " ^6.24.1" ,
20
+ "babel-preset-stage-2" : " ^6.24.1" ,
21
+ "jest" : " ^22.4.0" ,
22
+ "regenerator-runtime" : " ^0.11.1"
23
+ }
24
+ }
Original file line number Diff line number Diff line change 1
1
// 抽象产品角色 <Product>
2
2
function Admin ( ) {
3
- console . log ( 'Admin created!' )
3
+ return 'Admin created'
4
4
}
5
5
function Customer ( ) {
6
- console . log ( 'Customer created' )
6
+ return 'Customer created'
7
7
}
8
8
9
9
// 工厂角色 <Factory>
10
- const UserFactory = { }
10
+ export const UserFactory = { }
11
11
UserFactory . createUser = function ( type ) {
12
- if ( type === 'admin' ) return new Admin ( )
13
- if ( type === 'customer' ) return new Customer ( )
12
+ if ( type === 'admin' ) return Admin ( )
13
+ if ( type === 'customer' ) return Customer ( )
14
14
}
15
15
16
16
// 具体产品角色 <ConcreteProduct>
Original file line number Diff line number Diff line change
1
+ import { UserFactory } from '../UserFactory'
2
+
3
+ test ( 'UserFactory-admin' , ( ) => {
4
+ const admin = UserFactory . createUser ( 'admin' )
5
+ expect ( admin ) . toBe ( 'Admin created' )
6
+ } )
7
+
8
+ test ( 'UserFactory-customer' , ( ) => {
9
+ const customer = UserFactory . createUser ( 'customer' )
10
+ expect ( customer ) . toBe ( 'Customer created' )
11
+ } )
Load Diff Large diffs are not rendered by default.
You can’t perform that action at this time.
0 commit comments