設計模式四 工廠模式

0、基本定義

工廠模式定義:一個用於建立對象的接口,讓子類決定實例化哪個類。實例化延遲到其子類。‘spring

抽象工廠模式:爲建立一組相關或相互依賴的對象提供一個接口,並且無須指定他們的具體類。(概念略抽象)設計模式

只對結果負責。ide

解耦合優化

spring 中 AbstractFactoryBean 建立對象使用了工廠模式,很好的詮釋了 依賴倒置原則:要依賴抽象,不要依賴具體。spa

一、類型

1.1 、簡單工廠模式:

public class SimpleFactory {


    public Milk getMilk(String name) {

        if ("特侖蘇".equals(name)) {
            return new Telunsu();
        } else if ("伊犁".equals(name)) {
            return new Yili();
        } else if ("蒙牛".equals(name)) {
            return new Mengniu();
        }
        return null;
    }
}

 

1.二、工廠模式

public interface Factory {

    //統一產品出口

    Milk getMilk();
}


public class MengniuFactotry implements Factory {
    @Override
    public Milk getMilk() {
        return new Mengniu();
    }
}


public class TelunsuFactotry implements Factory {
    @Override
    public Milk getMilk() {
        return new Telunsu();
    }
}


public class YiliFactotry implements Factory {
    @Override
    public Milk getMilk() {
        return new Yili();
    }
}

 

Test設計

public class FactoryTest {

    public static void main(String[] args) {

        Factory factory = new MengniuFactotry();
        System.out.println(factory.getMilk().getName());
    }

 

 1.3 抽象工廠模式:

產品登記,產品族code

public abstract class AbstractFactory {

    public  abstract Milk getMengniu();
    public  abstract Milk getYili();
    public  abstract Milk getTelunsu();

}


public class MilkFactory extends AbstractFactory {
    @Override
    public Milk getMengniu() {
        return new Mengniu();
    }

    @Override
    public Milk getYili() {
        return new Yili();
    }

    @Override
    public Milk getTelunsu() {
        return new Telunsu();
    }
}


public class AbstractFactoryTest {

public static void main(String[] args) {


AbstractFactory factory = new MilkFactory();

System.out.println(factory.getMengniu());
}
}
 

 可經過 反射或者配置文件進行 優化代碼對象

public class DataAccess {

    private static String pack = "com.zzf.design.factory.db.";
    private static String db = "SqlServer";//Access

    //配置文件處理
    private static Properties properties = new Properties();
    static {

        //配置文件獲取db
        try {
            ResourceUtils.getFile("classpath:db.properties");
            properties.load(DataAccess.class.getClassLoader().getResourceAsStream("db.properties"));

            db = properties.getProperty("db");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根據 db 參數選擇
    public static IUserDao creatUser() {
        IUserDao userDao = null;

        switch (db) {
            case "SqlServer":
                userDao = new SqlServerUserDao();
                break;
            case "Access":
                userDao = new AccessUserDao();
                break;

        }
        return userDao;
    }

    public static IDepartmentDao creatDepartment() {
        IDepartmentDao departmentDao = null;

        switch (db) {
            case "SqlServer":
                departmentDao = new SqlServerDepartmentDao();
                break;
            case "Access":
                departmentDao = new AccessDepartmentDao();
                break;

        }
        return departmentDao;
    }


    /**
     * 反射實現
     * @return
     */
    public static IUserDao createUserDaoV2() {
        try {
            return (IUserDao) Class.forName(pack + db + "UserDao").newInstance();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static IDepartmentDao createDepartmentDaoV2() {
        try {
            return (IDepartmentDao) Class.forName(pack + db + "DepartmentDao").newInstance();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

 

三、優勢blog

優勢:接口

》良好的封裝性,代碼結構清晰

》擴展性好

 

使用場景

》new一個對象的替代品

 

==================================

抽象工廠模式的概念略抽象, 代碼能實現,目前和概念不能有效的結合。。。。。

產品等級

產品族 新增,變更比較大

=================================

 

參考資料:

咕泡學院

《大話設計模式》

相關文章
相關標籤/搜索