diff options
author | Luke "Jared" Bennett <lbennett@gitlab.com> | 2017-05-15 14:04:51 +0000 |
---|---|---|
committer | Luke "Jared" Bennett <lbennett@gitlab.com> | 2017-05-15 14:04:51 +0000 |
commit | 7d451a63b6136d7532f17407d623ff3956c2990b (patch) | |
tree | 07d165713abeefcba9f31e4be6ef682ac179ddad | |
parent | da6493793d20fe463a39e362261ea5306852d314 (diff) | |
download | gitlab-ce-7d451a63b6136d7532f17407d623ff3956c2990b.tar.gz |
Update singleton pattern docs in design_patterns.md
-rw-r--r-- | doc/development/fe_guide/design_patterns.md | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/doc/development/fe_guide/design_patterns.md b/doc/development/fe_guide/design_patterns.md index e05887a19af..ffca801fd03 100644 --- a/doc/development/fe_guide/design_patterns.md +++ b/doc/development/fe_guide/design_patterns.md @@ -2,32 +2,31 @@ ## Singletons -When exactly one object is needed for a given task, prefer to define it as a -`class` rather than as an object literal. Prefer also to explicitly restrict -instantiation, unless flexibility is important (e.g. for testing). +As with everything at GitLab, the simplest approach should be taken. +Below we have defined a few patterns to achieve singleton-like behaviour. +Pick the simplest one that fits your usecase. ```javascript -// bad - const MyThing = { - prop1: 'hello', - method1: () => {} + prop: 'hello', + init: () => {} }; export default MyThing; +``` -// good - +```javascript class MyThing { constructor() { - this.prop1 = 'hello'; + this.prop = 'hello'; } - method1() {} + init() {} } export default new MyThing(); +``` -// best +```javascript export default class MyThing { constructor() { @@ -39,7 +38,7 @@ export default class MyThing { } init() { - this.prop1 = 'hello'; + this.prop = 'hello'; } method1() {} |