设计模式四 工厂模式
0、基本定义
工厂模式定义:一个用于创建对象的接口,让子类决定实例化哪一个类。实例化延迟到其子类。‘
抽象工厂模式:为创建一组相关或相互依赖的对象提供一个接口,而且无须指定他们的具体类。(概念略抽象)
只对结果负责。
解耦合
spring 中 AbstractFactoryBean 创建对象使用了工厂模式,很好的诠释了 依赖倒置原则:要依赖抽象,不要依赖具体。
1、类型
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.2、工厂模式
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 抽象工厂模式:
产品登记,产品族
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; } } }
3、优点
优点:
》良好的封装性,代码结构清晰
》扩展性好
使用场景
》new一个对象的替代品
==================================
抽象工厂模式的概念略抽象, 代码能实现,目前和概念不能有效的结合。。。。。
产品等级
产品族 新增,变动比较大
=================================
参考资料:
咕泡学院
《大话设计模式》