实验一 工厂模式的应用

实验内容:

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


代码:

package factory;

/**
 * 工厂模式
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "工厂模式" );
        Factory Acer=new AcerFactory();
        Factory Dell=new DellFactory();
        Factory Lenovo=new LenovoFactory();
        
        computer A=Acer.getcomputerType();
        computer D=Dell.getcomputerType();
        computer L=Lenovo.getcomputerType();
        A.computerType();
        D.computerType();
        L.computerType();
    }
}
/**
 * 产品接口
 * @author zhang
 *
 */
interface computer
{
	void computerType();
}
class Acer implements computer
{
	public void computerType()
	{
		System.out.print("Acer \n");
	}
}

class Dell implements computer
{
	public void computerType()
	{
		System.out.print("Dell \n");
	}
}

class Lenovo implements computer
{
	public void computerType()
	{
		System.out.print("Lenovo \n");
	}
}
/**
 * 工厂接口
 */
interface Factory{
	computer getcomputerType();
}
/**
 * 工厂类
 */

class AcerFactory implements Factory
{
	 public computer getcomputerType() {
		return new Acer();
	}
}

class DellFactory implements Factory
{
public computer getcomputerType() {
	return new Dell();
	}
}

class LenovoFactory implements Factory
{
public computer getcomputerType() {
	return new Lenovo();
	}
}

截图:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值