Simple Factory, Abstract Factory, Builder, Singleton, Prototype.
It is a laboratory work by number 1, where 5 template have been used.
- This is Singleton, it helps me to show the messages which i print on console. To proof it, i created 2 instances of Logger a and b, then i made a===b, and saw on console true, that means that this is singleton
namespace Logger {
let log = "";
export function add(msg) {return log += msg + "\n"}
export function show() {console.log(log); log = "";}
}
- This is somepieces of Builder. This is class
Shopin which i got constuct with 1 argumentbuilder. For example i have classHouseBuilder, where i got steps and in my classShopnow i can use those steps to create my House (you can see example below of creating new House), i create variablehouseBuilderwhich contains instance ofHouseBuilderthen i construct house usingShopclass and giving there my instance of HouseBuilder
export default class Shop {
construct(builder) {
builder.step1();
builder.step2();
return builder.get();
}
}
__________________________________________________
class HouseBuilder {
house : any;
step1(){
this.house = new House(4,4);
};
step2() {
this.house.addParts();
};
get() {
return this.house;
};
}
___________________________________________________
let houseBuilder = new HouseBuilder();
let house = shop.construct(houseBuilder);
- This is my prototype, class
HousePrototypehas clone method in which i clone classHouseand get the same properties as my originalHouse
class HousePrototype{
constructor(private proto: any){};
clone() {
let house = new House(doors.createDetail(4),windows.createDetail(4));
house.doors = this.proto.doors;
house.windows = this.proto.windows;
return house;
};
}
___________________________________________________
let proto = new House(doors.createDetail(5),windows.createDetail(6));
let prototype = new HousePrototype(proto);
let protoHouse = prototype.clone();
- Factory pattern: firstly i create interface
Detailwith createDetail method with quantity argument, then i create interfaceFactoryand getcreateProductimplementingDetail, after this i create classProductFactorywhich implementsFactoryso i can create products, which can be chosen by switch case. I chose classDoorsto show how it is implemented,firstly it implementscreateDetailmethod in this method i have check if i got enought resources it create doors, if not it gets resources and then create doors ( it contains some realization of Abstract Factory)
interface Detail {
createDetail(quantity): number;
}
_____________________________________
interface Factory {
createProduct(name): Detail;
}
_________________________________________
class ProductFactory implements Factory {
createProduct(name): Detail {
switch(name) {
case 'doors':
return new Doors();
case 'windows':
return new Windows();
default:
return null;
}
}
}
_______________________________________
class Doors implements Detail {
createDetail(quantity): number {
if(resourceStorage.wood >= quantity){
console.log(`${quantity} doors were created`)
resourceStorage.wood -= quantity;
console.log('wood remain',resourceStorage.wood)
return quantity;
} else {
resourceStorage.wood += woodFactory.Pieces(quantity).value;
console.log('wood added',resourceStorage.wood)
resourceStorage.wood -= quantity;
console.log(`${quantity} doors were created`)
console.log('wood remain',resourceStorage.wood)
return quantity
}
}
}
- Abstract Factory, this is just example of WoodFactory, firstly i create Abstract class
Storagewhere i gotrefillResourcesmethod which i use to refill my resouces if i need, then i have classWoodStoragewhich extends Storage so i can refill resources, i gotAbstractFactorywhich got abstractPiecesthere i put quantity of items i need (for example wood or glass), then i create classWoodFactorywhich extends methods ofAbstractFactoryso i can createPiecesthat i need
abstract class Storage {
abstract refillResources(): void;
}
_________________________________
class WoodStorage extends Storage {
constructor(public value: number) {
super();
}
refillResources(): void {}
}
_________________________________
abstract class AbstractFactory {
abstract Pieces(quantity): Storage;
}
_________________________________
class WoodFactory extends AbstractFactory {
Pieces(quantity): WoodStorage {
return new WoodStorage(quantity);
}
}