工厂模式

工厂模式:

 工厂模式的构想:
   工厂类自主生产产品,而不用其它类去new 一个产品.
   
工厂模式可以分为三类:
   1. 简单工厂模式(Simple Factory)
   2. 工厂方法模式 (Factory Method)
   3. 抽象工厂模式 (Abstract Factory)
将简单工厂模式(Simple Factory)看为工厂方法模式的一种特例,两者归为一类

工厂方法模式和抽象工厂模式的区别:
   工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个.
   工厂方法模式的具体工厂类只能创建一个具体产品类的实例,而抽象工厂模式可以创建多个.
   
简单工厂模式实例:

//(1)创建一个接口
public interface CarInterf {
    public abstract void createCar();
   }
//(2)创建实现接口的实现体
 public class JeepCar implement CarInterf {
     @Override
     public void createCar() {
        System.out.println("我是小吉普");    
     }
   }
//(3)创建一个工厂,生成基于给定信息的实体类的对象
 public class CarFactory {
    public static CarInterf getCar(){
         return new JeepCar();
        }
      }
//(4)ApplicationContext.xml
//IOC 通过静态工厂模式创建对象,根据 id 获取到工厂的类名
// 获取类名之后调用factory-method方法(静态方法)
<bean id = "car" class = "com.xalo.action.CarFactory" factory-method = "getCar">
<bean>
//(5)单元测试
public class DITest {
    @Test
 public void test() {
   ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
   CarInterf car = (CarInterf) context.getBean("car");
           car.createCar();
        }
     } 

工厂方法模式实例:

//(1)创建一个接口
public interface CarInterf {
    public abstract void createCar();
   }
//(2)创建实现接口的实现体
 public class JeepCar implement CarInterf {
     @Override
     public void createCar() {
        System.out.println("我是小吉普");    
     }
   }
//(2)创建实现接口的实现体
public class SUVCar implements CarInterf{
    @Override
    public void createCar() {
        System.out.println("我是SUV");
    }
}
//(3)创建一个工厂,生成基于给定信息的实体类的对象
public class Carfactory {
   public CarInterf getCar(String type) {
      switch(type) {
          case "jeep":
            return new JeepCar();
          case "suv":
            return new SUVCar();
          default:
             return null;
      }
     }
    }
//(4)ApplicationContext.xml
/*
 *工厂方法模式创建对象: 工厂中获取具体对象的方法不是静态方法,而是成员方法
 *   1. 需要加载工厂对象
 *   2. 在要使用工厂对象来创建的bean中
 *       通过factory-bean 获取工厂对象
 *       通过factory-method 配置工厂对象要调用的方法
 */
    <bean id="carFactory" class="com.xalo.action.CarFactory"></bean>     
    <bean id="car" factory-bean="carFactory" factory-method="getCar"> 
       <constructor-arg name="type" value="suv"></constructor-arg>        
    </bean>

 //(5)单元测试
public class DITest {
      @Test
   public void test() {
     ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
     CarInterf car = (CarInterf) context.getBean("car");
             car.createCar();
        }
     } 

抽象工厂模式:

/*
 *运行步骤:
 *   1.需求:  轮胎  发动机    创建产品接口(ProductInterf)
 *   2.需求轮胎种类 例如: 轿车轮胎(CarTyre)  卡车轮胎(TruckTyre)
 *     需求发动机种类 例如: 柴油发动机(DiesEngine)  汽油发动机(PetrolEngine)
 *            创建classimplements  ProductInterf
 *   3. 创建造轮胎  造发动机接口(AbsFactory)
 *   4. 指派指定工厂(轮胎工厂  发动机工厂) (AssignFactory)
 *       创建class类 轮胎工厂(TyreFactory)  implements  AbsFactory
 *                       发动机工厂(EngineFactory) implements AbsFactory
 */
   //1. 需求 轮胎 发动机
      public interface ProductInterf {
              //轮胎
            public abstract void tyre(); 
             //发动机
            public abstract void engine();
         }
 //2.需求轮胎种类   发动机种类
     //轿车轮胎
    public class CarTyre implements ProductInterf {
          @Override
          public void tyre() {
              System.out.println("轿车轮胎");
         }
     }

    //卡车轮胎
   public class TruckTyre implements ProductInterf {
        @Override
       public void tyre() {
            System.out.println("卡车轮胎");
      }
  }

    //柴油发动机
  public class DiesEngine implements ProductInterf { 
         @Override
     public void engine() {
           System.out.println("柴油发动机");
      }
   }

     //汽油发动机
   public class PetrolEngine implements ProductInterf{
          @Override
      public void engine() {
            System.out.println("汽油发动机");
    }
 }
   //3. 创建造轮胎  造发动机接口(AbsFactory)
    public interface AbsFactory {
          //造轮胎
        public ProductInterf createTyre(String type);
         //造发动机
        public ProductInterf createEngine(String type);
    }
//4. 指派指定工厂(轮胎工厂  发动机工厂) (AssignFactory)
      //指派指定工厂
  public class AssignFactory {
     public static AbsFactory getFactory(String type) {
          switch (type) {
              case "tyre":
                  return new TyreFactory();
              case "engine":
                   return new EngineFactory();
             default:
                   return null;
          }
      }
   }
    public  class TyreFactory implements AbsFactory {
       public ProductInterf createTyre(String type) {
          switch (type) {
              case "car":
                   return new CarTyre();
              case "truck"  :
                   return new TruckTyre();
              default:
                  return null;
         }      
      }
   }

   public class EngineFactory implements AbsFactory{
         @Override
       public ProductInterf createEngine(String type) {
           switch (type) {
              case "dies":
                   return new DiesEngine();
              case "petrol":
                   return new PetrolEngine();
              default:
                   return null;
       }
     }
  }
//单元测试
  public class TestAbsFactory {
    @Test
    public void test() {
       //需要一个汽车轮胎
      //1.获取制造轮胎的工厂对象
  AbsFactory tyreFactory = AssignFactory.getFactory("tyre");
   ProductInterf tyre = tyreFactory.createTyre("truck");
               tyre.tyre();
    AbsFactory factory = AssignFactory.getFactory("engine");
     ProductInterf engine = factory.createEngine("petrol");
              engine.engine();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值