Skip to content

Commit e71cd8c

Browse files
committed
Prototype pattern
1 parent 4c052dc commit e71cd8c

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
- [Abstract Factory](./src/AbstractFactory/README.md)
88
- [Builder](./src/Builder/README.md)
99
- [Factory Method](./src/FactoryMethod/README.md)
10+
- [Prototype](./src/Prototype/README.md)

src/Prototype/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Prototype
2+
3+
https://refactoring.guru/ja/design-patterns/prototype
4+
5+
```mermaid
6+
classDiagram
7+
class Prototype {
8+
<<Interface>>
9+
clone()
10+
}
11+
class ConcretePrototypeA {
12+
clone()
13+
}
14+
class ConcretePrototypeB {
15+
clone()
16+
}
17+
class Client {
18+
}
19+
20+
ConcretePrototypeA ..|> Prototype
21+
ConcretePrototypeB ..|> Prototype
22+
Client ..> Prototype
23+
```

src/Prototype/index.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export {};
2+
3+
class Prototype {
4+
public primitive: any;
5+
public component: object;
6+
public circularReference: ComponentWithBackReference;
7+
8+
public clone(): this {
9+
const clone = Object.create(this);
10+
11+
clone.component = Object.create(this.component);
12+
13+
clone.circularReference = {
14+
...this.circularReference,
15+
prototype: { ...this },
16+
};
17+
18+
return clone;
19+
}
20+
}
21+
22+
class ComponentWithBackReference {
23+
public prototype;
24+
25+
constructor(prototype: Prototype) {
26+
this.prototype = prototype;
27+
}
28+
}
29+
30+
function clientCode() {
31+
const p1 = new Prototype();
32+
p1.primitive = 245;
33+
p1.component = new Date();
34+
p1.circularReference = new ComponentWithBackReference(p1);
35+
36+
const p2 = p1.clone();
37+
if (p1.primitive === p2.primitive) {
38+
console.log('Primitive field values have been carried over to a clone. Yay!');
39+
} else {
40+
console.log('Primitive field values have not been copied. Booo!');
41+
}
42+
if (p1.component === p2.component) {
43+
console.log('Simple componet has not been cloned. Booo!');
44+
} else {
45+
console.log('Simple component has been cloned. Yay!');
46+
}
47+
48+
if (p1.circularReference === p2.circularReference) {
49+
console.log('Component with back reference has not been cloned. Booo!');
50+
} else {
51+
console.log('Component with back refarence has been cloned. Yay!');
52+
}
53+
if (p1.circularReference.prototype === p2.circularReference.prototype) {
54+
console.log('Component with back reference is linked to original object. Booo!');
55+
} else {
56+
console.log('Component with back reference is linked to the clone. Yay!');
57+
}
58+
}
59+
60+
clientCode();

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
8888
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
8989
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
90-
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
90+
"strictPropertyInitialization": false, /* Check for class properties that are declared but not set in the constructor. */
9191
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
9292
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
9393
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */

0 commit comments

Comments
 (0)