| Author |
Strategy Pattern for EJB stateless session beans
|
vShyam Sundar
Ranch Hand
Joined: Oct 13, 2010
Posts: 59
|
|
Hi all,
Can any one help me in understanding how a strategy pattern can be used with stateless session beans for code re usability
Thanks,
Shyam.
|
 |
Hebert Coelho
Ranch Hand
Joined: Jul 14, 2010
Posts: 754
|
|
I use like this: This is how I use it.
|
[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
|
 |
vShyam Sundar
Ranch Hand
Joined: Oct 13, 2010
Posts: 59
|
|
Hebert, Can you explain me in detail?
Thanks,
Shyam.
|
 |
Hebert Coelho
Ranch Hand
Joined: Jul 14, 2010
Posts: 754
|
|
vShyam Sundar wrote:Hebert, Can you explain me in detail?
Thanks,
Shyam.
Which detail do you need? I do use like that, all my façades are stateless that implements an Interface.
Those interfaces are injected inside others Session Beans.
|
 |
vShyam Sundar
Ranch Hand
Joined: Oct 13, 2010
Posts: 59
|
|
|
So by this way you say that. your injected bean acts as a strategy algorithm which can be changed without clients knowledge(In this case bean's knowledge)
|
 |
Hebert Coelho
Ranch Hand
Joined: Jul 14, 2010
Posts: 754
|
|
|
Yes. That is how I see it. [=
|
 |
Roberto Perillo
Bartender
Joined: Dec 28, 2007
Posts: 2216
|
|
Hebert Coelho wrote:Yes. That is how I see it. [=
Hum... in your example, who is the strategy? How do you choose different strategies at runtime?
|
Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
|
 |
Mr sujeet khandelwal
Greenhorn
Joined: Dec 21, 2011
Posts: 13
|
|
Hi Shyam,
As far as i know strategy pattern is not used for code re usability. Strategy pattern is to used to select the algorithm at run time.
In EJB, we can have multiple implementation of business interface and at run time we can decide conditionally which implementation has to be selected.
Thanks
|
 |
Roberto Perillo
Bartender
Joined: Dec 28, 2007
Posts: 2216
|
|
Mr sujeet khandelwal wrote:In EJB, we can have multiple implementation of business interface and at run time we can decide conditionally which implementation has to be selected.
Conditionally? With "ifs"?
|
 |
Hebert Coelho
Ranch Hand
Joined: Jul 14, 2010
Posts: 754
|
|
Mr sujeet khandelwal wrote:As far as i know strategy pattern is not used for code re usability. Strategy pattern is to used to select the algorithm at run time.
Hello, Mr sujeet.
I have read the book HeadFirst Design Pattern and I wrote an post about strategy: Design Pattern: Strategy.
And the Strategy principles are:
Identify everything that changes and isolate it from what does not change.Program to an interface.Give priority to composition rather than inheritance.
As I can see, it is a good pattern that helps with code resuability.
|
 |
Andrew Moko
Ranch Hand
Joined: Dec 16, 2011
Posts: 55
|
|
Hebert Coelho wrote:
Mr sujeet khandelwal wrote:As far as i know strategy pattern is not used for code re usability. Strategy pattern is to used to select the algorithm at run time.
Hello, Mr sujeet.
I have read the book HeadFirst Design Pattern and I wrote an post about strategy: Design Pattern: Strategy.
And the Strategy principles are:
Identify everything that changes and isolate it from what does not change.Program to an interface.Give priority to composition rather than inheritance.
As I can see, it is a good pattern that helps with code resuability.
Seperation of concerns. That was a good read!!
|
 |
Roberto Perillo
Bartender
Joined: Dec 28, 2007
Posts: 2216
|
|
Hebert Coelho wrote:
Hello, Mr sujeet.
I have read the book HeadFirst Design Pattern and I wrote an post about strategy: Design Pattern: Strategy.
And the Strategy principles are:
Identify everything that changes and isolate it from what does not change.Program to an interface.Give priority to composition rather than inheritance.
As I can see, it is a good pattern that helps with code resuability.
Right... in the example of your blog, do you agree that, when you say "engineBehavior = new RegularEngine();", you are not isolating things that change from what does not change? In your example, having an interface or not doesn't make any difference, right? Also, in the second figure of your post, don't you mean "Batman" instead of "Bataman"?
|
 |
Andrew Moko
Ranch Hand
Joined: Dec 16, 2011
Posts: 55
|
|
|
Coming to think of it, with every child object created there's an unused RegularEngine object. That's not a good practice imho
|
 |
Roberto Perillo
Bartender
Joined: Dec 28, 2007
Posts: 2216
|
|
Andrew Gabriel wrote:Coming to think of it, with every child object created there's an unused RegularEngine object. That's not a good practice imho
Agreed. In his examples, objects will also be created unnecessarily.
|
 |
Hebert Coelho
Ranch Hand
Joined: Jul 14, 2010
Posts: 754
|
|
Andrew Gabriel wrote:Coming to think of it, with every child object created there's an unused RegularEngine object. That's not a good practice imho
How you would do it? When a class need to extend another?
|
 |
Roberto Perillo
Bartender
Joined: Dec 28, 2007
Posts: 2216
|
|
Hebert Coelho wrote:How you would do it? When a class need to extend another?
In your examples, you already know the algorithm that you are going to use, so you don't really need to apply the Strategy pattern. This would be over design.
But, one thing that would improve your example would be:
This improves your design, since no objects will be created needlessly. The object model also makes more sense now, because in your domain, toy car is an abstract concept. In order to get the engine, only subclasses of ToyCar will be able to do so, and the getEngine method won't be available in the ToyCar's API.
|
 |
vShyam Sundar
Ranch Hand
Joined: Oct 13, 2010
Posts: 59
|
|
Hebert, as your first reply says. That you will be invoking your session beans again from a bean . Does this not make 'N' number of network calls in the case of EJBs existing in the Remote location.Which in turn leads to poor performance
I think a Session bean can invoke a method in java class where a strategy pattern could be applied--> please comment on this.
Robert and Hebert. Thanks for your valuable replies. I am in the process of learning things.
Thanks,
Shyam.
|
 |
 |
|
|
subject: Strategy Pattern for EJB stateless session beans
|
|
|