《读_Head_First_有感》_“工厂方法模式”

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

[TOC]

1.官方话语

概述

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

六大原则:

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

个人话语

概述

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

六大原则

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

今日主题

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

场景:

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

概述:

比较简单,需要什么对象,用什么工厂的实现。见代码吧。(附加 简单工厂代码)

代码如下

'简单工厂全部'代码

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);
    }
}

'抽象工厂全部'代码

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.本系列文章内容会比较简陋,望有兴趣读者还是fork源码,调试一下。(如果你看过该书,一定可以加深印象)
2.联想下实际运用的哪些业务场景用到该模式,哪些中间件用到该模式,是否自己能在业务中使用。
3.即使你现在用不到某些设计模式,但是还是应该理解其原理的。
4.当时理解并不意味着自己已会,可以自己尝试练习,并幻想一种业务场景,画画类图,设计一下。


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

代码请参考码云:https://gitee.com/tjhuey/CodingGroup
设计模式相关概念请参考:http://naotu.baidu.com/file/5811bf42020e6a2d877b992cfca90d26

进度

  • [x] 策略模式已完成
  • [x] 观察者模式已完成;
  • [x] 装饰者模式已完成;
  • [x] 工厂方法模式已完成;
  • [ ] ..未完成;

你可能感兴趣的:(《读_Head_First_有感》_“工厂方法模式”)