《Head First 设计模式》 - 工厂方法模式

2020-04-10 16:25:40 浏览数 (3)

前言: 前沿技术一直在迭代,有一种东西是不会更新的,那就是设计模式的思想。 可以来学习学习设计模式的思维,巧妙设计! 工厂模式最简单,但对于OCP最为提现!

一、官方话语

概述

设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结。

六大原则

  1. 单一职责原则 (Single ResponsiBility Principle) 概括:应该有且仅有一个原因引起类的变更
  2. 里氏替换原则(liskov Substitution Principle ) 概括:基类出现的地方,子类一定可以出现
  3. 依赖倒转原则(Depndece Inversion Principle) 概括:针对接口编程,依赖于抽象而不是具体
  4. 接口隔离原则(Interface Segregation Principle) 概括:使用多个隔离的接口,比使用单个接口好 (细分接口,降低耦合)
  5. 迪米特法则 (Demeter Principle) 概括:实体应当尽量少的与其他类发生互相作用,使得系统功能模块相对独立
  6. 开闭原则(Open Close Principle) 概括: 对扩展开放,对修改关闭

二、个人解读

概述

设计模式在代码层级中,是让你在某种业务场景刚开始设计时,能让未来的相关需求扩展极为方便的一个思想。 简单的说,在一开始设计好,扩展是很方便的,设计模式就是这个功劳者。 对于我们本来就懒的开发人员来说,这是求之不得的。

六大原则

而对于六大原则,简单过一下就行,不用刻意理解,如果你会了面向对象和设计模式的使用,自然就遵循了。

三、今日主题

工厂方法模式: 概述:定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的初始化延迟至其子类

场景

火锅店,任何一个订单,都会生成一个订单,订单包含着什么样的火锅对象。创建工程来做案例

概述

比较简单,需要什么对象,用什么工厂的实现,参照代码。(附:简单工厂代码)

相关代码

简单工厂模式

代码语言:javascript复制
package top.huey.designpattern.factorypattern.simplefactory.beans;

/**
 * @author huey
 * @Description : 火锅抽象对象组件
 * @Date Created in 2018/7/29 15:18
 */
public abstract class HotPot {

    /**
     * 肉
     */
    protected String meat;

    /**
     * 菜
     */
    protected String greens;

    /**
     * 底料
     */
    protected String material;

    /**
     * @author huey
     * @Description : 每类火锅需要哪些肉,菜,底料等。
     * @Date Created in 2018/7/29 15:20
     */
    public abstract String prepare();

    /**
     * @author huey
     * @Description : 区分不同对象
     * @Date Created in 2018/7/29 15:29
     */
    public void display() {
        System.out.println(this.meat   this.greens   this.material);
    }
}

package top.huey.designpattern.factorypattern.simplefactory.beans;

/**
 * @author huey
 * @Description : 1类 火锅
 * @Date Created in 2018/7/29 15:18
 */
public class OneHotPot extends HotPot {

    public OneHotPot() {
        this.meat = "oneMeat";
        this.greens = "oneGreens";
        this.material = "oneMaterial";
    }


    /**
     * @author huey
     * @Description : 每类火锅需要哪些肉,菜,底料等。
     * @Date Created in 2018/7/29 15:20
     */
    @Override
    public String prepare() {
        return this.getClass()   "prepare----------";
    }
}

package top.huey.designpattern.factorypattern.simplefactory.beans;

/**
 * @author huey
 * @Description : 2类 火锅
 * @Date Created in 2018/7/29 15:18
 */
public class TwoHotPot extends HotPot {

    public TwoHotPot() {
        this.meat = "twoMeat";
        this.greens = "twoGreens";
        this.material = "twoMaterial";
    }

    /**
     * @author huey
     * @Description : 每类火锅需要哪些肉,菜,底料等。
     * @Date Created in 2018/7/29 15:20
     */
    @Override
    public String prepare() {
        return this.getClass()   "prepare----------";
    }
}

package top.huey.designpattern.factorypattern.simplefactory.factory;

import top.huey.designpattern.factorypattern.simplefactory.beans.HotPot;
import top.huey.designpattern.factorypattern.simplefactory.beans.OneHotPot;
import top.huey.designpattern.factorypattern.simplefactory.beans.TwoHotPot;

/**
 * @author huey
 * @Description : 建造对象
 * @Date Created in 2018/7/29 15:33
 */
public class HotPotFactory {

    /**
     * 对象类型
     */
    public static final int ONE_TYPE = 1;
    public static final int TW0_TYPE = 2;

    /**
     * @author huey
     * @Description : 对象逻辑设定
     * @Date Created in 2018/7/29 15:35
     */
    public HotPot createHotPot(int type) {
        if (type == ONE_TYPE) {
            return new OneHotPot();
        } else if (type == TW0_TYPE) {
            return new TwoHotPot();
        } else {
            return null;
        }
    }
}

package top.huey.designpattern.factorypattern.simplefactory.creater;

import top.huey.designpattern.factorypattern.simplefactory.beans.HotPot;
import top.huey.designpattern.factorypattern.simplefactory.factory.HotPotFactory;

/**
 * @author huey
 * @Description : 商店
 * @Date Created in 2018/7/29 15:40
 */
public class HotPotStore {
    /**
     * 组合工厂对象
     */
    private HotPotFactory hotPotFactory;

    /**
     * @author huey
     * @Description : 初始化工厂对象
     * @Date Created in 2018/7/29 15:45
     */
    public HotPotStore(HotPotFactory hotPotFactory) {
        this.hotPotFactory = hotPotFactory;
    }

    /**
     * @author huey
     * @Description : 暴露订单
     * @Date Created in 2018/7/29 15:45
     */
    public void orderHotPot(int type) {
        HotPot hotPot = this.hotPotFactory.createHotPot(type);
        hotPot.prepare();
        hotPot.display();
    }


}

package top.huey.designpattern.factorypattern.simplefactory;

import org.junit.Test;
import top.huey.designpattern.factorypattern.simplefactory.creater.HotPotStore;
import top.huey.designpattern.factorypattern.simplefactory.factory.HotPotFactory;

/**
 * @author huey
 * @Description : 简单工厂测试
 * @Date Created in 2018/7/29 15:24
 */
public class SimpleFactoryTest {

    @Test
    public void test1() {
        HotPotStore hotPotStore = new HotPotStore(new HotPotFactory());
        hotPotStore.orderHotPot(HotPotFactory.ONE_TYPE);
        hotPotStore.orderHotPot(HotPotFactory.TW0_TYPE);
    }
}

抽象工厂模式

代码语言:javascript复制
package top.huey.designpattern.factorypattern.factorymethod.beans;

/**
 * @author huey
 * @Description : 火锅抽象对象组件
 * @Date Created in 2018/7/29 15:18
 */
public abstract class HotPot {

    /**
     * 肉
     */
    protected String meat;

    /**
     * 菜
     */
    protected String greens;

    /**
     * 底料
     */
    protected String material;

    /**
     * @author huey
     * @Description : 每类火锅需要哪些肉,菜,底料等。
     * @Date Created in 2018/7/29 15:20
     */
    public abstract String prepare();

    /**
     * @author huey
     * @Description : 区分不同对象
     * @Date Created in 2018/7/29 15:29
     */
    public void display() {
        System.out.println(this.meat   this.greens   this.material);
    }
}

package top.huey.designpattern.factorypattern.factorymethod.beans;

/**
 * @author huey
 * @Description : 1类 火锅
 * @Date Created in 2018/7/29 15:18
 */
public class OneHotPot extends HotPot {

    public OneHotPot() {
        this.meat = "oneMeat";
        this.greens = "oneGreens";
        this.material = "oneMaterial";
    }


    /**
     * @author huey
     * @Description : 每类火锅需要哪些肉,菜,底料等。
     * @Date Created in 2018/7/29 15:20
     */
    @Override
    public String prepare() {
        return this.getClass()   "prepare----------";
    }
}

package top.huey.designpattern.factorypattern.factorymethod.beans;

/**
 * @author huey
 * @Description : 2类 火锅
 * @Date Created in 2018/7/29 15:18
 */
public class TwoHotPot extends HotPot {

    public TwoHotPot() {
        this.meat = "twoMeat";
        this.greens = "twoGreens";
        this.material = "twoMaterial";
    }

    /**
     * @author huey
     * @Description : 每类火锅需要哪些肉,菜,底料等。
     * @Date Created in 2018/7/29 15:20
     */
    @Override
    public String prepare() {
        return this.getClass()   "prepare----------";
    }
}

package top.huey.designpattern.factorypattern.factorymethod.creater;

import top.huey.designpattern.factorypattern.simplefactory.beans.HotPot;

/**
 * @author huey
 * @Description : 商店
 * @Date Created in 2018/7/29 15:40
 */
public abstract class HotPotStore {

    /**
     * 暴露
     */
    public void orderHotPot() {
        /**
         *调用具体创建者
         */
        HotPot hotPot = this.createHotPot();
        hotPot.prepare();
        hotPot.display();
    }

    /**
     * @author huey
     * @Description : 抽象创建火锅对象
     * @Date Created in 2018/7/29 16:02
     */
    public abstract HotPot createHotPot();

}

package top.huey.designpattern.factorypattern.factorymethod.creater;

import top.huey.designpattern.factorypattern.simplefactory.beans.HotPot;
import top.huey.designpattern.factorypattern.simplefactory.beans.OneHotPot;

/**
 * @author huey
 * @Description : 具体创建者
 * @Date Created in 2018/7/29 16:04
 */
public class OneHotPotStore extends HotPotStore {
    /**
     * @author huey
     * @Description : 抽象创建火锅对象
     * @Date Created in 2018/7/29 16:02
     */
    @Override
    public HotPot createHotPot() {
        return new OneHotPot();
    }
}

package top.huey.designpattern.factorypattern.factorymethod.creater;

import top.huey.designpattern.factorypattern.simplefactory.beans.HotPot;
import top.huey.designpattern.factorypattern.simplefactory.beans.TwoHotPot;

/**
 * @author huey
 * @Description : 具体创建者
 * @Date Created in 2018/7/29 16:04
 */
public class TwoHotPotStore extends HotPotStore {
    /**
     * @author huey
     * @Description : 抽象创建火锅对象
     * @Date Created in 2018/7/29 16:02
     */
    @Override
    public HotPot createHotPot() {
        return new TwoHotPot();
    }
}

package top.huey.designpattern.factorypattern.factorymethod;

import org.junit.Test;
import top.huey.designpattern.factorypattern.factorymethod.creater.HotPotStore;
import top.huey.designpattern.factorypattern.factorymethod.creater.OneHotPotStore;
import top.huey.designpattern.factorypattern.factorymethod.creater.TwoHotPotStore;

/**
 * @author huey
 * @Description : 简单工厂测试
 * @Date Created in 2018/7/29 15:24
 */
public class FactoryMethodTest {

    @Test
    public void test1() {
        HotPotStore hotPotStore = new OneHotPotStore();
        hotPotStore.orderHotPot();
        HotPotStore hotPotStore2 = new TwoHotPotStore();
        hotPotStore2.orderHotPot();
    }
}

四、读者须知

1.有的设计模式我们在业务开发并不是很常用,但是框架内部有很多的,有兴趣可以看看一些框架的源码比如经典的spring,流行的springboot,Tomcat等。 2.设计模式理解的是思想,根据具体业务场景,合理使用。 3.即使你现在用不到某些设计模式,但是还是应该理解其原理的。 4.当时理解并不意味着自己已会,可以自己尝试练习,并幻想一种业务场景,画画类图,设计一下。

coding 时,做到了如何落实; writing时,做到了如何表达; sharing时,做到了如何传授; thinking时,做到了如何提升;

0 人点赞