File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Color {
2
+ constructor ( name ) {
3
+ this . name = name
4
+ }
5
+ }
6
+
7
+ class colorFactory {
8
+ constructor ( name ) {
9
+ this . colors = { } ;
10
+ }
11
+ create ( name ) {
12
+ let color = this . colors [ name ] ;
13
+ if ( color ) return color ;
14
+ this . colors [ name ] = new Color ( name ) ;
15
+ return this . colors [ name ] ;
16
+ }
17
+ } ;
18
+
19
+ module . exports = colorFactory ;
Original file line number Diff line number Diff line change
1
+ const expect = require ( 'chai' ) . expect ;
2
+
3
+ const colorFactory = require ( '../src/structural/flyweight/flyweight_es6' ) ;
4
+
5
+ describe ( 'flyweight tests' , ( ) => {
6
+
7
+ it ( 'sanity' , ( ) => {
8
+ const cf = new colorFactory ( ) ;
9
+ cf . create ( 'RED' ) ;
10
+ cf . create ( 'RED' ) ;
11
+ cf . create ( 'RED' ) ;
12
+ cf . create ( 'YELLOW' ) ;
13
+ cf . create ( 'YELLOW' ) ;
14
+ expect ( Object . keys ( cf . colors ) ) . to . have . lengthOf ( 2 ) ;
15
+ } ) ;
16
+
17
+ } ) ;
You can’t perform that action at this time.
0 commit comments