Skip to content

Commit 936223e

Browse files
committed
JavaScript Design Patterns: The Creational Pattern, README.md
1 parent 9a01936 commit 936223e

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

Design_Patterns/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# JavaScript Design Patterns
2+
3+
## What is a pattern?
4+
5+
A pattern is a solution that can be reused to solve commonly occuring problems in software design. The design patterns have three main benefits:
6+
7+
- Proven solutions: these solutions reflect the experience and insights the developers that helped define and improve them bring to the pattern.
8+
- Reused easily: A pattern can be adapted to suit your own programs.
9+
- Expressive: A pattern is a set of structure and "vocabulary"/"terms" to the solution.
10+
11+
## The Structure of a Design Pattern
12+
13+
- Pattern name
14+
- Context outline: the contexts in which the pattern is effective in responding to the users needs.
15+
- Problem statement: a statement of the problem being addressed so we can understand the intent of the pattern
16+
- Solution: a description of how problems are being solved in a list of steps and perceptions.
17+
- Design:
18+
- Implementation
19+
- Illustration: a visual representation of classes in the patterns (UML diagrams)
20+
- Co-requisites: what other patterns may be needed to support
21+
- Relations:
22+
- Known usage
23+
- Discussion
24+
25+
## Categories of Design Patterns
26+
27+
The design patterns can be locked down into 3 main categories:
28+
29+
- Creational Design Patterns: focus on handling object, classes creation. These patterns aim to solve problems by controlling creation process.
30+
31+
- Constructor
32+
- Factory
33+
- Abstract
34+
- Prototype
35+
- Singleton
36+
- Builder
37+
38+
- Structural Design Patterns: the pattern concerns object composition and relationships between different objects. For example, they help to ensure that one part of a system changes, the entire structure of the system doesn't need to do the same.
39+
40+
- Decorator
41+
- Facade
42+
- Flyweight
43+
- Adapter
44+
- Proxy
45+
46+
- Behavioral Design Patterns: focus on moving or streamlining the communication between objects in a system.
47+
- Iterator
48+
- Mediator
49+
- Observer
50+
- Visitor
51+
52+
## The Table of Design Patterns
53+
54+
| Creational | Structural | Behavioral |
55+
| ------------------------------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
56+
| Classes: Factory Method | Classes: Adapter | Classes: Interpreter, Template Method |
57+
| Object: Abstract Factory, Builder, Prototype, Singleton | Object: Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy | Chain of Reponsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Visitor |
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**The Creational Pattern
2+
*
3+
*
4+
* We will form the basic of other design patterns in creational group.
5+
*
6+
* In JavaScript, there are three common ways to create new objects
7+
*/
8+
9+
let newObject1 = {};
10+
let newObject2 = Object.create(null);
11+
let newObject3 = new Object();
12+
13+
// There are 4 ways to assign keys and values to an object
14+
15+
// 1. Using dot syntax
16+
newObject1.someKey = "A Man Learns Code";
17+
let key = newObject1.someKey; // A Man Learns Code
18+
19+
// 2. Square bracket syntax
20+
newObject2["someKey"] = "A Man Learns Code";
21+
let key2 = newObject2["someKey"]; // A Man Learns Code
22+
23+
// 3. Using Object.defineProperty
24+
Object.defineProperty(newObject3, "someKey", {
25+
value: "A Man Learns Code",
26+
writable: true,
27+
enumerable: true,
28+
configurable: true,
29+
});
30+
31+
// 4. Using Object.defineProperties
32+
Object.defineProperties(newObject3, {
33+
anotherKey1: {
34+
value: "Hello, World!",
35+
writable: true,
36+
},
37+
anotherKey2: {
38+
value: "Bye",
39+
writable: true,
40+
},
41+
});

0 commit comments

Comments
 (0)