您的位置:首页 > 编程语言 > Java开发

Java设计模式:抽像工厂模式

2014-12-14 20:42 267 查看
抽象工厂模式为工厂模式添加了一个抽象层,是创建其它工厂的超级工厂,也称为工厂的工厂。

Abstract Factory class diagram




/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package client;

interface CPU
{
void process();
}

interface CPUFactory
{
CPU produceCPU();
}

class AMDFactory implements CPUFactory
{
public CPU produceCPU()
{
return new AMDCPU();
}
}

class IntelFactory implements CPUFactory
{
public CPU produceCPU()
{
return new IntelCPU();
}
}

class AMDCPU implements CPU
{
public void process()
{
System.out.println("AMD is processing....");
}
}

class IntelCPU implements CPU
{
public void process()
{
System.out.println("Intel is processing...");
}
}

class Computer
{
CPU cpu;
public Computer(CPUFactory factory)
{
cpu = factory.produceCPU();
cpu.process();
}
}

public class Client
{
public static void main(String[] args)
{
new Computer(createSpecificFactory());
}

public static CPUFactory createSpecificFactory()
{
int sys = 0;
if (sys == 0) return new AMDFactory();
else return new IntelFactory();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: