设计模式--工厂模式

这篇博客讨论了披萨项目在设计模式上的演变,从最初的简单工厂模式解决品种扩展问题,到工厂方法模式应对加盟店的定制需求,最后升级为抽象工厂模式以支持不同地区的口味差异。通过这些模式的应用,实现了代码的可扩展性和维护性,遵循了开闭原则。
摘要由CSDN通过智能技术生成

披萨项目:要方便披萨品种的扩展、要便于维护、要能运行时扩展

披萨族设计:

 1、正常逻辑

抽象披萨类(Pizza )、两个具体披萨类(CheesePizza、GreekPizza)、披萨订单类(OrderPizza )

如果我们有新的披萨类,就要对OrderPizza类,增加else if,违背了开闭原则。
public abstract class Pizza {
    protected String name;
    /**
     * 披萨准备
     */
    public abstract void prepare();

    /**
     * 披萨烘焙
     */
    public void bake() {
        System.out.println(name + " baking;");
    }

    /**
     * 披萨切割
     */
    public void cut() {
        System.out.println(name + " cutting;");
    }

    /**
     * 披萨打包
     */
    public void box() {
        System.out.println(name + " boxing;");
    }

    public void setname(String name) {
        this.name = name;
    }
}
public class CheesePizza extends Pizza {
   @Override
   public void prepare() {
      super.setname("CheesePizza");
      System.out.println(name + " preparing;");
   }
}
public class GreekPizza extends Pizza {
   @Override
   public void prepare() {
      super.setname("GreekPizza");
      System.out.println(name + " preparing;");
   }
}

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

public class OrderPizza {
   public OrderPizza() {
      Pizza pizza = null;
      String ordertype;
      do {
         ordertype = gettype();
         if (ordertype.equals("cheese")) {
            pizza = new CheesePizza();
         } else if (ordertype.equals("greek")) {
            pizza = new GreekPizza();
         } else  {
            break;
         }
         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 "";
      }
   }

   public static void main(String [] args) {
      OrderPizza orderPizza = new OrderPizza();

   }
}

2、使用简单工厂模式

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

简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为

增加一个简单工厂类(SimplePizzaFactory )和修改披萨订单类(OrderPizzaBySimple )

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();
      }
      return pizza;
   }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class OrderPizzaBySimple {
    private SimplePizzaFactory simplePizzaFactory;

    public OrderPizzaBySimple(SimplePizzaFactory simplePizzaFactory) {
        this.simplePizzaFactory = simplePizzaFactory;
        Pizza pizza = null;
        String ordertype;
        do {
            ordertype = gettype();
            pizza = this.simplePizzaFactory.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 "";
        }
    }

    public static void main(String [] args) {
        SimplePizzaFactory simplePizzaFactory = new SimplePizzaFactory();
        OrderPizzaBySimple orderPizza = new OrderPizzaBySimple(simplePizzaFactory);

    }
}

3、使用工厂模式

披萨项目新需求的困惑:披萨项目加盟店

如果用简单工厂模式来处理,每个加盟店都要创建一个简单工厂。

工厂方法模式设计方案:将披萨项目里的披萨对象实例化功能抽象成抽象方法,在 不同加盟店具体实现功能。

工厂方法模式:定义了一个创建对象的抽象方法,由子类决定要实例化的类。 工厂方法模式将对象的实例化推迟到子类

披萨抽象类(OrderPizzaByFactory)、两个具体的子类(伦敦加盟商LDOrderPizza、纽约加盟商NYOrderPizza)

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

public abstract class OrderPizzaByFactory {
    abstract Pizza createPizza(String ordertype);

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

        do {
            ordertype = gettype();
            pizza = createPizza(ordertype);
            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 "";
        }
    }
}
public class LDOrderPizza extends OrderPizzaByFactory {
   @Override
   Pizza createPizza(String ordertype) {
      Pizza pizza = null;

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

   }

   public static class LDCheesePizza extends Pizza{
      @Override
      public void prepare() {
         super.setname("CheesePizza");
         System.out.println(name + " preparing;");
      }
   }


   public static class LDGreekPizza extends Pizza{
      @Override
      public void prepare() {
         super.setname("LDGreekPizza");
         System.out.println(name + " preparing;");
      }
   }

   public static void main(String [] args) {
      OrderPizzaByFactory orderPizza = new LDOrderPizza();
   }

}
public class NYOrderPizza extends OrderPizzaByFactory {
   @Override
   Pizza createPizza(String ordertype) {
      Pizza pizza = null;

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

   }

   public static class NYCheesePizza extends Pizza{
      @Override
      public void prepare() {
         super.setname("NYCheesePizza");
         System.out.println(name + " preparing;");
      }
   }


   public static class NYGreekPizza extends Pizza{
      @Override
      public void prepare() {
         super.setname("NYCheesePizza");
         System.out.println(name + " preparing;");
      }
   }

   public static void main(String [] args) {
      OrderPizzaByFactory orderPizza = new NYOrderPizza();
   }

}

4、升级抽象工程模式

增加抽象工厂类(AbsFactory)、两个具体的子类工厂(伦敦工厂NYFactory、纽约工厂LDFactory)、披萨订单类(OrderPizzaByAbs)

public interface AbsFactory {
   public Pizza CreatePizza(String ordertype) ;
}
public class NYFactory implements AbsFactory {
   @Override
   public Pizza CreatePizza(String ordertype) {
      Pizza pizza = null;

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

   }

   public static class NYCheesePizza extends Pizza{
      @Override
      public void prepare() {
         super.setname("NYCheesePizza");
         System.out.println(name + " preparing;");
      }
   }

   public static class NYGreekPizza extends Pizza{
      @Override
      public void prepare() {
         super.setname("NYCheesePizza");
         System.out.println(name + " preparing;");
      }
   }
}
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("greek")) {
         pizza = new LDGreekPizza();
      }
      return pizza;
   }

   public static class LDCheesePizza extends Pizza{
      @Override
      public void prepare() {
         super.setname("CheesePizza");
         System.out.println(name + " preparing;");
      }
   }

   public static class LDGreekPizza extends Pizza{
      @Override
      public void prepare() {
         super.setname("LDGreekPizza");
         System.out.println(name + " preparing;");
      }
   }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class OrderPizzaByAbs {
    private AbsFactory mFactory;

    public OrderPizzaByAbs(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 "";
        }
    }

    public static void main(String [] args) {
        AbsFactory absFactory = new LDFactory();
        OrderPizzaByAbs orderPizzaByAbs = new OrderPizzaByAbs(absFactory);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青春1314

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值