利用泛型的基础工厂实现MVC三层解耦

package com.gus.factory;

import java.io.FileReader;
import java.util.Properties;

public class BasicFactory {
    private static BasicFactory basicFactory = new BasicFactory();
    private static Properties properties;
    private BasicFactory(){}

    public BasicFactory getBasicFactory() {
        return basicFactory;
    }

    static {
        properties = new Properties();
        try {
            properties.load(new FileReader(BasicFactory.class.getClassLoader().getResource("config.properties").getPath()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public <T> T getInstance(Class<T> clazz) {
        try {
            String cName = clazz.getSimpleName();
            String cImplName = properties.getProperty(cName);
            return (T)Class.forName(cImplName).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}



//在ServiceImpl中只需要这么写:
//CustomerDao customerDao = BasicFactory.getBasicFactory().getInstance(CustomerDao.class);

一般的实现解耦的工厂类:

package com.gus.factory;

import com.gus.dao.CustomerDao;
import java.io.FileReader;
import java.util.Properties;

//单例模式,需要对外提供方法
//这个工厂提供了dao层和service的解耦
public class CustomerDaoFactory {
    private static CustomerDaoFactory customerDaoFactory = new CustomerDaoFactory();
    private static Properties properties = null;

    private CustomerDaoFactory() {

    }

    public static CustomerDaoFactory getCustomerFactory() {
        return customerDaoFactory;
    }


    //    读取配置文件
    static {
        properties = new Properties();
        try {
            properties.load(new FileReader(CustomerDaoFactory.class.getClassLoader().getResource("config.properties").getPath()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public CustomerDao getCustomerDao() {
        String clazz = properties.getProperty("CustomerDao");
        try {
            return (CustomerDao) Class.forName(clazz).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}


//在service层需要这么写
CustomerDao customerDao = CustomerDaoFactory.getCustomerFactory().getCustomerDao();

service和dao层需要解耦,web层与service层也需要解耦,

每个解耦都需要构建一个工厂类。

还有一种很基础的实现方式:

    public Object getInstance(String clazz) {
        clazz = properties.getProperty(clazz);
        try {
            return  Class.forName(clazz).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

在service层需要这么写:
CustomerDao customerDao = (CustomerDao) BasicFactory.getBasicFactory().getInstance("CustomDao");
不仅需要将类名变成String参数传进去,还需要强转一下。

所以,就用第一个吧。

转载于:https://my.oschina.net/u/3544275/blog/1504441

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值