设计模式之工厂家族

《冒号课堂编程范式与OOP思想》 13.1 创建模式笔记

工厂家族

构造器的弊端:名字必须与类名一致,缺乏表现力;每次调用都会创建新对象;无法多态,new 必须使用具体类型,没法使用抽象的超类型。

抽象工厂模式:
1. 把静态工厂拆分成了一个接口和若干个实现类;
2. 把工厂方法模式中的主题类中的抽象工厂方法提炼为一个接口,用对象合成取代了类继承。

代码说明示例

1. 静态工厂模式:

public class StaticFactory {
    public enum Type {
        AWT, SWING
    };

    public static Container createFrame(Type type, String title) {
        switch (type) {
            case AWT:
                return new Frame(title);
            case SWING:
                return new JFrame(title);
            default:
                return null;
        }
    }

    public static Component createLabel(Type type, String text) {
        switch (type) {
            case AWT:
                return new Label(text);
            case SWING:
                return new JLabel(text);
            default:
                return null;
        }
    }
}

// 使用静态公共方法的类
class LoginForm {
    public Container createLoginWindow() {
        // 使用前要指定具体的类型
        StaticFactory.Type type = StaticFactory.Type.AWT;
        Container frame = StaticFactory.createFrame(type, "标题");
        Component label = StaticFactory.createLabel(type, "文本");
        // 组装组件
        return null;
    }
}

每个创建对象的方法都通过参数来指定要创建的具体对象类型,调用方必须指定具体的类型。

2. 工厂方法模式:

public abstract class LoginForm {
    public abstract Container createFrame(String title);

    public abstract Container createLabel(String text);

    // 一个使用抽象方法的模板方法
    public Container createLoginForm() {
        Container frame = createFrame("工厂方法");
        Container label = createLabel("");
        // 组装对象
        return null;
    }
}

public class AwtLoginForm extends LoginForm {
    public Container createFrame(String title) {
        return null;
    }

    public Container createLabel(String text) {
        return null;
    }
}

public class SwingLoginForm extends LoginForm {
    public Container createFrame(String title) {
        return null;
    }

    public Container createLabel(String text) {
        return null;
    }
}

通过多态取代了静态工厂方法里每个方法类型参数;

3. 抽象工厂模式:

interface WigetFactory {
    Container createFrame(String title);

    Container createLabel(String text);
}

class AwtWigetFactory implements WigetFactory {
    @Override
    public Container createFrame(String title) {
        return null;
    }

    @Override
    public Container createLabel(String text) {
        return null;
    }
}

class SwingWigetFactory implements WigetFactory {
    public Container createFrame(String title) {
        return null;
    }

    public Container createLabel(String text) {
        return null;
    }
}

class LoginForm {
    private WigetFactory wigetFactory;

    public LoginForm(WigetFactory wigetFactory) {
        this.wigetFactory = wigetFactory;
    }

    public Container createLoginForm() {
        Container frame = wigetFactory.createFrame("标题");
        Container label = wigetFactory.createLabel("文本");
        // 组装对象
        return null;
    }
}

通过依赖注入把具体的工厂实现注入到使用方,用组合取代了类的继承。


欢迎关注我的微信公众号: coderbee笔记,可以更及时回复你的讨论。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据