abstract class Pizza {
public abstract show(): void;
public abstract cut(): void;
}
class KFCPizza extends Pizza {
public show(): void {
console.log('This is KFCPizza!');
}
public cut(): void {
console.log('Cut KFCPizza!');
}
}
class MCPizza extends Pizza {
public show(): void {
console.log('This is MCPizza!');
}
public cut(): void {
console.log('Cut MCPizza!');
}
}
class PizzaFactory {
/**
* 静态工厂方法
* @param pizzaType 限制传入的参数为类,而非类的实例
*/
public static createPizza(pizzaType: new () => Pizza): Pizza {
let pizza = null;
try {
pizza = new pizzaType();
} catch (e) {
console.error('Create failed!');
}
return pizza;
}
}
let pizza = PizzaFactory.createPizza(KFCPizza);
pizza.show();
pizza = PizzaFactory.createPizza(MCPizza);
pizza.show();
abstract class Pizza {
public abstract show(): void;
public abstract cut(): void;
}
class KFCPizza extends Pizza {
public show(): void {
console.log('This is KFCPizza!');
}
public cut(): void {
console.log('Cut KFCPizza!');
}
}
class MCPizza extends Pizza {
public show(): void {
console.log('This is MCPizza!');
}
public cut(): void {
console.log('Cut MCPizza!');
}
}
abstract class PizzaFactory {
public abstract createPizza(): Pizza;
}
class KFCPizzaFactory extends PizzaFactory {
public createPizza(): Pizza {
try {
return new KFCPizza();
} catch (error) {
console.log(error);
}
}
}
class MCPizzaFactory extends PizzaFactory {
public createPizza(): Pizza {
try {
return new MCPizza();
} catch (error) {
console.log(error);
}
}
}
let factory: PizzaFactory = new KFCPizzaFactory();
let pizza = factory.createPizza();
pizza.show();
// KFC产品父类
abstract class KFCPizza {
public abstract show(): void;
public abstract cut(): void;
}
// MC产品父类
abstract class MCPizza {
public abstract show(): void;
public abstract cut(): void;
}
// 具体KFCPizza类
class KFCFruitPizza extends KFCPizza {
public show(): void {
console.log('This is KFCFruitPizza!');
}
public cut(): void {
console.log('Cut KFCFruitPizza!');
}
}
class KFCCheesePizza extends KFCPizza {
public show(): void {
console.log('This is KFCCheesePizza!');
}
public cut(): void {
console.log('Cut KFCCheesePizza!');
}
}
// 具体KFCPizza类
class MCFruitPizza extends MCPizza {
public show(): void {
console.log('This is MCFruitPizza!');
}
public cut(): void {
console.log('Cut MCFruitPizza!');
}
}
class MCCheesePizza extends MCPizza {
public show(): void {
console.log('This is MCCheesePizza!');
}
public cut(): void {
console.log('Cut MCCheesePizza!');
}
}
abstract class PizzaFactory {
public abstract createKFCPizza(): KFCPizza;
public abstract createMCPizza(): MCPizza;
}
// 水果披萨工厂
class FruitPizzaFactory extends PizzaFactory {
public createKFCPizza(): KFCPizza {
try {
return new KFCFruitPizza();
} catch (error) {
console.log(error);
}
}
public createMCPizza(): MCPizza {
try {
return new MCFruitPizza();
} catch (error) {
console.log(error);
}
}
}
// 芝士披萨工厂
class CheesePizzaFactory extends PizzaFactory {
public createKFCPizza(): KFCPizza {
try {
return new KFCCheesePizza();
} catch (error) {
console.log(error);
}
}
public createMCPizza(): MCPizza {
try {
return new MCCheesePizza();
} catch (error) {
console.log(error);
}
}
}
let factory: PizzaFactory = new CheesePizzaFactory();
let cheesePizza = factory.createKFCPizza();
cheesePizza.show();