Reputation: 1357
I know that the issue in question has been addressed numerous times, it is evident therefore, that it is not as trivial as it is often thought ...
To clarify things, I thought I'd build the examples of the three different implementations(simple factory, factory method, abstract factory) using the same family of bean and the same concept,
What do you think of the following examples? You find them clear and correct?
For all the example i'll use this family of beans
interface Vehicle
{
public void drive();
public void clean();
}
class Car implements Vehicle
{
@Override
public void drive()
{
System.out.println("Driving a car...");
}
@Override
public void clean()
{
System.out.println("Cleaning a car...");
}
}
class Bus implements Vehicle
{
@Override
public void drive()
{
System.out.println("Driving a Bus...");
}
@Override
public void clean()
{
System.out.println("Cleaning a Bus...");
}
}
SIMPLE FACTORY
/*client snippet*/
Vehicle car = VehicleFactory.getVehicle("small");
/*our factory*/
class VehicleFactory
{
public static Vehicle getVehicle(String criteria)
{
if ( criteria.equals("small") )
return new Car();
else if ( criteria.equals("big") )
return new Bus();
return null;
}
}
FACTORY METHOD
//client Class
public class FactoryMethodPattern
{
public static void main(String[] args)
{
//handleCreator(new ConcreteCreator1())
handleVehicle(new CarFactory());
handleVehicle(new BusFactory ());
}
//handleCreator(Creator creator)
static void handleVehicle(VehicleDriver2 vDriver)
{
System.out.println("Handling a new vehicle. Pre lambda way");
vDriver.driveVehicle();
vDriver.cleanVehicle();
}
}
//client snippet
VehicleFactory v = new CarFactory();
Vehicle vehicle = v.getVehicle();
OR
VehicleFactory v = new CarFactory();
v.driveVehicle();
//creator
abstract class VehicleFactory
{
//the abstract method of the creator
public abstract Vehicle getVehicle();
public void driveVehicle()
{
getVehicle().drive();
}
public void cleanVehicle()
{
getVehicle().clean();
}
}
//concrete creator
class CarFactory extends VehicleFactory
{
@Override
public Vehicle getVehicle()
{
return new Car();
}
}
//concrete creator
class BusFactory extends VehicleFactory
{
@Override
public Vehicle getVehicle()
{
return new Bus();
}
}
ABSTRACT FACTORY
For the abstract factory example, i've add those beans
interface Fuel
{
public void combust();
}
class Petrol implements Fuel
{
@Override
public void combust()
{
System.out.println("Petrol...");
}
}
class Gas implements Fuel
{
@Override
public void combust()
{
System.out.println("Gas...");
}
}
And here comes the Abstract factory example
//AbstractFactory
public abstract class AbstractFactory
{
abstract Fuel getFuel(String criteria);
abstract Vehicle getVehicle(String criteria) ;
}
//ConcreteFactory
public class VehicleFactory extends AbstractFactory
{
@Override
public Vehicle getVehicle(String criteria)
{
if(criteria == null)
{
return null;
}
if(criteria.equals("BIG"))
{
return new Bus();
}
else if(criteria.equals("SMALL"))
{
return new Car();
}
return null;
}
@Override
Color getFuel(String criteria)
{
return null;
}
}
//ConcreteFactory
public class FuelFactory extends AbstractFactory
{
@Override
public Vehicle getVehicle(String criteria)
{
return null;
}
@Override
Fuel getFuel(String criteria)
{
if(criteria == null)
{
return null;
}
if(criteria.equals("CHEAP"))
{
return new Gas();
}
else if(criteria.equals("EXPANSIVE"))
{
return new Petrol();
}
return null;
}
}
public class FactoryProducer
{
public static AbstractFactory getFactory(String criteria)
{
if(criteria.equalsIgnoreCase("VEHICLE"))
{
return new VehicleFactory();
}
else if(criteria.equalsIgnoreCase("FUEL"))
{
return new FuelFactory();
}
return null;
}
}
//client class
public class AbstractFactoryPatternDemo
{
public static void main(String[] args)
{
AbstractFactory vehicleFactory = FactoryProducer.getFactory("VEHICLE");
Vehicle car = vehicleFactory.getVehicle("SMALL");
car.dirve();
AbstractFactory fuelFactory = FactoryProducer.getFactory("FUEL");
Fuel petrol = colorFactory.getFuel("EXPANSIVE");
petrol.combust
}
}
Upvotes: 1
Views: 546
Reputation: 2509
Your examples are nice. I'd only suggest to use constants or even an enum
for criteria to avoid having to remember the strings and to avoid misspellings.
Something like:
public class VehicleFactory {
public enum Criteria{
SMALL,BIG
}
public static Vehicle getVehicle(Criteria criteria)
{
if ( criteria.equals(Criteria.SMALL) )
return new Car();
else if ( criteria.equals(Criteria.BIG) )
return new Bus();
return null;
}
}
And client code:
VehicleFactory.getVehicle(VehicleFactory.Criteria.SMALL);
Moreover, I think you mean 'Expensive' as oposed to 'Cheap' but sure that petrol is expansive too :)
Upvotes: 2