Need suggestions for my design. Am I using Factory and Impl classes properly?

Posted on 2011-10-11
Medium Priority
Last Modified: 2012-05-12
In my 2d simulator I have a CargoTruckImpl, which is spawned from the TruckImplFactory (that was called by the CargoTruck class), asking the MovementImplFactory for it's CargoTruckMovementImpl. And then I also have a FuelTruckImpl doing the same thing by calling the MovementImplFactory for it's own FuelTruckMovementImpl. So basically I'm doing the following:

CargoTruck->TruckImplFactory->CargoTruckImpl->MovementImplFactory-> CargoTruckMovmentImpl

FuelTruck->TruckImplFactory->FuelTruckImpl->MovementImplFactory-> FuelTruckMovmentImpl

Is this a good or bad design? Am I violating the Seperation Principle or the Open-Closed Principle by doing this? Is there a better way of doing this?

I would prefer a thoughtful conceptual answer. No quick posting or links please.
Question by:Eindoofus
LVL 86

Expert Comment

ID: 36949396
There is not often a reference in an implementation class to its factory. I'd be interested to be shown examples ofwhere that's not the case though

Author Comment

ID: 36949473
I posted that wrong. I meant that CargoTruck is referencing CargoTruckImpl, which both implement an interface called Truck. And that TruckImplFactory is just making the decision to return CargoTuckImpl to CargoTruck when called. Which brings me back to my other questions of how can I make the TruckImplFactory choose between CargoTruckImpl and FuelTruckImpl based on whether CargoTruck or FuelTruck call it?:

LVL 10

Accepted Solution

gordon_vt02 earned 2000 total points
ID: 36955853
First suggestion, append Factory to the name of the interface instead of Impl -- so you'd have TruckFactory and MovementFactory.  That doesn't help with your problem, but will make your code a little easier to understand.

If I'm understanding correctly, you have a TruckFactory that you are calling in various places to create either a CargoTruckImpl or a FuelTruckImpl.  What criteria defines the type of truck you need, or is it specific to the situation?  If all you are doing is replacing calls to 'new XxxTruckImpl' with a factory method, it might not be the best situation for a Factory class.  Factories are normally used when there is some distinguishing characteristic of various implementations, or as CEHJ was indicating in your other question, a need to configure the desired implementation without recompiling the class.

A good example of Factory class usage is the EnumSet object.  The static methods of(...), allOf(), noneOf(), etc. are factory methods because they return a different implementation of EnumSet depending on the number of elements in the Set to provide the best performance.  If there is some way to distinguish between your Truck implementations based on the parameters provided to the factory method, you should make the best choice for implementation based on that; otherwise I'd recommend just using 'new XxxTruck()' in your code and coding to the Truck interface in classes that work with your Trucks.

A quick and dirty way to use the Factory method to simply replace 'new,' assuming you have a default constructor is:

public class TruckFactory {
    public static <T extends Truck> T getTruck(Class<T> truckClass) {
        // you'll have to add try-catch blocks for reflection exceptions
        return truckClass.newInstance();

Open in new window


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question