• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A question about Strategy Pattern

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic