抽象工厂模式

抽象工厂模式

介绍

为创建一组相关或相互依赖的对象提供一个接口,而且无需指定他们的具体类。(创建型模式)

和工厂模式区别

工厂模式提供一个产品的结构,而抽象工厂模式提供多个产品的结构,可以组成一个产品族。

QQ换皮肤可以使用抽象工厂模式

优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。缺点:扩展非常麻烦,需要修改很多代码。

DEMO

可以在前一个例子上给图形增加颜色,就相当于一个产品族了:

先创建一个图形和颜色的接口类:

package factory.pattern.abs;

/**
 * Created by FK on 2017/6/18.
 */
public interface Color {
    void fill();
}

package factory.pattern.abs;

/**
 * Created by FK on 2017/6/18.
 */
public interface Shape {
    void draw();
}

然后分别实现各自两个类进行测试:

package factory.pattern.abs;

/**
 * Created by FK on 2017/6/18.
 */
public class Black implements Color {
    @Override
    public void fill() {
        System.out.println("Black : fill()");
    }
}

package factory.pattern.abs;

/**
 * Created by FK on 2017/6/18.
 */
public class Red implements Color {
    @Override
    public void fill() {
        System.out.println("Red : fill()");
    }
}

package factory.pattern.abs;

/**
 * Created by FK on 2017/6/18.
 */
public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Circle : draw()");
    }
}

package factory.pattern.abs;

/**
 * Created by FK on 2017/6/18.
 */
public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Rectangle : draw()");
    }
}

然后实现工厂抽象类:

package factory.pattern.abs;

/**
 * Created by FK on 2017/6/18.
 */
public abstract class AbstractFactory {
    abstract Color getColorFactory(String color);
    abstract Shape getShapeFactory(String shape);
}

然后扩展工厂抽象类:

package factory.pattern.abs;

/**
 * Created by FK on 2017/6/18.
 */
public class ShapeFactory extends AbstractFactory {
    @Override
    Color getColorFactory(String color) {
        return null;
    }

    @Override
    Shape getShapeFactory(String shape) {
        if(shape == null) {
            return null;
        }
        if ("rectangle".equals(shape)){
            return new Rectangle();
        }else if ("circle".equals(shape)){
            return new Circle();
        }else {
            return null;
        }
    }
}

package factory.pattern.abs;

/**
 * Created by FK on 2017/6/18.
 */
public class ColorFactory extends AbstractFactory {
    @Override
    Color getColorFactory(String color) {
        if(color == null) {
            return null;
        }
        if("red".equals(color)){
            return new Red();
        }else if("black".equals(color)){
            return new Black();
        }else {
            return null;
        }
    }

    @Override
    Shape getShapeFactory(String shape) {
        return null;
    }
}

然后写一个工厂创造器:

package factory.pattern.abs;

/**
 * Created by FK on 2017/6/18.
 */
public class FactoryProducer {
    public static AbstractFactory getFactory(String choice){
        if(choice.equalsIgnoreCase("shape")){
            return new ShapeFactory();
        } else if(choice.equalsIgnoreCase("color")){
            return new ColorFactory();
        }
        return null;
    }
}

最后写一个测试类来试试输出:

package factory.pattern.abs;

/**
 * Created by FK on 2017/6/18.
 */
public class AbstractFactoryPattern {

    public static void main(String[] args) {
        AbstractFactory shapeFactory = FactoryProducer.getFactory("shape");
        Shape shape1 = shapeFactory.getShapeFactory("circle");
        shape1.draw();
        Shape shape2 = shapeFactory.getShapeFactory("rectangle");
        shape2.draw();
        AbstractFactory colorFactory = FactoryProducer.getFactory("color");
        Color color1 = colorFactory.getColorFactory("red");
        color1.fill();
        Color color2 = colorFactory.getColorFactory("black");
        color2.fill();
    }

}

输出如果如下:

Circle : draw()
Rectangle : draw()
Red : fill()
Black : fill()

#设计模式#
设计模式介绍 文章被收录于专栏

设计模式是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结。其实就是经过前人反复使用总结使用得出在不同场景有对应的解决方案。

全部评论
抽象工厂模式 是 简单工厂模式的演变
点赞 回复 分享
发布于 2024-04-23 14:25 北京

相关推荐

自从我室友在计算机导论课上听说了“刷 LeetCode 是进入大厂的敲门砖”,整个人就跟走火入魔了一样。他在宿舍门口贴了一张A4纸,上面写着:“正在 DP,请勿打扰,否则 Time Limit Exceeded。”日记本的扉页被他用黑色水笔加粗描了三遍:“Talk is cheap. Show me the code。”连宿舍聚餐,他都要给我们讲解:“今天的座位安排可以用回溯算法解决,但为了避免栈溢出,我建议用动态规划。来,这是状态转移方程:dp[i][j] 代表第 i 个人坐在第 j 个位置的最优解。”我让他去楼下取个快递,他不直接去,非要在门口踱步,嘴里念念有词:“这是一个图的遍历问题。从宿舍楼(root)到驿站(target node),我应该用 BFS 还是 DFS?嗯,求最短路径,还是广度优先好。”和同学约好出去开黑,他会提前发消息:“集合点 (x, y),我们俩的路径有 k 个交点,为了最小化时间复杂度,应该在 (x/2, y/2) 处汇合。”有一次另一个室友低血糖犯了,让他帮忙找颗糖,他居然冷静地分析道:“别急,这是一个查找问题。零食箱是无序数组,暴力查找是 O(n)。如果按甜度排序,我就可以用二分查找,时间复杂度降到 O(log n)。”他做卫生也要讲究算法效率:“拖地是典型的岛屿问题,要先把连通的污渍区块都清理掉。倒垃圾可以用双指针法,一个指针从左往右,一个从右往左,能最快匹配垃圾分类。”现在我们宿舍的画风已经完全变了,大家不聊游戏和妹子,对话都是这样的:“你 Two Sum 刷了几遍了?”“别提了,昨天遇到一道 Hard 题,我连暴力解都想不出来,最后只能看题解。你呢?”“我动态规划还不行,总是找不到最优子结构。今天那道接雨水给我整麻了。”……LeetCode 真的害了我室友!!!
老六f:编程嘉豪来了
AI时代还有必要刷lee...
点赞 评论 收藏
分享
评论
3
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务