Skip to content

Instantly share code, notes, and snippets.

@YutoKashiwagi
Created June 22, 2021 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YutoKashiwagi/d7d911f09ba480abc09f323ab48521d6 to your computer and use it in GitHub Desktop.
Save YutoKashiwagi/d7d911f09ba480abc09f323ab48521d6 to your computer and use it in GitHub Desktop.
デザインパターン: ファクトリーメソッドパターン
abstract class Product {
abstract use: () => void
}
abstract class Factory {
create = (owner: string): Product => {
const product = this.createProduct(owner)
this.registerProduct(product)
return product
}
protected abstract createProduct: (owner: string) => Product
protected abstract registerProduct: (product: Product) => void
}
/**
* Product, Factoryを使ってIDカードを作る
*/
class IDCard extends Product {
private owner: string
constructor(owner: string) {
super()
console.log(owner + 'のカードを作ります')
this.owner = owner
}
use = () => {
console.log(this.owner + 'のカードを使います')
}
getOwner = (): string => {
return this.owner
}
}
class IDCardFactory extends Factory {
private owners: string[]
constructor() {
super()
this.owners = []
}
protected createProduct = (owner: string) => {
return new IDCard(owner)
}
protected registerProduct = (product: Product) => {
// FIXME ここのasは良くない
this.owners.push((product as IDCard).getOwner())
}
}
class IDCardUsecase {
constructor() {
const idCardFactory = new IDCardFactory()
const cardA = idCardFactory.create('owner A')
const cardB = idCardFactory.create('owner B')
const cardC = idCardFactory.create('owner C')
cardA.use()
cardB.use()
cardC.use()
}
}
// owner Aのカードを作ります
// owner Bのカードを作ります
// owner Cのカードを作ります
// owner Aのカードを使います
// owner Bのカードを使います
// owner Cのカードを使います
new IDCardUsecase()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment