设计模式2 - 抽象工厂模式AbstractFactory Method


提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
适用性
1.一个系统要独立于它的产品的创建、组合和表示时。
2.一个系统要由多个产品系列中的一个来配置时。
3.当你要强调一系列相关的产品对象的设计以便进行联合使用时。
4.当你提供一个产品类库,而只想显示它们的接口而不是实现时。

public abstract class Garden {                                                                                
    public abstract Plant getCenter();
    public abstract Plant getBorder();
    public abstract Plant getShade();
}

public class Plant {
    private String name;
    public Plant(String pname) {                                                                              
        name = pname;
    }
    public String getName() {
        return name;
    }
}

三种Garden:

public class VeggieGarden extends Garden {                                                                    
    public Plant getShade() {
        return new Plant("Broccoli");
    }
    public Plant getCenter() {
        return new Plant("Corn");
    }
    public Plant getBorder() {
        return new Plant("Peas");
    }
}

public class AnnualGarden extends Garden {                                                                    
    public Plant getShade() {
        return new Plant("Coleus");
    }
    public Plant getCenter() {
        return new Plant("Marigold");
    }
    public Plant getBorder() {
        return new Plant("Alyssum");
    }

}

public class PerennialGarden extends Garden {                                                                 
    public Plant getShade() {
        return new Plant("Astilbe");
    }
    public Plant getCenter() {
        return new Plant("Dicentrum");
    }
    public Plant getBorder() {
        return new Plant("Sedum");
    }

}

GardenFactory:

public class GardenFactory {
    private static Garden[] gardens= new Garden[] {
                    new VeggieGarden(), new AnnualGarden(), new PerennialGarden(),
                    };
    private static java.util.Random rand = new java.util.Random();
    public static Garden getGarden() {                                                                        
        return gardens[rand.nextInt(3)];
    }
}

测试:

public class GardenFactoryTest {
    public static void main(String[] args) {
        Garden garden = GardenFactory.getGarden();
        System.out.println(garden.getCenter().getName());
        System.out.println(garden.getBorder().getName());
        System.out.println(garden.getShade().getName());                                                      
    }
}

One of the main purposes of the Abstract Factory is that it isolates the concrete classes that are generated. The actual names of these classes are hidden in the factory and need not be known at the client level.

Because of the isolation of classes, you can change or interchange these product class families freely. Further, since you generate only one kind of concrete class, this system keeps you from inadvertently using classes from different families of products. However, adding new class families takes some effort because you must define new, unambiguous conditions that cause such a new family of classes to be returned.


While all of the classes that the Abstract Factory pattern generates have the same base class, nothing prevents some subclasses from having additional methods that differ from the methods of other classes. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值