Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In my 2d destruction derby simulator I have a CargoTruck class and CargoTruckImpl that implement an interface called Truck. CargoTruck has a reference to CargoTruckImpl which is returned through the CargoTruckImplFactory.

But then I decided to add to this chain by having CargoTruckImpl and CargoTruckMovementImpl use the interface called Movement. CargoTruckImpl has a reference to CargoTruckMovementImpl which is returned back to CargoTruck through the MovementImplFactory.

CargoTruck->CargoTruckImpl->CargoTruckMovmentImpl

FuelTruck->FuelTruckImpl->FuelTruckMovmentImpl

Is adding to the chain like this bad? Am I violating the Separation Principle or the Open-Closed Principle by using Factory classes this way? Is there a better way of doing this?

share|improve this question
7  
If you want to continue getting answers to your questions, I recommend accepting the best answers to your previous questions. –  Mike Partridge Oct 11 '11 at 15:10

2 Answers 2

It's great that you're applying some of the SOLID principles! However, I'd be a little suspicious about the number of Impl classes and Factory classes you're implementing (CargoTruck and CargoTruckImpl sound like the same class to me for example). It sounds like you might want to apply some DI principles here (or at least have something like Guice do some of this heavy lifting for you).

I always go back to Martin Fowler's article on DI when I start to worry about creating too many Factories etc, it might help clarify your thinking in this area.

share|improve this answer

What is MovementImplFactory doing for you here? Abstract it and separate the instances into their domains.

This would have an AbstractMovementImplFactory being extended by CargoTruckMovementImplFactory and FireTrucImplkMovementFactory.

Then have your instances of the Trucks have references to the appropriate factories For example, construct CargoTruckImpl to have a reference to CargoTruckMovementImplFactory. When you need a movement for that truck, it can use its domain-specific factory to build it, and you've separated the movement logic from the truck logic.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.