(1)掌握工厂模式(Factory)的特点
(2)分析具体问题,使用工厂模式进行设计。
有一个OEM制造商代理做HP笔记本电脑(Laptop),后来该制造商得到了更多的品牌笔记本电脑的订单Acer,Lenovo,Dell,该OEM商发现,如果一次同时做很多个牌子的本本,有些不利于管理。利用工厂模式改善设计,用C#或Java、C++控制台应用程序实现该OEM制造商的工厂模式。绘制该模式的UML图。
三、源代码1.FactoryMethod类
public class FactoryMethod {public static void main(String[] args) {Computer c = null;Factory f = null;f = new DellFactory();c = f.getComputerType();c.ComputerType();f = new LenovoFactory();c = f.getComputerType();c.ComputerType();f = new AcerFactory();c = f.getComputerType();c.ComputerType();}} interface Factory{Computer getComputerType();}2.DellFactory类
class DellFactory implements Factory{@Overridepublic Computer getComputerType() {return new Dell();}}3.AcerFactory类
class AcerFactory implements Factory{@Overridepublic Computer getComputerType() {return new Acer();}}4.LenovoFactory类
class LenovoFactory implements Factory{@Overridepublic Computer getComputerType() {return new Lenovo();}} interface Computer{public void ComputerType();}5.Dell 类
class Dell implements Computer{@Overridepublic void ComputerType() {System.out.println("Dell Computer");}}6.Acer 类
class Acer implements Computer{@Overridepublic void ComputerType() {System.out.println("Acer Computer");}}7.Lenovo 类
class Lenovo implements Computer{@Overridepublic void ComputerType() {System.out.println("Lenovo Computer");}} 四、UML图