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

Posted on 2011-10-11
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

    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

    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

    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

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now