• 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

Design question

 
Ranch Hand
Posts: 37
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I'm reading a design document for good practices in Java and I keep wondering if someone can explain one thing to me.

The problem is how to go about designing a program where you can use any type of vehicles and move them, but each type of vehicle moves differently, i.e a plane or a car.
So they suggest to do it this way:



Then they have 1 class per type of movement:



..... etc etc...

isn't this too much? plus if you have to change it you have to go to each implementation. Why is this better than this:



Where you can change it in just one class. Anyone?

Thanks
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends a lot on the target of your system. If you were modeling say a country-wide rail system, you would naturally have classes such as: FastFreightTrain, LocalFreightTrain, VeryHighSpeedPasssengerTrain, LocalCommuterTrain, Streetcar, etc. and with them, the methods would be clear. But if the domain of your system was such that trains are only a near trivial part, then you might not care, and so your single-class approach might be good enough.

You want your classes to reflect important real world objects in your problem domain. If your problem is about moving say a ton of spice, then you have natural objects such as a SpiceContainer.

 
Ranch Hand
Posts: 33
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pat is quite right.
Also there is one more issue to consider and that is testing your code.
With first approach all of your code is loosely coupled and in case of change in GoByTrainFast implementation
you need not to test all you tested code (GoByTrain) again.
Since Motor class is pretty small now, you may think this as pretty simple to test again and again but as
it grows bigger and bigger it will become a maintenance nightmare.

Please Google "Strategy Pattern" OR you may read 1st chapter of "Head First Design Patterns"

Enjoy...
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Klament J. Kruoghst wrote:



first of all, this is your own implementation . you are not followed any specification/contract which is common to the world. so, no client can use your code. is not it?
 
Ranch Hand
Posts: 96
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Klament J. Kruoghst wrote:


Where you can change it in just one class. Anyone?

Thanks



What if you go by bicycle or surfboard?

+1 to the strategy pattern reference. Very well worth the read, as well all the rest of design patterns!

Cheers,
Wim
 
Klament J. Kruoghst
Ranch Hand
Posts: 37
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, no, i know it's the Strategy Pattern hence why i said I was reading a design document and why im asking why it's better, why send me to google to read what im already reading lol... so i guess the answer is just cause it's a small example it might as well work the Motor class approach, I like the testing answer as well I did not think of that, thanks.

Now for your question:

What if you go by bicycle or surfboard?


Wouldn't that be the same thing? they'll have to create yet another implementation of the IGo interface and i'd create another method. OK maybe Motor wasn't a good name, I might as well call it Go class and it still holds true.

But thanks for the answer to Pat Farrell.
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The real value is in the polymorphic behaviour of method go(), which won't be in the case of class Motor. Consider the following in a piece of code which uses your API:

Now, this would not be easy with the 'Motor' class without letting this code know which mode of transport is actually getting used in each leg of the journey.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic