我知道这个问题已经被解决了很多次,因此很明显,它并不像人们通常认为的那样微不足道......
为了澄清事情,我想我会使用相同的 bean 系列和相同的概念构建三种不同实现(简单工厂、工厂方法、抽象工厂)的示例,
您如何看待以下示例?你觉得它们清晰正确吗?
对于所有示例,我将使用这个 bean 系列
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...");
}
}
简易工厂
/*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;
}
}
工厂方法
//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();
}
}
抽象工厂
对于抽象工厂示例,我添加了这些 bean
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...");
}
}
这里是抽象工厂的例子
//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
}
}