javaee 设计模式_JavaEE重新审视设计模式:装饰器
javaee 設(shè)計(jì)模式
去年的這個(gè)時(shí)候,我寫了一系列有關(guān)JavaEE設(shè)計(jì)模式實(shí)現(xiàn)的博客文章。 大約一年后,我意識(shí)到我錯(cuò)過了我最喜歡的圖案裝飾器。
裝飾器模式基本上是通過裝飾其他對(duì)象來(lái)擴(kuò)展對(duì)象功能的方法,其他對(duì)象可以包裝目標(biāo)對(duì)象并為其添加行為。 如果您從未使用過或聽說(shuō)過裝飾器,我強(qiáng)烈建議您閱讀Head First Design Patterns的第3章。
就像我之前的文章中提到的其他模式一樣,JavaEE提供了一種簡(jiǎn)單而優(yōu)雅的方式來(lái)使用裝飾器模式。 讓我們從一個(gè)簡(jiǎn)單的無(wú)狀態(tài)會(huì)話Bean開始。
package com.devchronicles.decorator;import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType;/**** @author murat*/ @Stateless @TransactionAttribute(TransactionAttributeType.REQUIRED) public class EventService {public void startService(){System.out.println("do something important here...");} }要開始實(shí)現(xiàn)裝飾器模式,我們需要一個(gè)接口,以便可以將裝飾器和要裝飾的對(duì)象綁定在一起。
package com.devchronicles.decorator;/**** @author murat*/ public interface ServiceInterface {public void startService(); }接口具有裝飾器將在其上添加功能的方法。 接下來(lái),我們需要對(duì)現(xiàn)有的EventService bean進(jìn)行一些更改以使其可修飾。
package com.devchronicles.decorator;import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType;/**** @author murat*/ @Stateless @TransactionAttribute(TransactionAttributeType.REQUIRED) public class EventService implements ServiceInterface{public void startService(){System.out.println("do something important here...");} }現(xiàn)在我們準(zhǔn)備添加所需的裝飾器。 我們需要做的就是注釋我們的類,實(shí)現(xiàn)ServiceInterface并注入我們的服務(wù)委托。
package com.devchronicles.decorator;import javax.decorator.Decorator; import javax.decorator.Delegate; import javax.inject.Inject;/**** @author murat*/ @Decorator //declares this class as a decorator public class DecoratorService implements ServiceInterface{ //must implement the service interface@Inject //inject the service@Delegate //and annotate as the delegateServiceInterface service;@Overridepublic void startService() { //implement the startService method to add functionalitySystem.out.println("decorating the existing service!");service.startService(); //let the execution chain continue} }幾個(gè)裝飾器可以使用服務(wù)接口。
package com.devchronicles.decorator;import javax.decorator.Decorator; import javax.decorator.Delegate; import javax.inject.Inject;/**** @author murat*/ @Decorator public class Decorator2Service implements ServiceInterface{@Inject@DelegateServiceInterface service;@Overridepublic void startService() {System.out.println("decorating the service even further!!!");service.startService();} }大多數(shù)配置可以通過JavaEE6中的注釋來(lái)完成。 但是,我們?nèi)匀恍枰砑右恍﹛ml配置以使裝飾器起作用。 由于我們已經(jīng)為裝飾器添加了注釋,因此看起來(lái)似乎很令人失望,但是配置仍然非常簡(jiǎn)單,并且需要聲明執(zhí)行順序。 將以下行添加到空的beans.xml中。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"><decorators><class>com.devchronicles.decorator.DecoratorService</class><class>com.devchronicles.decorator.Decorator2Service</class></decorators> </beans>當(dāng)執(zhí)行EventService的startService方法時(shí),裝飾器將裝飾ejb并將其自身的行為添加到執(zhí)行中。
...INFO: WEB0671: Loading application [Decorator] at [/Decorator] INFO: Decorator was successfully deployed in 2,534 milliseconds. INFO: decorating the existing service! INFO: decorating the service even further!!! INFO: do something important here... ...
參考: JavaEE重新審視設(shè)計(jì)模式: Developer Chronicles博客上的JCG合作伙伴 Murat Yener的裝飾器 。
翻譯自: https://www.javacodegeeks.com/2012/10/javaee-revisits-design-patterns-decorator.html
javaee 設(shè)計(jì)模式
總結(jié)
以上是生活随笔為你收集整理的javaee 设计模式_JavaEE重新审视设计模式:装饰器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (spi linux)
- 下一篇: Java时间和日期指南