ファクトリーデザインパターン
ファクトリーデザインパターン
工場設計パターンを使用する場合
我々がオブジェクト創造論理を1つの場所に保つ必要があるとき、
コード例
class Shape {
constructor(description) {
this.description = description;
}
}
class shapeFactory {
createShape(shapeType) {
switch(shapeType) {
case 'square':
return new Shape('Square shape');
case 'rectangle':
return new Shape('Rectangle shape');
case 'circle':
return new Shape('Circle shape');
}
}
}
const factory = new shapeFactory();
const circle = factory.createShape('circle');
const square = factory.createShape('square');
const rectangle = factory.createShape('rectangle');
console.log(circle); // Shape {description: 'Circle shape'}
console.log(square); // Shape {description: 'Square shape'}
console.log(rectangle); // Shape {description: 'Rectangle shape'}
shapeFactory
が提供されるshapeType
に基づいて形状オブジェクトを作成するファクトリクラスです.つの最後のもの.
あなたが次を学ぶことが好きなコメントで私に知らせてください..ブログを訪問してくれてありがとう.
Reference
この問題について(ファクトリーデザインパターン), 我々は、より多くの情報をここで見つけました https://dev.to/07101994/factory-design-pattern-javascript-2dibテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol