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

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

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

    每日一得

    不求多得,只求一得 about java,hibernate,spring,design,database,Ror,ruby,快速開發
    最近關心的內容:SSH,seam,flex,敏捷,TDD
    本站的官方站點是:顛覆軟件

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      220 隨筆 :: 9 文章 :: 421 評論 :: 0 Trackbacks
    Facade用的非常的廣了,以前剛接觸的時候有個誤解,總覺得Facade是簡單的,而它后面的支撐服務是復雜的,對于客戶來說卻是簡單的,現在來看,不完全對,或者說只是說對了一半,因為有時候恰恰是Facade是復雜的.

    我們舉一個例子,比如發送短信,我們一般就定義一個MessageService的服務類,里面只提供一個方法就行了,sendToUser(String phone,String content)
    但是到了客戶端的時候有了自己的 "方言",比如它不是關心一個抽象的用戶,它只知道向教師發送短信,或者向學生發送短信,或者向家長發送短信。

    示例如下:

    facade.png
    由圖中可以看到,Facade的內容非常豐富,而支撐它的服務類卻很簡單,在開發過程中我們一般先實現通用的ServiceA,然后根據進一步的需求做一個面向具體復雜的Facade.



    在Spring提供的sample里發現一個小技巧,就是Facade和ServiceA都是接口,然后提供一個實現二者的支撐類:


    public?class?PetStoreAnnotationImpl?implements?PetStoreFacade,?OrderService?{

    ????
    private?AccountDao?accountDao;

    ????
    private?CategoryDao?categoryDao;

    ????
    private?ProductDao?productDao;

    ????
    private?ItemDao?itemDao;

    ????
    private?OrderDao?orderDao;


    ????
    //-------------------------------------------------------------------------
    ????
    //?Setter?methods?for?dependency?injection
    ????
    //-------------------------------------------------------------------------

    ????
    public?void?setAccountDao(AccountDao?accountDao)?{
    ????????
    this.accountDao?=?accountDao;
    ????}

    ????
    public?void?setCategoryDao(CategoryDao?categoryDao)?{
    ????????
    this.categoryDao?=?categoryDao;
    ????}

    ????
    public?void?setProductDao(ProductDao?productDao)?{
    ????????
    this.productDao?=?productDao;
    ????}

    ????
    public?void?setItemDao(ItemDao?itemDao)?{
    ????????
    this.itemDao?=?itemDao;
    ????}

    ????
    public?void?setOrderDao(OrderDao?orderDao)?{
    ????????
    this.orderDao?=?orderDao;
    ????}


    ????
    //-------------------------------------------------------------------------
    ????
    //?Operation?methods,?implementing?the?PetStoreFacade?interface
    ????
    //-------------------------------------------------------------------------

    ????
    public?Account?getAccount(String?username)?{
    ????????
    return?this.accountDao.getAccount(username);
    ????}

    ????
    public?Account?getAccount(String?username,?String?password)?{
    ????????
    return?this.accountDao.getAccount(username,?password);
    ????}

    ????
    public?void?insertAccount(Account?account)?{
    ????????
    this.accountDao.insertAccount(account);
    ????}

    ????
    public?void?updateAccount(Account?account)?{
    ????????
    this.accountDao.updateAccount(account);
    ????}

    ????
    public?List?getUsernameList()?{
    ????????
    return?this.accountDao.getUsernameList();
    ????}

    ????
    public?List?getCategoryList()?{
    ????????
    return?this.categoryDao.getCategoryList();
    ????}

    ????
    public?Category?getCategory(String?categoryId)?{
    ????????
    return?this.categoryDao.getCategory(categoryId);
    ????}

    ????
    public?List?getProductListByCategory(String?categoryId)?{
    ????????
    return?this.productDao.getProductListByCategory(categoryId);
    ????}

    ????
    public?List?searchProductList(String?keywords)?{
    ????????
    return?this.productDao.searchProductList(keywords);
    ????}

    ????
    public?Product?getProduct(String?productId)?{
    ????????
    return?this.productDao.getProduct(productId);
    ????}

    ????
    public?List?getItemListByProduct(String?productId)?{
    ????????
    return?this.itemDao.getItemListByProduct(productId);
    ????}

    ????
    public?Item?getItem(String?itemId)?{
    ????????
    return?this.itemDao.getItem(itemId);
    ????}

    ????
    public?boolean?isItemInStock(String?itemId)?{
    ????????
    return?this.itemDao.isItemInStock(itemId);
    ????}

    ????
    public?void?insertOrder(Order?order)?{
    ????????
    this.orderDao.insertOrder(order);
    ????????
    this.itemDao.updateQuantity(order);
    ????}

    ????
    public?Order?getOrder(int?orderId)?{
    ????????
    return?this.orderDao.getOrder(orderId);
    ????}

    ????
    public?List?getOrdersByUsername(String?username)?{
    ????????
    return?this.orderDao.getOrdersByUsername(username);
    ????}

    }


    看起來似乎不錯,不過仔細想想個人認為還是不是太好,總的感覺就是層次不清晰,因為很多時候Facade和Service之間是被服務與服務的關系,所以理當分開。 同時,這個類有點傾向于"萬能類"了,能分還是分開好.這和我們以前提到的dao又背離過來了(以前我們提倡一個業務一個dao,現在覺得只用一個通用的dao更合適),這個并不矛盾,具體問題具體看待.

    歡迎各位拍磚.
    posted on 2006-10-09 22:27 Alex 閱讀(1383) 評論(2)  編輯  收藏 所屬分類: design

    評論

    # re: 關于Facade的應用 2006-10-11 10:15 123bingbing
    找資料,接項目,MyLinux軟件加工廠為你解決一切難題。  回復  更多評論
      

    # re: 關于Facade的應用 2006-10-14 22:51 山風小子
    “具體問題具體看待”,您說的很有道理!  回復  更多評論
      

    主站蜘蛛池模板: 香蕉视频亚洲一级| 国产成人综合久久精品亚洲| 亚洲综合色视频在线观看| 免费可以看黄的视频s色| 18禁止看的免费污网站| 99re6在线视频精品免费| 污污污视频在线免费观看| 亚洲国产精品无码久久青草| 免费高清av一区二区三区| 69xx免费观看视频| 亚洲日韩精品无码专区加勒比☆| 三上悠亚亚洲一区高清| 中文字幕久久亚洲一区| 日本高清不卡aⅴ免费网站| 国产做国产爱免费视频| 久久免费看少妇高潮V片特黄| 亚洲精品国产肉丝袜久久| 成年在线网站免费观看无广告| 亚洲男人av香蕉爽爽爽爽| 日韩电影免费在线观看网站| 在线涩涩免费观看国产精品 | 亚洲性69影院在线观看| 日本不卡视频免费| 国产做床爱无遮挡免费视频| 亚洲AV无码日韩AV无码导航| 国产日产成人免费视频在线观看| 亚洲AV无码一区二区三区DV| 亚洲黄色片免费看| 国产亚洲美女精品久久久2020| 亚洲乱码在线卡一卡二卡新区| 成人免费毛片观看| 欧洲精品99毛片免费高清观看| 国产成人精品123区免费视频| 无码毛片一区二区三区视频免费播放 | 久久亚洲精品无码| 国产成人A人亚洲精品无码| 久久爰www免费人成| 激情综合亚洲色婷婷五月| 毛片在线播放免费观看| 中文字幕不卡亚洲| 国产一卡二卡四卡免费|