• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Switch cases VS. Strategy Pattern

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
In my recent project, I am trying to refactor some codes written by others. Basically, in the original codes, there is a large code block use switch cases( do different things based the event type.) The codes like this:
int eventType=obj.getEventType();
switch (eventType){
case DOCONNECT:
...
CASE DISCONNECT:
...
}
There are lot's of the types to switch. And there are possible new types to be added in future. This block of codes is about 300 lines.
I am thinking of use strategy pattern to encapsulate the envent handling into each strategy. So that the above codes can be simplized to the following:
Event aEvent=obj.getEvent();
aEvent.handle();
However, I don't want to create tiny classes that only have two or three lines of event handling code. This may result huge amount of classes.
How can I do? Does the "Bitter Java" address this kind of problem?
Thanks
[ June 04, 2002: Message edited by: Frank Zheng ]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, a strategy pattern will tend toward more "tiny" classes. However, you may soon realize that your strategies are related in a hierarchical way ... or that there is a lot of commonality such that one strategy may be apply to many strategy holders.
Let me say this, don't be afraid to create classes. If they are organized appropriately with proper naming and packaging, and you reuse code where applicable you will find that having a larger number of classes is not a problem
 
Frank Zheng
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill for the quick reply.
I have more confidence on my "Strategy" now. I also applied decorator pattern to reduce the amount of classes that need to be created.
After reading several books about design patterns(like GOF's), I almost instinctly think of applying certain patterns toward certain situations. However, I am not sure whether it is necessaryly good to do so in real world.
I have no knowledge about what anti-pattern is all about. I assume that is regarding to don't apply patterns blindly.
I hope Bruce can help explain how to use patterns wisely and apply it in right place.
Cheers!
 
Sheriff
Posts: 17065
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Frank Zheng:
I have no knowledge about what anti-pattern is all about. I assume that is regarding to don't apply patterns blindly.
Here's a page that has definitions for both patterns and antipatterns: http://www.enteract.com/~bradapp/docs/patterns-intro.html#PatternDefinition
Junilu
 
Frank Zheng
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu,
Thanks for the link! That's very helpful. I am sorry for my ignorance about AntiPattern. I should not assume and just read from the name. I am gona to read more about it.
Thanks again
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic