Facade用的非常的廣了,以前剛接觸的時(shí)候有個(gè)誤解,總覺得Facade是簡單的,而它后面的支撐服務(wù)是復(fù)雜的,對(duì)于客戶來說卻是簡單的,現(xiàn)在來看,不完全對(duì),或者說只是說對(duì)了一半,因?yàn)橛袝r(shí)候恰恰是Facade是復(fù)雜的.
我們舉一個(gè)例子,比如發(fā)送短信,我們一般就定義一個(gè)MessageService的服務(wù)類,里面只提供一個(gè)方法就行了,sendToUser(String phone,String content)
但是到了客戶端的時(shí)候有了自己的 "方言",比如它不是關(guān)心一個(gè)抽象的用戶,它只知道向教師發(fā)送短信,或者向?qū)W生發(fā)送短信,或者向家長發(fā)送短信。
示例如下:

由圖中可以看到,F(xiàn)acade的內(nèi)容非常豐富,而支撐它的服務(wù)類卻很簡單,在開發(fā)過程中我們一般先實(shí)現(xiàn)通用的ServiceA,然后根據(jù)進(jìn)一步的需求做一個(gè)面向具體復(fù)雜的Facade.
在Spring提供的sample里發(fā)現(xiàn)一個(gè)小技巧,就是Facade和ServiceA都是接口,然后提供一個(gè)實(shí)現(xiàn)二者的支撐類:
看起來似乎不錯(cuò),不過仔細(xì)想想個(gè)人認(rèn)為還是不是太好,總的感覺就是層次不清晰,因?yàn)楹芏鄷r(shí)候Facade和Service之間是被服務(wù)與服務(wù)的關(guān)系,所以理當(dāng)分開。 同時(shí),這個(gè)類有點(diǎn)傾向于"萬能類"了,能分還是分開好.這和我們以前提到的dao又背離過來了(以前我們提倡一個(gè)業(yè)務(wù)一個(gè)dao,現(xiàn)在覺得只用一個(gè)通用的dao更合適),這個(gè)并不矛盾,具體問題具體看待.
歡迎各位拍磚.
我們舉一個(gè)例子,比如發(fā)送短信,我們一般就定義一個(gè)MessageService的服務(wù)類,里面只提供一個(gè)方法就行了,sendToUser(String phone,String content)
但是到了客戶端的時(shí)候有了自己的 "方言",比如它不是關(guān)心一個(gè)抽象的用戶,它只知道向教師發(fā)送短信,或者向?qū)W生發(fā)送短信,或者向家長發(fā)送短信。
示例如下:

由圖中可以看到,F(xiàn)acade的內(nèi)容非常豐富,而支撐它的服務(wù)類卻很簡單,在開發(fā)過程中我們一般先實(shí)現(xiàn)通用的ServiceA,然后根據(jù)進(jìn)一步的需求做一個(gè)面向具體復(fù)雜的Facade.
在Spring提供的sample里發(fā)現(xiàn)一個(gè)小技巧,就是Facade和ServiceA都是接口,然后提供一個(gè)實(shí)現(xiàn)二者的支撐類:
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);
????}
}
????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);
????}
}
看起來似乎不錯(cuò),不過仔細(xì)想想個(gè)人認(rèn)為還是不是太好,總的感覺就是層次不清晰,因?yàn)楹芏鄷r(shí)候Facade和Service之間是被服務(wù)與服務(wù)的關(guān)系,所以理當(dāng)分開。 同時(shí),這個(gè)類有點(diǎn)傾向于"萬能類"了,能分還是分開好.這和我們以前提到的dao又背離過來了(以前我們提倡一個(gè)業(yè)務(wù)一個(gè)dao,現(xiàn)在覺得只用一個(gè)通用的dao更合適),這個(gè)并不矛盾,具體問題具體看待.
歡迎各位拍磚.