<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Kira-2006
    -僅僅是一陣風也罷了,偏偏是這樣永恒, 僅僅是一場夢也罷了,偏偏是如此的真實,
    posts - 4,comments - 7,trackbacks - 0
    解耦合的設(shè)計目標:
        1. 應(yīng)用層解耦合--應(yīng)用邏輯與數(shù)據(jù)邏輯相分離。
        2. 資源層解耦合--邏輯結(jié)構(gòu)與物理結(jié)構(gòu)相分離。

    DAO模式:即Data Accessor模式和Active Domain Object模式。
        Data Accessor模式:實現(xiàn)數(shù)據(jù)訪問和業(yè)務(wù)邏輯的分離。
        Active Domain Object:實現(xiàn)了業(yè)務(wù)數(shù)據(jù)的對象化封裝。
        Domain Object:簡單來講就是對領(lǐng)域內(nèi)涉及的各個數(shù)據(jù)對象,反映到代碼,就是一個擁有相關(guān)屬性的getter,setter方法的java Bean。

        DAO模式通過對業(yè)務(wù)邏輯蹭提供數(shù)據(jù)抽象層接口,實現(xiàn)了以下目標:
            
    1. 數(shù)據(jù)存儲邏輯的分離:通過對數(shù)據(jù)訪問邏輯進行抽象,為上層結(jié)構(gòu)提供抽象化的數(shù)據(jù)訪問接口。
            2. 數(shù)據(jù)訪問底層實現(xiàn)的分離:數(shù)據(jù)訪問劃分為抽象層和實現(xiàn)層,從而分離了數(shù)據(jù)使用和數(shù)據(jù)訪問的底層實現(xiàn)細節(jié)。
            3. 資源管理和調(diào)用的分離。
            4.數(shù)據(jù)抽象:DAO模式通過對底層數(shù)據(jù)的封裝,為業(yè)務(wù)層提供了一個面向?qū)ο蟮慕涌?,使得業(yè)務(wù)邏輯開發(fā)人員可以面向業(yè)務(wù)中的實體進行編程。
            DAO = Data+Accessor+Domain Object

    DAO模式的進一步改良
        Factory模式的引入:
            由于需要針對不同的數(shù)據(jù)庫訪問機制分別提供各個版本的Data Accessor實現(xiàn),自然我們會想到通過java interface定義一個調(diào)用接口,然后針對這個調(diào)用接口實現(xiàn)不同數(shù)據(jù)庫的Data Accessor。通過接口作為調(diào)用界面和實現(xiàn)規(guī)范,可以避免對具體實現(xiàn)的依賴。
            
    public interface CustomerDAO{
        
    public Customer getCustomer(String custID);
        
    public void save(Customer customer);
    }

    作為最常見的創(chuàng)建模式,F(xiàn)actory模式在這里起到連接接口和實現(xiàn)的橋梁作用,通過Factory模式,我們可以根據(jù)具體的需要加載相應(yīng)的實現(xiàn),并將此實現(xiàn)作為所對應(yīng)接口的一個實例提供給業(yè)務(wù)層使用。
    CustomerDAO custDAO = (CustomerDAO)DAOFactory.getDAO(CustomerDAO.class);
    Customer customer 
    = custDAO.getCustomer(custoemrID);
    業(yè)務(wù)邏輯層通過接口調(diào)用底層實現(xiàn),具體的DAO實現(xiàn)類不會出現(xiàn)在我們的業(yè)務(wù)代碼中。而具體實現(xiàn)類在配置文件中加以配置,之后DAOFactory.getDAO方法通過讀取配置文件獲得當前我們期望使用的實現(xiàn)類的類名,再通過java Class動態(tài)加載機制加載返回。
    從而我們的代碼不依賴于某個特定的實現(xiàn)類,只需要在部署的時候在配置文件中指定當前采用的實現(xiàn)類即可。
    public class DAOFactory{
        
    private static HashMap daoMap = null;

        
    //根據(jù)指定的Class來獲取DAO實例
        public static Object getDAO(Class daoInterface){
            initial();
            Object dao
    =daoMap.get(daoInterface);
            
    if(null==dao){
                
    throw new DAOException("No implementation found of DAO interface => "+daoInterface.getName());
            }

            
    return dao;
        }


        
    //初始化DAOFactory,加載DAO interface和 
        
    //implementation到daoMap中
        public static sychronized void initial(){
            
    if(null==daoMap){
                
    //根據(jù)配置文件中加載DAO實現(xiàn)配置
                daoMap = DAOConfig.load();
            }

        }

    }


    //DAOConfig類實現(xiàn)了配置文件的讀取功能,并根據(jù)配文
    //件中的內(nèi)容加載指定的接口和實現(xiàn)
    public class DAOConfig{
        
    private static Logger logger = LogManager.getLogger(DAOConfig.class);
        
    private static final String DAO_CONFIG_FILE="dao.xml";
        
    private static final String DAO_CONFIG_SECTION="DAO";

        
    public static synchronized HashMap load(){
            HashMap map 
    = new HashMap();
            JFigLocator jfigLocator 
    = JFig.getInstance(jfigLocator);
            Properties prop
    =daoConfig.getSectionAsProperties(DAO_CONFIG_SECTION);
            Enumeration enumSection
    =prop.Keys();
            
    while(enumSection.hasMoreElements()){
                String daoIface
    =(String)enumSectioni.nextElement();
                String daoImpl
    =(String)prop.getProperty(daoIface);
                
    try{
                    Class iface
    =ClassToolKit.loadClass(daoIface);
                    Class impl
    =ClassToolKit.loadClass(daoImpl);
                    
    //將接口作為索引,實現(xiàn)作為值。
                    map.put(iface,impl);
                }
    catch(ClassNotFoundException e){
                    logger.debug(
    "No Class Found => "+e);
                 }

            }

            
    return map;
        }

    }

    ClassToolKit.loadClass方法實現(xiàn)了類文件的動態(tài)加載:
    public class ClassToolKit{
        
    public static Class loadClass(String className)throws ClassNotFoundException{
            Class cls
    =null;
            
    try{
                
    //首先嘗試用當前ClassLoader加載
                cls=Thread.currentThread().getContextClassLoader().loadClass(className);
            }
    catch(Exception e){
                e.printStackTrace();
            }

            
    if(cls==null){
                
    //如果通過當前ClassLoader加載失敗,使
                  
    //用系統(tǒng)ClassLoader加載
                cls=Class.forName(className);
            }

            
    return cls;
        }

    }
    這樣,通過接口與實現(xiàn)的分離,并結(jié)合DAOFactory動態(tài)加載實現(xiàn)類,我們就實現(xiàn)了底層訪問實現(xiàn)的參數(shù)化配置功能。從而為增強產(chǎn)品的部署能力提供了強有力的支持。
        經(jīng)過Factory模式的改造,業(yè)務(wù)層代碼進行相應(yīng)的修改:
    public BigDecimal calcAmount(String customerID,BigDecimal amount){
        
    //根據(jù)客戶ID獲得客戶記錄
        CustomerDAO customerDAO=(CustomerDAO)DAOFactory.getDAO(CustomerDAO.class);
        Customercustomer
    =customerDAO.getCustomer(customerID);

        
    //根據(jù)客戶等級獲得打折比率
        PromotionDAO promoDAO=(PromotionDAO)DAOFactory.getDAO(PromotionDAO.class);
        Promotion promotion
    =promoDAO.getPromotion(customer.getLevel());

        
    //累計客戶總消費額,并更新數(shù)據(jù)庫
        customer.setSumAmount(customer.getSumAmount().add(amount));
        customerDAO.save(customer);

        
    //返回打折后金額
        return amount.multiply(promotion.getRatio());

    }
    這段代碼中混雜了數(shù)據(jù)訪問層的內(nèi)容,如DAOFactory.getDAO方法的調(diào)用。

    Proxy模式的引入
        為了保持業(yè)務(wù)邏輯代碼的簡潔,將Factory模式帶來的Bad Smell排除在系統(tǒng)外,引入Proxy模式。
        Proxy模式的作用:通過提供一個中間層(Proxy),將上層調(diào)用接口與下層實現(xiàn)相銜接。
        經(jīng)過Proxy模式改進后的業(yè)務(wù)層代碼:
    public BigDecimal calcAmount(String customerID,BigDecimal amount){
        Customer customer
    =CustomerProxy.getCustomer(customerID);

        Promotion promotion
    =PromotionProxy.getPromotion(customer.getLevel());

        customer.setSumAmount(customer.getSumAmount.add(amount));

        CustomerProxy.save(customer);

        
    return amount.multiply(promotion.getRatio());

    }

    public class CustomerProxy{
        
    //get Customer Object by CustomerID
        public static Customer getCustomer(String customerID){
            customerDAO custDAO
    =(CustomerDAO)DAOFactory.getDAO(CustomerDAO.class);
            
    return custDAO.getCustomer(customerID);
        }


        
    //Save Customer Object to DataBase
        public static void save(Customer customer){
            CustomerDAO custDAO
    =(CUstomerDAO)DAOFactory.getDAO(CustomerDAO.class);
            custDAO.save(customer);
        }

    }

    posted on 2008-05-02 19:33 Kira-2006 閱讀(401) 評論(0)  編輯  收藏 所屬分類: hibernate
    主站蜘蛛池模板: 在线观看成人免费| 亚洲精品专区在线观看| 亚洲精品欧美综合四区| 免费A级毛片无码久久版| a级毛片在线免费| 亚洲一区二区三区91| 亚洲国产一级在线观看| 无码免费一区二区三区免费播放| 国产成人精品日本亚洲网址| 亚洲高清视频一视频二视频三| 久久精品视频免费播放| 亚洲午夜无码久久久久软件| 国产av无码专区亚洲av果冻传媒| 精品免费久久久久久久| 女bbbbxxxx另类亚洲| 亚洲一区精品中文字幕| 日本最新免费不卡二区在线| 99re免费99re在线视频手机版| 亚洲aⅴ无码专区在线观看| 久久久久亚洲AV成人无码| 日本媚薬痉挛在线观看免费| 特级精品毛片免费观看| 特级毛片A级毛片100免费播放| 亚洲小说区图片区| 亚洲乱码中文字幕久久孕妇黑人| 24小时日本在线www免费的| 3344在线看片免费| 国产精品亚洲综合一区在线观看| 911精品国产亚洲日本美国韩国 | 三级网站免费观看| 亚洲精品无码久久久久秋霞 | 亚洲国产成人精品青青草原| 亚洲中文字幕伊人久久无码| 成年女人毛片免费播放人| 日韩精品无码免费一区二区三区| 一二三四在线观看免费中文在线观看| 亚洲а∨天堂久久精品9966| 亚洲综合小说久久另类区 | 无码亚洲成a人在线观看| 久久亚洲精品成人av无码网站| 国产啪亚洲国产精品无码|