Factory工厂模式

工厂模式的核心:提供一个创建对象的功能,不需要关系具体实现,降低模块之间的耦合度

案例:创建UserInfo对象

1、创建UserInfo类

public class UserInfo {

    public UserInfo() {
    }

    public UserInfo(String name) {
        this.name = name;
    }

    public UserInfo(String name, int age) {
        this.name = name;
        this.age = age;
    }

    private String name;

    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2、定义创建UserInfo的接口

public interface UserApi {

    UserInfo create();
}

3、接口实现

UserApiImpl.class

public class UserApiImpl implements UserApi {

    @Override
    public UserInfo create() {
        return new UserInfo("",0);
    }
}

UserApiImpl_A.class

public class UserApiImpl_A implements UserApi {
    @Override
    public UserInfo create() {
        return new UserInfo("A",22);
    }
}

UserApiImpl_B.class

public class UserApiImpl_B implements UserApi {
    @Override
    public UserInfo create() {
        return new UserInfo("B",23);
    }
}

4、简单工厂实现:

public class SimpleFactory {
    public static UserApi createUserApi(){
        return new UserApiImpl();
    }
}

简单工厂调用:

UserApi  userApi = SimpleFactory.createUserApi();
UserInfo user = userApi.create();
System.out.println("SimpleFactory -->"+user.toString());

5、参数工厂实现:

public class ParamterFactory {

    public static UserApi createApi(int parameter){
        switch (parameter){
            case 1:
                return new UserApiImpl_A();

            case 2:
                return new UserApiImpl_B();
        }

        return new UserApiImpl();

    }
}

调用:

src/main/assets/config.properties

UserApi  userApi = ParamterFactory.createApi(1);
UserInfo user = userApi.create();
System.out.println("ParameterFactory -->"+user.toString());

6、配置文件工厂实现:

create_a=com.example.factory.impl.UserApiImpl_A
create_b=com.example.factory.impl.UserApiImpl_B
public class PropertiesFactory {

    public static UserApi createApi(Context context){
        Properties props = new Properties();

        Class clazz = null;
        try {
            //app/src/main/assets
            InputStream  inputStream = context.getAssets().open("config.properties");
            props.load(inputStream);
            clazz = Class.forName(props.getProperty("create_b"));

            return (UserApi)clazz.newInstance();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        return null;
    }
}

调用:

UserApi userApi = PropertiesFactory.createApi(this);
UserInfo user = userApi.create();
Log.d("PropertiesFactory -->",user.toString());

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