Skip to content

Commit 3910a7c

Browse files
committed
[ts] refactor prototype
1 parent 1c78ffe commit 3910a7c

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

Diff for: prototype-pattern/prototype-pattern.ts

+12-18
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,33 @@ interface Animal {
1616
}
1717

1818
class Sheep implements Animal {
19-
private name: string;
19+
name: string;
2020

2121
public constructor() {
2222
console.log("Sheep is made");
2323
}
2424

25-
public makeCopy(): Animal {
25+
makeCopy(): Animal {
2626
console.log("Sheep is being made");
2727
const sheepObject: Sheep = Object.create(this);
2828
return sheepObject;
2929
}
30-
31-
public setName(name: string): void {
32-
this.name = name;
33-
}
34-
35-
public getName(): string {
36-
return this.name;
37-
}
3830
}
3931

4032
class CloneFactory {
41-
public getClone(animalSample: Animal): Animal {
33+
getClone(animalSample: Animal): Animal {
4234
return animalSample.makeCopy();
4335
}
4436
}
4537

4638
//-------------------------------------------------
4739

48-
const animalMaker: CloneFactory = new CloneFactory();
49-
const sally: Sheep = new Sheep();
50-
sally.setName("sally");
51-
const clonedSheep: Sheep = <Sheep>animalMaker.getClone(sally);
52-
clonedSheep.setName("sally clone");
53-
console.log(sally.getName());
54-
console.log(clonedSheep.getName());
40+
const animalMaker = new CloneFactory();
41+
const sally = new Sheep();
42+
sally.name = "sally";
43+
44+
const clonedSheep = <Sheep>animalMaker.getClone(sally);
45+
clonedSheep.name = "sally clone";
46+
47+
console.log(sally.name);
48+
console.log(clonedSheep.name);

0 commit comments

Comments
 (0)