工厂模式

【实验内容和要求】

有一个OEM制造商代理做HP笔记本电脑(Laptop),后来该制造商得到了更多的品牌笔记本电脑的订单AcerLenovoDell,该OEM商发现,如果一次同时做很多个牌子的本本,有些不利于管理。利用工厂模式改善设计,用JAVA语言实现  (C#控制台应用程序实现)OEM制造商的工厂模式。绘制该模式的UML图。

模式UML

 

public interface ComputerFactory

{

    public void getComputerType();

}

public class AcerFactory implements ComputerFactory

{

    public void getComputerType()

   {

       return new AcerFactory;

    }   

}

public class DellFactory implements ComputerFactory

{

    public void getComputerType()

   {

       return new DellFactory;

    }   

}

public class LenovoFactory implements ComputerFactory

{

    public void getComputerType()

   {

       return new LenovoFactory;

    }   

}

public interface Computer

{

    public void computerType();

}

public class Acer implements Computer

{

   public AcerFactory computerType()

{

     return "Acer";

}

}

public class Dell implements Computer

{

    public DellFactory computerType()

   {

       return  "Dell";

    }   

}

public class Lenovo implements Computer

{

    public LenovoFactory computerType()

   {

       return "Lenovo";

    }   

}

原文地址:https://www.cnblogs.com/ljs-666/p/8678225.html