设计模式之--抽象工厂

    抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。抽象工厂模式又称为Kit模式,它是一种对象创建型模式。举例子说明。


代码如下:

package abstractfactory;
public interface Fruit
{
public String getName();
}

package abstractfactory;


public interface Veggie
{
public String getName();
}

package abstractfactory;


public class NorthernFruit implements Fruit
{
    private String name;


    public NorthernFruit(String name)
    {
    this.name=name;
    }


    public String getName()
    {
return name;
}


    public void setName(String name)
    {
this.name = name;
}


}


package abstractfactory;


public class TropicalFruit implements Fruit
{
    private String name;


    public TropicalFruit(String name)
    {
    }


    public String getName()
    {
return name;
}


    public void setName(String name)
    {
this.name = name;
}


}


package abstractfactory;
public interface Gardener
{
    public Fruit createFruit(String name);


    public Veggie createVeggie(String name);
}


package abstractfactory;


public class TropicalGardener implements Gardener
{
    public Fruit createFruit(String name)
    {
        return new TropicalFruit(name);
    }


    public Veggie createVeggie(String name)
    {
        return new TropicalVeggie(name);
    }


}

package abstractfactory;


public class NorthernGardener implements Gardener
{
    public Fruit createFruit(String name)
    {
        return new NorthernFruit(name);
    }


    public Veggie createVeggie(String name)
    {
        return new NorthernVeggie(name);
    }
}


package abstractfactory;


public class TestClient {
private static Gardener gardener;
public static void main(String[] args){
gardener=new NorthernGardener();
Fruit f=gardener.createFruit("苹果");
System.out.println(f.getName());
Veggie v=gardener.createVeggie("香菜");
System.out.println(v.getName());

}


}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值