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

来源:互联网 发布:如何用mac剪辑音频 编辑:程序博客网 时间:2024/06/05 22:53


提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
适用性
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. 

原创粉丝点击