8000 Merge pull request #17 from Naor-Tedgi/naor/fly-weight-es6 · wolfwithcode/design-patterns-JS@bb9ab86 · GitHub
[go: up one dir, main page]

Skip to content

Commit bb9ab86

Browse files
authored
Merge pull request fbeline#17 from Naor-Tedgi/naor/fly-weight-es6
Naor/fly weight es6
2 parents abe5d60 + 0330d94 commit bb9ab86

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

docs.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,29 @@ var colorFactory = {
16031603

16041604
module.exports = colorFactory;
16051605

1606+
```
1607+
##### flyweight_es6.js
1608+
```Javascript
1609+
class Color {
1610+
constructor(name){
1611+
this.name = name
1612+
}
1613+
}
1614+
1615+
class colorFactory {
1616+
constructor(name){
1617+
this.colors = {};
1618+
}
1619+
create(name) {
1620+
let color = this.colors[name];
1621+
if(color) return color;
1622+
this.colors[name] = new Color(name);
1623+
return this.colors[name];
1624+
}
1625+
};
1626+
1627+
export { colorFactory };
1628+
16061629
```
16071630

16081631
### Proxy
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+
export { 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+
import { colorFactory } from '../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