Head First设计模式C++实现-Factory模式
定义:
工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。
作用:
将创建对象的代码集中在一个对象或方法中,可以避免代码中的重复,并且更方便以后的维护。这也意为着客户在实例化对象时,只会依赖于接口,而不是具体类。这遵循了实现针对接口编程,而不针对实现编程的原则。可以让代码更具有弹性,可以应对未来的扩展。
UML结构图如下:
实现代码:
程序输出:
Preparing NY Style Sauce and Cheese Pizza
Tossing Thin Crust Dough
Adding Martinara Sauce
Adding toppings:
Greated Reggiano Cheese
Bake for 25 minutes at 350
Cutting the pizza into diagonal slices
Place pizza in official PizzaStore box
Ethan ordered a NY Style Sauce and Cheese Pizza
Preparing Chicago Style Deep Dish Cheese Pizza
Tossing Extra Thick Crust Dough
Adding Plum Tomato Sauce
Adding toppings:
Shredded Mozzarella Cheese
Bake for 25 minutes at 350
Cutting the pizza into square slices
Place pizza in official PizzaStore box
Joel ordered a Chicago Style Deep Dish Cheese Pizza
请按任意键继续. . .