工厂模式总结

简单工厂

public interface View {
    void draw();
}
//第一个实现view接口的类
public class ButtonView implements View {
    public ButtonView() {
        System.out.println(  "ButtonView: created");
    }
    @Override
    public void draw() {
        System.out.println(  "draw: ButtonView");
    }
}

//第二个实现view接口的类
public class TextView implements View {
    public ButtonView() {
        System.out.println(  "TextView: created");
    }
    @Override
    public void draw() {
        System.out.println(  "draw: TextView");
    }
}
public class ViewFactory {
    public static final String TAG = "ViewFactory";
    public static View getView(String type) {
        View view = null;
        if (type.equalsIgnoreCase("button")) {
            view = new ButtonView();
        } else if (type.equalsIgnoreCase("text")) {
            view = new TextView();
        } else if (type.equalsIgnoreCase("circle")) {
            view = new CircleView();    //以此实现类似的实现view接口的类
        }
        return view;
    }
}
//使用方法
View view = ViewFactory.getView("button");
view.draw();
View view = ViewFactory.getView("text");
view.draw();

以上方法的缺点是如果新增产品的话,需要修改工厂类中的getView()方法,不符合开闭原则

开闭原则:对扩展开放,对修改关闭。

public class ViewFactory2{
    public static Object getClass(Class clazz){
        Object obj = null;
        try{
            obj = Class.forName(class.getName()).newInstance();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch(InstantiationException e){
            e.printStackTrace();  
        }catch(IllegalAccessException e){
            e.printStackTrace();   
        }
        return obj;
    }
}
//使用
public static void main(String[] args){
    ButtonView btnView = ViewFactory2.getClass(ButtonView.class);
    btnView.draw();
}

上面通过反射的方法实现符合了开闭原则,但是每一次的产品类都是全部路径,也会比较麻烦,如果需要改善的话可以通过反射+配置文件的形式来改善

工厂方法

//定义Reader接口
public interface Reader {
    void read();
}
//实现Reader接口
public class JpgReader implements Reader {
    @Override
    public void read() {
        System.out.print("read jpg");
    }
}
public class PngReader implements Reader {
    @Override
    public void read() {
        System.out.print("read png");
    }
}
//定义工厂类接口
public interface ReaderFactory {
    Reader getReader();
}
public class JpgReaderFactory implements ReaderFactory {
    @Override
    public Reader getReader() {
        return new JpgReader();
    }
}
public class PngReaderFactory implements ReaderFactory {
    @Override
    public Reader getReader() {
        return new PngReader();
    }
}
//调用工厂方法
ReaderFactory factory=new JpgReaderFactory();
Reader  reader=factory.getReader();
reader.read();
ReaderFactory factory=new PngReaderFactory();
Reader  reader=factory.getReader();
reader.read();

抽象工厂模式

//抽象操作控制器
public interface OperationController {
    void control();
}
//抽象界面控制器
public interface UIController {
    void display();
}
//各个系统平台具体操作控制器和界面控制器
//android
public class AndroidOperationController implements OperationController {
    @Override
    public void control() {
        System.out.println("AndroidOperationController");
    }
}
public class AndroidUIController implements UIController {
    @Override
    public void display() {
        System.out.println("AndroidInterfaceController");
    }
}
//ios
public class IosOperationController implements OperationController {
    @Override
    public void control() {
        System.out.println("IosOperationController");
    }
}

public class IosUIController implements UIController {
    @Override
    public void display() {
        System.out.println("IosInterfaceController");
    }
}
//定义一个抽象工厂,该工厂需要可以创建OperationController和UIController
public interface SystemFactory {
    public OperationController createOperationController();
    public UIController createInterfaceController();
}
//在各平台具体的工厂类中完成操作控制器和界面控制器的创建过程
//android
public class AndroidFactory implements SystemFactory {
    @Override
    public OperationController createOperationController() {
        return new AndroidOperationController();
    }

    @Override
    public UIController createInterfaceController() {
        return new AndroidUIController();
    }
}
//ios
public class IosFactory implements SystemFactory {
    @Override
    public OperationController createOperationController() {
        return new IosOperationController();
    }

    @Override
    public UIController createInterfaceController() {
        return new IosUIController();
    }
}

    //客户端调用
    SystemFactory mFactory;
    UIController interfaceController;
    OperationController operationController;

    //Android
    mFactory = new AndroidFactory();
    //Ios
    mFactory = new IosFactory();
    //Wp
    mFactory = new WpFactory();

    interfaceController = mFactory.createInterfaceController();
    operationController = mFactory.createOperationController();
    interfaceController.display();
    operationController.control();

适用场景:
(1)和工厂方法一样客户端不需要知道它所创建的对象的类。
(2)需要一组对象共同完成某种功能时。并且可能存在多组对象完成不同功能的情况。
(3)系统结构稳定,不会频繁的增加对象。(因为一旦增加就需要修改原有代码,不符合开闭原则)

你可能感兴趣的:(工厂模式总结)