Java常用设计模式————抽象工厂模式

简介

每一个具体工厂类只负责创建抽象产品的某一个具体子类的实例。

与工厂方法模式的区别

工厂方法模式针对的是一个产品等级结构,而抽象工厂模式针对的是多个产品等级结构,因此抽象工厂模式在结构上要比工厂方法模式更加复杂和抽象,也更具一般性。

代码实现

第一步:创建产品族接口

package design.pattern.abstractfactory;

public interface Shape {
    void draw();
}
package design.pattern.abstractfactory;

public interface Color {
    void fill();
}

第二步:创建产品类

package design.pattern.abstractfactory;
/**
 * 矩形类
 * <br>类名:Rectangle<br>
 * 作者: mht<br>
 * 日期: 2018年3月21日-下午9:02:26<br>
 */
public class Rectangle implements Shape{

    @Override
    public void draw() {
        System.out.println("Rectangle drawing...");
    }
}
package design.pattern.abstractfactory;
/**
 * 正方形类
 * <br>类名:Square<br>
 * 作者: mht<br>
 * 日期: 2018年3月21日-下午9:03:13<br>
 */
public class Square implements Shape{

    @Override
    public void draw() {
        System.out.println("Square drawing...");
    }
}
package design.pattern.abstractfactory;
/**
 * 圆形类
 * <br>类名:Circle<br>
 * 作者: mht<br>
 * 日期: 2018年3月21日-下午9:03:43<br>
 */
public class Circle implements Shape{

    @Override
    public void draw() {
        System.out.println("Circle drawing...");
    }
}
package design.pattern.abstractfactory;
/**
 * 红色类
 * <br>类名:Red<br>
 * 作者: mht<br>
 * 日期: 2018年3月21日-下午9:04:23<br>
 */
public class Red implements Color{

    @Override
    public void fill() {
        System.out.println("Red filling....");
    }
}
package design.pattern.abstractfactory;
/**
 * 绿色类
 * <br>类名:Green<br>
 * 作者: mht<br>
 * 日期: 2018年3月21日-下午9:05:08<br>
 */
public class Green implements Color{

    @Override
    public void fill() {
        System.out.println("Green filling...");
    }
}
package design.pattern.abstractfactory;
/**
 * 蓝色类
 * <br>类名:Blue<br>
 * 作者: mht<br>
 * 日期: 2018年3月21日-下午9:05:30<br>
 */
public class Blue implements Color{

    @Override
    public void fill() {
        System.out.println("Blue filling...");
    }
}

第三步:创建工厂族接口

package design.pattern.abstractfactory;
/**
 * 工厂族接口
 * <br>类名:Factory<br>
 * 作者: mht<br>
 * 日期: 2018年3月21日-下午9:05:53<br>
 */
public interface Factory {
    
    Object getPruduct(Class c);
}

第四步:创建工厂类

package design.pattern.abstractfactory;
/**
 * 形状工厂类
 * <br>类名:ShapeFactory<br>
 * 作者: mht<br>
 * 日期: 2018年3月21日-下午9:06:55<br>
 */
public class ShapeFactory implements Factory{

    @Override
    public Shape getPruduct(Class c) {
        if(c == null) return null;
        
        try {
            Shape shape = (Shape) Class.forName(c.getName()).newInstance();
            return shape;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
        return null;
    }
}
package design.pattern.abstractfactory;
/**
 * 颜色工厂类
 * <br>类名:ColorFactory<br>
 * 作者: mht<br>
 * 日期: 2018年3月21日-下午9:07:29<br>
 */
public class ColorFactory implements Factory{

    @Override
    public Color getPruduct(Class c) {
        if(c == null) return null;
        
        try {
            Color color = (Color) Class.forName(c.getName()).newInstance();
            return color;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
        return null;
    }
}

第五步:创建工厂生成类

package design.pattern.abstractfactory;
/**
 * 工厂生成类
 * <br>类名:FactoryProducer<br>
 * 作者: mht<br>
 * 日期: 2018年3月21日-下午9:08:08<br>
 */
public class FactoryProducer {
    
    public static Factory getFactory(Class c){
        if(c == null) return null;

        try {
            Factory f = (Factory) Class.forName(c.getName()).newInstance();
            return f;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }        
        return null;
    }
}

以上五步已经实现了抽象工厂模式,现在来测试一下:

package design.pattern.abstractfactory;

public class testDemo {
    public static void main(String[] args) {
        // 创建形状工厂
        Factory shapeFactory = FactoryProducer.getFactory(ShapeFactory.class);
        // 创建形状产品
        Shape rect = (Shape) shapeFactory.getPruduct(Rectangle.class);
        Shape square = (Shape) shapeFactory.getPruduct(Square.class);
        Shape circle = (Shape) shapeFactory.getPruduct(Circle.class);
        // 使用产品
        rect.draw();
        square.draw();
        circle.draw();

        Factory colorFactory = FactoryProducer.getFactory(ColorFactory.class);

        Color red = (Color) colorFactory.getPruduct(Red.class);
        Color green = (Color) colorFactory.getPruduct(Green.class);
        Color blue = (Color) colorFactory.getPruduct(Blue.class);

        red.fill();
        green.fill();
        blue.fill();
    }
}

运行,输出结果:

Rectangle drawing...
Square drawing...
Circle drawing...
Red filling....
Green filling...
Blue filling...

结构图:


抽象工厂模式的理解

猜你喜欢

转载自blog.csdn.net/u014745069/article/details/79645861
今日推荐