SimpleFactory,FactoryMethod,抽象工厂(实例)

时间:2014-03-16 15:36:45

标签: java design-patterns abstract-factory factory-method

我知道有问题的问题已经多次得到解决,显而易见的是,它并不像通常认为的那样微不足道......

为了澄清事情,我想我会使用相同的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...");
    }
}

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;
   }
}   

工厂方法

//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
   }
}

1 个答案:

答案 0 :(得分:2)

你的例子很好。我建议只使用常量甚至enum作为标准,以避免记住字符串并避免拼写错误。

像:

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;
   }
}

客户代码:

VehicleFactory.getVehicle(VehicleFactory.Criteria.SMALL);

而且,我认为你的意思是“昂贵的”#39;因为选择了“廉价”。但确定汽油也是膨胀的:)