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

抽象工厂模式为工厂模式添加了一个抽象层,是创建其它工厂的超级工厂,也称为工厂的工厂。

Abstract Factory class diagram

abstract-factory-design-pattern

/*
 * 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();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值