This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about Strategy Pattern

 
Ricky Wang
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic