wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes A question about Strategy Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "A question about Strategy Pattern" Watch "A question about Strategy Pattern" New topic
Author

A question about Strategy Pattern

Ricky Wang
Greenhorn

Joined: Oct 27, 2003
Posts: 14
The seventh consequence of "Strategy Pattern" referred in the "Design Pattern" of Gang of Four is of "Increased number of objects".

Authors suggest you may alleviate it by "stateless objects" that contexts can share. The "residual state" us maintained by the context,...

What are the "stateless objects" as well as "residual state" they refer to as?

Could anyone give me an example?

Thanks a lot.
Ricky.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Interesting. I guess if a strategy is stateless - no member variables that hold data between method calls - it could be a Singleton or Just Create One. The data that it needs to do its job would all be passed as arguments and used in a thread-safe way.

I hope that parameter passing was what "residual state" meant. A quick Google on "residual state" comes up with some electrical stuff and a synonym for "Schizophrenic defect". Probably not what they meant.
[ May 09, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Hi "Ricky",

There aren't many rules around here, however adherence to the JavaRanch Naming Policy is one of them. Please adjust your display name. Your cooperation is appreciated.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by Stan James:
I hope that parameter passing was what "residual state" meant.

Most likely. It could refer to the data structure(s) that is (are) left over after you strip away the behaviour - it's that data that the context would have to provide to the "stateless object".

However ultimately I would think that such tactic is self-defeating. If you integrate data of any complexity into a larger pre-existing context object you are sacrificing proper segregation. And if you don't want to sacrifice that you end up more objects - n objects with state vs. 1 stateless object and n "residual state" objects - the only way to reduce the number of simultaneous instances is by making the "residual state" instances transient:
  • The strategy client creates the "processing context object" and initializes the "input data"
  • The strategy client invokes the strategy object's method providing this "processing context object".
  • During processing the strategy object extracts the "input data" from the "processing context object", saves intermediate results to the "processing context object" and finally places the results in the "processing context object".
  • The strategy client extracts the results from the "processing context object" and finally discards the "processing context object".

  • Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    Singleton or Just Create One would work fine for Functor style strategies. Many of the strategies I've used over the years get what they need as arguments and could be made thread safe easily enough. In fact, here are the (perhaps poorly named) commands in my Wiki. They are not Singletons, but no other code references the implementation classes.

    [ May 10, 2007: Message edited by: Stan James ]
    Ricky Wang
    Greenhorn

    Joined: Oct 27, 2003
    Posts: 14
    Originally posted by Peer Reynders:
    Hi "Ricky",

    There aren't many rules around here, however adherence to the JavaRanch Naming Policy is one of them. Please adjust your display name. Your cooperation is appreciated.


    Oh, Sure! I have never noticed that before.

    Ricky.
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    Heh heh, just noticed that my commands are Just Create Two or Three sometimes. I really thought about making a local variable so I could use the same instance two or three times but it blew the looks of the method.
     
     
    subject: A question about Strategy Pattern
     
    Similar Threads
    class diagram of MVC
    Looking for a Pattern
    any article about state, strategy and vistor pattern?
    State Vs Stratergy Pattern
    difference between state and strategy design pattern