开发者

How can i use a Action subclass in Struts as a Facade?

开发者_如何转开发Also how can Business Delegator pattern can help me ?


I have been using struts for a little while now and usually went with putting all the business code in the action class itself. Now somebody said to me that this is a bad practice

Yes it is.

and i should use the facade and business delegator pattern.

I don't know about that, maybe yes maybe no, it depends on your application. Ask this person to detail what he/she meant by that.

A facade is used in the business side to provide a simpler access interface to a complex system (more complex object, set of objects, other inner-systems etc) hiding the complexity and interactions of that system from the client of the system.

A business delegate is used on the presentation side to again hide the complexity of business components called by the client (managing the lookup of the business components by using a Service Locator, managing network calls, exceptions etc).

So basically a Facade and a Business Delegate go hand in hand to more loosely link a client with the business services it uses.

Now, if you consider that Struts is a MVC type application, I don't really see how the Facade and Business Delegate fit together. That's why you should ask the person to detail.

In Struts you have :

  • a Model which is responsible for the domain logic
  • a View which is responsible for presenting the state of the model to the user and allow for interactions with the user;
  • a Controller which acts like the glue between the Model and the View transforming the user requests in actions to be performed by the Model and afterwords selecting the next View to display the new state of the Model.

All the domain logic should be in the Model. Your Action class is not part of the Model, it is part of the Controller. See those HttpServletRequest and HttpServletResponse objects on the execute method? Does that sound like domain logic related stuff? No!

You don't put all your business rules inside the Action class. In the Action class you delegate to the classes that take care of the domain logic. This normally happens in a Service Layer.

The service layer communicates with the outside world with DTOs (usually POJOs) and does not care about its callers type (HTTP web apps, desktop applications etc). The Action class should convert HttpServletRequest params to the proper type expected by the service layer and give it full control. When the MOdel returns, it sends the result to the View.

This is a good practice because you can now reuse your Service Layer and the domain logic it contains, its no longer tied to some web framework (namely Struts).

P.S. One word of advice. Don't use the ActionForm objects as DTOs for the service layer. People believe that ActionForms belong to the Model but that is not true. The ActionForms are the black sheep of Struts, they are not POJOs but are loosely coupled to Struts since you have to extend a struts ActionForm class to make them work and that means a tight couple with the framework.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜