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