工厂方法 Factory Method

本文介绍了工厂方法模式的概念及其应用,通过实例展示了如何使用该模式来解决简单工厂模式中难以扩展的问题,使得添加新类型变得更加灵活。

1.工厂方法

    定义一个用于创建对象的接口,让子类决定实例化哪一个类。
    工厂方法使一个类的实例化延迟到其子类。
    工厂方法把简单工厂的内部逻辑判断移到了客户端代码来进行。
    你想要加功能,本来是改工厂类的,现在是修改客户端!

2.结构图

这里写图片描述

3.代码

package com.hlf.designPatterns.FactoryMethod;

/**
 * 学雷锋,做好事
 * Created by Administrator on 2016/8/24.
 */
public interface LeiFeng {
    //帮助老人过马路
    void helpWalk();

    //捐钱
    void giveMoney();

    //帮人洗衣服
    void helpWash();
}

package com.hlf.designPatterns.FactoryMethod;

/**
 * 学习雷锋的志愿者们
 * Created by hlf on 2016/8/24.
 */
public class Volunteers implements LeiFeng{
    @Override
    public void helpWalk() {
        System.out.println("Volunteers help people walk");
    }

    @Override
    public void giveMoney() {
        System.out.println("Volunteers help people give money");
    }

    @Override
    public void helpWash() {
        System.out.println("Volunteers help people wash");
    }
}

package com.hlf.designPatterns.FactoryMethod;

/**
 * 学习雷锋的大学生
 * Created by hlf on 2016/8/24.
 */
public class CollegeStudent implements LeiFeng{
    @Override
    public void helpWalk() {
        System.out.println("CollegeStudent help people walk");
    }

    @Override
    public void giveMoney() {
        System.out.println("CollegeStudent help people give money");
    }

    @Override
    public void helpWash() {
        System.out.println("CollegeStudent help people wash");
    }
}

package com.hlf.designPatterns.FactoryMethod;

/**
 * 如果是静态工厂的话是这样做的
 * Created by hlf on 2016/8/24.
 */
public class StaticFactory {
    //志愿者
    public static LeiFeng getVolunteers(){
        return new Volunteers();
    }
    //大学生
    public static LeiFeng getCollegeStudent(){
        return new CollegeStudent();
    }
}

这时如果我要加入一个学习雷锋的小学生的话有麻烦, 这时用到工厂方法的话。


package com.hlf.designPatterns.FactoryMethod;

/**
 * 后来加入的学习雷锋的小学生
 * Created by hlf on 2016/8/24.
 */
public class Pupil implements LeiFeng{
    @Override
    public void helpWalk() {
        System.out.println("Pupil help people walk");
    }

    @Override
    public void giveMoney() {
        System.out.println("Pupil help people give money");
    }

    @Override
    public void helpWash() {
        System.out.println("Pupil help people wash");
    }
}

package com.hlf.designPatterns.FactoryMethod;

/**
 * 工厂方法,就是要把简单工厂类抽象出来
 *
 * Created by hlf on 2016/8/24.
 */
public interface LeiFengFactory {
    LeiFeng getHelper();
}

package com.hlf.designPatterns.FactoryMethod;

/**
 * 后来加入的只要再创建一个对应的工厂就行了
 * Created by hlf on 2016/8/24.
 */
public class PupilFactory implements LeiFengFactory{
    @Override
    public LeiFeng getHelper() {
        return new Pupil();
    }
}

package com.hlf.designPatterns.FactoryMethod;

/**
 * 大学生工厂
 * Created by hlf on 2016/8/24.
 */
public class CollegeStudentFactory implements LeiFengFactory{
    @Override
    public LeiFeng getHelper() {
        return new CollegeStudent();
    }
}

package com.hlf.designPatterns.FactoryMethod;

/**
 * 志愿者工厂
 * Created by hlf on 2016/8/24.
 */
public class VolunteersFactory implements LeiFengFactory{
    @Override
    public LeiFeng getHelper() {
        return new Volunteers();
    }
}

package com.hlf.designPatterns.FactoryMethod;

/**
 * Created by hlf on 2016/8/24.
 */
public class TestFactoryMethod {
    public static void main(String[] args) {
        //如果用静态工厂方法创建
        LeiFeng collegeStudent = StaticFactory.getCollegeStudent();
        collegeStudent.giveMoney();
        collegeStudent.helpWalk();

        //如果想再加一个学习雷锋的对象小学生pupil的话,就要修改StaticFactory
        //这时用工厂方法就好点
        PupilFactory pupilFactory = new PupilFactory();
        LeiFeng pulil = pupilFactory.getHelper();
        pulil.giveMoney();
        pulil.helpWalk();
        pulil.helpWash();
    }
}

结果

CollegeStudent help people give money
CollegeStudent help people walk

Pupil help people give money
Pupil help people walk
Pupil help people wash
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值