<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
    主站蜘蛛池模板: 亚洲中文字幕不卡无码| 成人黄色免费网址| 四虎影库久免费视频| 亚洲精品自偷自拍无码| 3d成人免费动漫在线观看| 亚洲av无码一区二区三区乱子伦 | 男女免费观看在线爽爽爽视频| 亚洲黄色免费观看| 99久久免费中文字幕精品| 亚洲高清在线mv| 国产人成免费视频网站| 亚洲综合校园春色| 午夜免费福利在线观看| 91av免费观看| 亚洲一区二区三区国产精品无码| 成年女人午夜毛片免费视频| 亚洲日本在线电影| 日本免费福利视频| 伊人久久国产免费观看视频| 自拍偷自拍亚洲精品第1页| 成人久久免费网站| 免费国产成人高清视频网站| ww在线观视频免费观看w| 久久精品国产亚洲一区二区| 0588影视手机免费看片| 亚洲欧美日韩综合久久久| 日韩亚洲国产二区| 亚洲一卡2卡4卡5卡6卡在线99| 国产成人A在线观看视频免费| 自拍偷自拍亚洲精品播放| ww4545四虎永久免费地址| 亚洲性无码AV中文字幕| 免费夜色污私人影院在线观看| 少妇性饥渴无码A区免费| 亚洲国产精品久久网午夜| 久久国产精品免费专区| 亚洲精品成人无码中文毛片不卡| 最近新韩国日本免费观看| 亚洲日日做天天做日日谢| 国产国拍精品亚洲AV片| 国产免费不卡视频|