Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

DDD - How to solve  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Currently I�m going through old exercises ... and I'm stuck @ the following problem:

Given Situation and Code:

Preset Code:



This is the Relation to be implemented in Java ( I know there is no multipble inheritance )



I need to write a code for the classes CombustionMotor, ElectroMotor and HybridEngine for building a class HybridCar using the HybridEngine

Anyone got a clue ?

cab
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a homework assignment to me.

What work have you done so far?
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would suggest your design is wrong. a hybrid car does not have ONE engine that is both combustion AND electric. it has two separate engines, independant of one another, that can both power the car.

i don't think there is a hybrid engine.

now, if you HAVE to do something like this, maybe you could do it with a couple of interfaces. create a combustionMotor interface, and an electricMotor interface. then your hybrid engine would implement both.

note that you need to carefully think about what methods should be in each interface, and how they should be named...
 
Andreas Reisser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its no homework, i�m preparing myself for a test (Uni Regensburg) ... and i was not able to solve this problem.

I tried to , but i�m not shure IF � can build a superclass ( fe. CombustionMotor IS-A VehicleEngine and ElectroMotor IS-A Vehicle Engine ... but VehicleEngine is meant to be an interface .... )

I don�t know how to strucurize the whole thing.
 
Andreas Reisser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ fred : these are the given Guidelines =)

My approach to this problem:

HybridEngine HAS-A CombustionMotor und HAS-A ElectroMotor

So Hybrid Engine is a seperate class, CombustionMotor und ElectroMotor are both subclass of ... maybe a superclass called Engine.

HybridEngine has two ReferenceVariable, one containing an instance of ElectroMotor udn one containing an Instance of CombustionMotor.

VehicleEngine is an interface.

Here i got stuck ...
[ August 09, 2006: Message edited by: Andreas Reisser ]
 
Andreas Reisser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...


and HybridEngine implements VehicleEngine ... right ?
 
Andreas Reisser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now, if you HAVE to do something like this, maybe you could do it with a couple of interfaces. create a combustionMotor interface, and an electricMotor interface. then your hybrid engine would implement both.


But how can i map the relation between combustionMotor interface, electricMotor interface and VehicleEngine ?

Is there something like:

imterface electricMotor extends VehicleEngine ?

Because VehicleEngine is in the Guidelines as: public interface VehicleEngine { } ...

I don't think so ... like I said ... i�m stuck
[ August 09, 2006: Message edited by: Andreas Reisser ]
 
Andreas Reisser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another approach:

interface VehicleEngine {} ( given guideline )

interface CombustionMotor implements VehicleEngine {} // <- is soimething like that possible ?
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

interface CombustionMotor implements VehicleEngine {}

and now the answer all the posters like to hear: have you tried :-)


no this does not work. CombustionMotor can not implement the methods described in VehicleEngine as it is an interface. but the following will work:



If you need to folow the (somewhat strange design) then your aproach sounds ok to me:

- an interface for VehicleEngine, as well as an implementation as base class
- interfaces for CombustionMotor and ElectroMotor as well as implementations
- interface for HybridEngine implementing the CombustionMotor and ElectorMotor interface and delegates the calls to instances of the two engine types (composition)

possible problems: if both engine implementation contain a startEngine() method (since this method is defined in VehicleEngine) which engine do you start in your HybridEngine startEngine() implementation ?
and if you ask your engine how strong are you (horsepower) then which value do you return ? just one, both summed up ?

i�m preparing myself for a test (Uni Regensburg)

sag den leuten da sie haben komische motoren :-)

cheers

pascal
 
Andreas Reisser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pascal can you please check your PMs ?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andreas Reisser:
Preset Code:



This is the Relation to be implemented in Java ( I know there is no multipble inheritance )



I need to write a code for the classes CombustionMotor, ElectroMotor and HybridEngine for building a class HybridCar using the HybridEngine

Anyone got a clue ?

cab[/QB]

  • CombustionMotor and ElectroMotor implement VehicleEngine
  • HybridEngine is composed of CombustionMotor and ElectroMotor
  • HybridCar is composed of CombustionMotor ,HybridEngine and ElectroMotor

  • Am I right?
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!