zoukankan      html  css  js  c++  java
  • 小白学习设计模式之工厂模式

    工厂模式(运用在需要大量实例化对象的项目中):
    定义一个创建对象的类,由这个类来封装实例化对象的行为
    工厂方法模式:
    定义一个创建对象的抽象方法,由子类来决定要实例化的类,工厂方法模式将对象的实例化推迟到了子类
    抽象工厂模式:
    定义了一个接口用于创建相关或有依赖关系的对象族,而无需明确指定具体类

    依赖抽象原则:
    变量不要持有具体类的引用
    不要让类继承自具体类,要继承自抽象类或接口
    不要覆盖基类中已实现的方法

    首先抽象披萨类:
    package com.wz.FiveFactory;

    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;
    }
    }
    不同国家不同口味的披萨:
    伦敦:
    package com.wz.FiveFactory;

    public class LDPepperPizza extends Pizza {

    @Override
    public void prepare() {
    // TODO Auto-generated method stub
    super.setname("LDPepperPizza");

    System.out.println(name+" preparing;");
    }

    }
    package com.wz.FiveFactory;

    public class LDCheesePizza extends Pizza {

    @Override
    public void prepare() {
    // TODO Auto-generated method stub
    super.setname("LDCheesePizza");

    System.out.println(name+" preparing;");
    }

    }

    纽约:
    package com.wz.FiveFactory;

    public class NYCheesePizza extends Pizza {

    @Override
    public void prepare() {
    // TODO Auto-generated method stub
    super.setname("NYCheesePizza");

    System.out.println(name+" preparing;");
    }

    }
    package com.wz.FiveFactory;

    public class NYPepperPizza extends Pizza {

    @Override
    public void prepare() {
    // TODO Auto-generated method stub
    super.setname("NYPepperPizza");

    System.out.println(name+" preparing;");
    }

    }
    将国家设计为接口(工厂):
    package com.wz.FiveFactory;


    public interface AbsFactory {
    public Pizza CreatePizza(String ordertype) ;
    }
    伦敦:
    package com.wz.FiveFactory;

    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.wz.FiveFactory;

    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("pepper")) {
    pizza = new NYPepperPizza();
    }
    return pizza;

    }

    }

    最后的测试:
    package com.wz.FiveFactory;



    public class PizzaStroe {
    public static void main(String[] args) {

    OrderPizza mOrderPizza;
    // 不同的国家在这体现
    mOrderPizza=new OrderPizza(new LDFactory());

    }



    }




  • 相关阅读:
    odoo10 入门
    git 命令详细介绍
    odoo中Python实现小写金额转换为大写金额
    {DARK CTF } OSINT/Eye
    2020 12 18
    2020 12 17
    2020 12 16
    2020 12 15
    2020 11 14
    2020 11 13
  • 原文地址:https://www.cnblogs.com/wadmwz/p/8916143.html
Copyright © 2011-2022 走看看