Factory Methodをtypescriptで書いてみた。
クラス図
コード
abstract class Product {
public abstract use(): void
}
abstract class Factory {
public abstract create(owner: string): Product
}
class IdCardFactory extends Factory {
create(owner: string): IdCard {
return new IdCard(owner)
};
}
class IdCard extends Product {
owner: string;
constructor(owner: string) {
super();
console.log(`${owner}さんのカードを作ります`)
this.owner = owner;
};
public use(): void {
console.log(`${this.owner}のカードを使います`)
}
}
function main() {
const idFactory = new IdCardFactory();
const fooId = idFactory.create('foo');
fooId.use();
}
main()