LoginSignup
0
0

More than 1 year has passed since last update.

typescriptでデザインパターン(Factory Method)

Posted at

Factory Methodをtypescriptで書いてみた。

クラス図

image.png

コード

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()

出力結果

image.png

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0