8000 flyweight_es6 + specs · wolfwithcode/design-patterns-JS@fb99349 · GitHub
[go: up one dir, main page]

Skip to content

Commit fb99349

Browse files
committed
flyweight_es6 + specs
1 parent 30f04fe commit fb99349

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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;

test/flyweight_es6-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
});

0 commit comments

Comments
 (0)
0