javaee 设计模式_JavaEE重新审视设计模式:装饰器
javaee 設計模式
去年的這個時候,我寫了一系列有關JavaEE設計模式實現的博客文章。 大約一年后,我意識到我錯過了我最喜歡的圖案裝飾器。
裝飾器模式基本上是通過裝飾其他對象來擴展對象功能的方法,其他對象可以包裝目標對象并為其添加行為。 如果您從未使用過或聽說過裝飾器,我強烈建議您閱讀Head First Design Patterns的第3章。
就像我之前的文章中提到的其他模式一樣,JavaEE提供了一種簡單而優雅的方式來使用裝飾器模式。 讓我們從一個簡單的無狀態會話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...");} }要開始實現裝飾器模式,我們需要一個接口,以便可以將裝飾器和要裝飾的對象綁定在一起。
package com.devchronicles.decorator;/**** @author murat*/ public interface ServiceInterface {public void startService(); }接口具有裝飾器將在其上添加功能的方法。 接下來,我們需要對現有的EventService 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 implements ServiceInterface{public void startService(){System.out.println("do something important here...");} }現在我們準備添加所需的裝飾器。 我們需要做的就是注釋我們的類,實現ServiceInterface并注入我們的服務委托。
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} }幾個裝飾器可以使用服務接口。
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();} }大多數配置可以通過JavaEE6中的注釋來完成。 但是,我們仍然需要添加一些xml配置以使裝飾器起作用。 由于我們已經為裝飾器添加了注釋,因此看起來似乎很令人失望,但是配置仍然非常簡單,并且需要聲明執行順序。 將以下行添加到空的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>當執行EventService的startService方法時,裝飾器將裝飾ejb并將其自身的行為添加到執行中。
...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重新審視設計模式: Developer Chronicles博客上的JCG合作伙伴 Murat Yener的裝飾器 。
翻譯自: https://www.javacodegeeks.com/2012/10/javaee-revisits-design-patterns-decorator.html
javaee 設計模式
總結
以上是生活随笔為你收集整理的javaee 设计模式_JavaEE重新审视设计模式:装饰器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (spi linux)
- 下一篇: (抗ddos系统功能)