dimanche 4 mars 2018

My understanding of Factory Method and Abstract Factory pattern

Before you vote to close this question, hear me out. I know there have been numerous questions about this topic here and I've read almost all of them. I am still confused mainly because all the different answers say slightly different things or at least they seem so to me.

In order to really confirm my understanding, I would like to share what I understand to be Factory Method and Abstract Factory pattern. Can the experienced folks please help me and see if I got this right?

I'm sharing only the client code, hopefully that conveys the intent. Do you think my understanding of these patterns is correct, based on this code?

Client code for Simple Factory:

// Simple Factory Pattern - Client Code
VehicleFactory vehicleFactory = new VehicleFactory();

Vehicle car = vehicleFactory.buildVehicle("C"); // Returns a 'Car' instance that extends 'Vehicle'
Vehicle truck = vehicleFactory.buildVehicle("T"); // Returns a 'Truck' instance that extends 'Vehicle'

Client code for Factory Method:

// Factory Method Pattern - Client Code
VehicleFactory carFactory = new CarFactory(); // 'CarFactory' extends abstract class VehicleFactory
VehicleFactory truckFactory = new TruckFactory(); // 'TruckFactory' extends abstract class VehicleFactory

Vehicle car = carFactory.buildVehicle(); // Returns a 'Car' instance that extends 'Vehicle'
Vehicle truck = truckFactory.buildVehicle(); // Returns a 'Truck' instance that extends 'Vehicle'

Client code for Abstract Factory (1st Version):

// Abstract Factory Pattern - Client Code
VehicleFactory toyotaFactory = abstractVehicleFactory.buildToyotaFactory(); // Returns a 'ToyotaFactory' instance that extends 'VehicleFactory'
VehicleFactory hondaFactory = abstractVehicleFactory.buildHondaFactory(); // Returns a 'HondaFactory' instance that extends 'VehicleFactory'

Vehicle toyotaCar = toyotaFactory.buildVehicle("C"); // Returns a 'Car' instance that extends 'Vehicle'
Vehicle toyotaTruck = toyotaFactory.buildVehicle("T"); // Returns a 'Truck' instance that extends 'Vehicle'

Vehicle hondaCar = hondaFactory.buildVehicle("C"); // Returns a 'Car' instance that extends 'Vehicle'
Vehicle hondaTruck = hondaFactory.buildVehicle("T"); // Returns a 'Truck' instance that extends 'Vehicle'

Client code for Abstract Factory (2nd Version):

// Abstract Factory Pattern - Client Code
VehicleFactory toyotaFactory = abstractVehicleFactory.buildToyotaFactory(); // Returns a 'ToyotaFactory' instance that extends 'VehicleFactory'
VehicleFactory hondaFactory = abstractVehicleFactory.buildHondaFactory(); // Returns a 'HondaFactory' instance that extends 'VehicleFactory'

Vehicle toyotaCar = toyotaCarFactory.buildCar(); // Returns a 'Car' instance that extends 'Vehicle'
Vehicle toyotaTruck = toyotaTruckFactory.buildTruck(); // Returns a 'Truck' instance that extends 'Vehicle'

Vehicle hondaCar = hondaCarFactory.buildCar(); // Returns a 'Car' instance that extends 'Vehicle'
Vehicle hondaTruck = hondaTruckFactory.buildTruck(); // Returns a 'Truck' instance that extends 'Vehicle'

Client code for Abstract Factory (3rd Version):

// Abstract Factory Pattern - Client Code
VehicleFactory toyotaCarFactory = abstractVehicleFactory.buildToyotaCarFactory(); // Returns a 'ToyotaCarFactory' instance that extends 'VehicleFactory'
VehicleFactory hondaCarFactory = abstractVehicleFactory.buildHondaCarFactory(); // Returns a 'HondaCarFactory' instance that extends 'VehicleFactory'

VehicleFactory toyotaTruckFactory = abstractVehicleFactory.buildToyotaTruckFactory(); // Returns a 'ToyotaTruckFactory' instance that extends 'VehicleFactory'
VehicleFactory hondaTruckFactory = abstractVehicleFactory.buildHondaTruckFactory(); // Returns a 'HondaTruckFactory' instance that extends 'VehicleFactory'

Vehicle toyotaCar = toyotaCarFactory.buildVehicle(); // Returns a 'Car' instance that extends 'Vehicle'
Vehicle toyotaTruck = toyotaTruckFactory.buildVehicle(); // Returns a 'Truck' instance that extends 'Vehicle'

Vehicle hondaCar = hondaCarFactory.buildVehicle(); // Returns a 'Car' instance that extends 'Vehicle'
Vehicle hondaTruck = hondaTruckFactory.buildVehicle(); // Returns a 'Truck' instance that extends 'Vehicle'

I would appreciate your help in clearing all my confusions.

Aucun commentaire:

Enregistrer un commentaire