java设计模式学习之工厂模式(更简单的理解设计模式)

简单工厂模式
简单工厂模式的设计方案:定义一个实例化披萨对象的类,封装创建对象的代码
简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为

package com.java.jikexueyuan.pizzastore.simplefactory;

import com.java.jikexueyuan.pizzastore.pizza.CheesePizza;
import com.java.jikexueyuan.pizzastore.pizza.GreekPizza;
import com.java.jikexueyuan.pizzastore.pizza.PepperPizza;
import com.java.jikexueyuan.pizzastore.pizza.Pizza;

/**
 *  改进代码,使用简单工厂模式,
 *  简单工厂模式,其实就是创建一个工厂对象,将创建实例的代码与业务解耦
 */
public class SimplePizzaFactory {

    public Pizza CreatePizza(String ordertype) {
        Pizza pizza = null;

        if (ordertype.equals("cheese")) {
            pizza = new CheesePizza();
        } else if (ordertype.equals("greek")) {
            pizza = new GreekPizza();
        } else if (ordertype.equals("pepper")) {
            pizza = new PepperPizza();
        }
        return pizza;

    }

}

package com.java.jikexueyuan.pizzastore.simplefactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.java.jikexueyuan.pizzastore.pizza.CheesePizza;
import com.java.jikexueyuan.pizzastore.pizza.GreekPizza;
import com.java.jikexueyuan.pizzastore.pizza.PepperPizza;
import com.java.jikexueyuan.pizzastore.pizza.Pizza;

/**
 *  改进代码,使用简单工厂模式,
 *  简单工厂模式,其实就是创建一个工厂对象,并进行注入,
 *  将创建披萨实例的代码迁移到工厂中,当我们有新的披萨种类时,只需要修改工厂类即可
 *   简单工厂模式的弊端:
 *    只能创建指定的工厂对象,工厂类和业务类耦合,如果要新增工厂,就不是很方便了
 */
public class OrderPizza {
   SimplePizzaFactory mSimplePizzaFactory;

   public OrderPizza(SimplePizzaFactory mSimplePizzaFactory) {

      setFactory(mSimplePizzaFactory);
   }

   public void setFactory(SimplePizzaFactory mSimplePizzaFactory) {
      Pizza pizza = null;
      String ordertype;

      this.mSimplePizzaFactory = mSimplePizzaFactory;

      do {
         ordertype = gettype();
         pizza = mSimplePizzaFactory.CreatePizza(ordertype);
         if (pizza != null) {
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
         }

      } while (true);

   }

   private String gettype() {
      try {
         BufferedReader strin = new BufferedReader(new InputStreamReader(
               System.in));
         System.out.println("input pizza type:");
         String str = strin.readLine();

         return str;
      } catch (IOException e) {
         e.printStackTrace();
         return "";
      }
   }

}

package com.java.jikexueyuan.pizzastore.simplefactory;



public class PizzaStroe {
   public static void main(String[] args) {
      SimplePizzaFactory mSimplePizzaFactory;
      OrderPizza mOrderPizza;
      mOrderPizza=new    OrderPizza(new SimplePizzaFactory());
      
   }

   

}

工厂方法模式设计方案:将披萨项目里的披萨对象实例化功能抽象成抽象方法,在不同加盟店具体实现功能。
工厂方法模式:定义了一个创建对象的抽象方法,由子类决定要实例化的类。
工厂方法模式将对象的实例化推迟到子类

package com.java.jikexueyuan.pizzastore.method;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.java.jikexueyuan.pizzastore.pizza.CheesePizza;
import com.java.jikexueyuan.pizzastore.pizza.ChinesePizza;
import com.java.jikexueyuan.pizzastore.pizza.GreekPizza;
import com.java.jikexueyuan.pizzastore.pizza.PepperPizza;
import com.java.jikexueyuan.pizzastore.pizza.Pizza;

/**
 * 工厂方法模式: 将创建披萨实例的方法进行抽象,具体如果进行创建交给子类进行决定
 * 实现实例类与工厂类的解耦。
 */
public abstract class OrderPizza {

   public OrderPizza() {
      Pizza pizza = null;
      String ordertype;

      do {
         ordertype = gettype();
         pizza = createPizza(ordertype);

         pizza.prepare();
         pizza.bake();
         pizza.cut();
         pizza.box();
      } while (true);
   }

   abstract Pizza createPizza(String ordertype);

   private String gettype() {
      try {
         BufferedReader strin = new BufferedReader(new InputStreamReader(
               System.in));
         System.out.println("input pizza type:");
         String str = strin.readLine();

         return str;
      } catch (IOException e) {
         e.printStackTrace();
         return "";
      }
   }

}

package com.java.jikexueyuan.pizzastore.method;

import com.java.jikexueyuan.pizzastore.pizza.CheesePizza;
import com.java.jikexueyuan.pizzastore.pizza.GreekPizza;
import com.java.jikexueyuan.pizzastore.pizza.NYCheesePizza;
import com.java.jikexueyuan.pizzastore.pizza.NYPepperPizza;
import com.java.jikexueyuan.pizzastore.pizza.PepperPizza;
import com.java.jikexueyuan.pizzastore.pizza.Pizza;

public class NYOrderPizza extends OrderPizza {

   @Override
   Pizza createPizza(String ordertype) {
      Pizza pizza = null;

      if (ordertype.equals("cheese")) {
         pizza = new NYCheesePizza();
      } else if (ordertype.equals("pepper")) {
         pizza = new NYPepperPizza();
      }
      return pizza;

   }

}
package com.java.jikexueyuan.pizzastore.method;



public class PizzaStroe {
   public static void main(String[] args) {
      
      OrderPizza mOrderPizza;
      mOrderPizza=new    NYOrderPizza();
      
   }

   

}

抽象工厂模式:定义了一个接口用于创建相关或有依赖关系的对象族,而无需明确指定具体类

package com.java.jikexueyuan.pizzastore.absfactory;

import com.java.jikexueyuan.pizzastore.pizza.Pizza;


public interface AbsFactory {
   public Pizza CreatePizza(String ordertype) ;
}

package com.java.jikexueyuan.pizzastore.absfactory;

import com.java.jikexueyuan.pizzastore.pizza.LDCheesePizza;
import com.java.jikexueyuan.pizzastore.pizza.LDPepperPizza;
import com.java.jikexueyuan.pizzastore.pizza.Pizza;

public class LDFactory implements AbsFactory {

   @Override
   public Pizza CreatePizza(String ordertype) {
      Pizza pizza = null;

      if (ordertype.equals("cheese")) {
         pizza = new LDCheesePizza();
      } else if (ordertype.equals("pepper")) {
         pizza = new LDPepperPizza();
      }
      return pizza;

   }

}

package com.java.jikexueyuan.pizzastore.absfactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.java.jikexueyuan.pizzastore.pizza.CheesePizza;
import com.java.jikexueyuan.pizzastore.pizza.GreekPizza;
import com.java.jikexueyuan.pizzastore.pizza.PepperPizza;
import com.java.jikexueyuan.pizzastore.pizza.Pizza;
/**
 * 抽象工厂模式:将工厂的创建进行抽象
 * 创建实例时传递创建工厂的接口,具体如何创建交给工厂的实现进行描述,
 * 实现了业务类和工厂类的解耦。
 */
public class OrderPizza {
   AbsFactory mFactory;

   public OrderPizza(AbsFactory mFactory) {

      setFactory(mFactory);
   }

   public void setFactory(AbsFactory mFactory) {
      Pizza pizza = null;
      String ordertype;

      this.mFactory = mFactory;

      do {
         ordertype = gettype();
         pizza = mFactory.CreatePizza(ordertype);
         if (pizza != null) {
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
         }

      } while (true);

   }

   private String gettype() {
      try {
         BufferedReader strin = new BufferedReader(new InputStreamReader(
               System.in));
         System.out.println("input pizza type:");
         String str = strin.readLine();

         return str;
      } catch (IOException e) {
         e.printStackTrace();
         return "";
      }
   }

}

package com.java.jikexueyuan.pizzastore.absfactory;



public class PizzaStroe {
   public static void main(String[] args) {
      
      OrderPizza mOrderPizza;
      mOrderPizza=new    OrderPizza(new LDFactory());
      
   }

   

}

工厂模式的关键点:
1.工厂模式的意义
2.三种工厂模式
3.依赖抽象原则
变量不要持有具体类的引用
不要让类继承自具体类,要继承自抽象类或接口
不要覆盖基类中已实现的方法

你可能感兴趣的:(设计模式,技术使用总结,知识总结)