标签:简单工厂设计模式
1)目的就是统一管理访问层的所有Dao,让Service在Dao的处理上相对独立测试类
`import org.junit.Test;
//业务层(不变)
public class Demo1 {
@Test
public void biaosh()
{
Service service = new Service();
service.create();
}
}
`
业务层
/**业务层*/
public class Service {
public void create()
{
DaoFactory daoFactory = DaoFactory.getDaoFactory();
IDao iDao = daoFactory.getDao();
iDao.create();
}
}
工厂层
/*工厂模式 */
public class DaoFactory {
//NO1 外界不能方位,私有的构造函数
private DaoFactory(){}
//NO2 提供对外访问公共类,外界通过类名点访问
public static DaoFactory getDaoFactory(){
if(daoFactory==null){
daoFactory = new DaoFactory();
}
return daoFactory;
}
//NO3 私有静态变量
private static DaoFactory daoFactory;
public IDao getDao(){
return new Daojdbc();
}
}
接口层:
//接口层
public interface IDao {
public void create();
}
访问层:
//访问层
public class Daojdbc implements IDao {
public void create()
{
System.out.println("Daojdbcclass:create()");
}
}
标签:简单工厂设计模式
原文地址:http://blog.51cto.com/357712148/2103997