由一个工厂对象决定创建出哪一种产品类的实例,不属于23种设计模式。
public interface ICourse {
/**
* 录制视频
*/
void record();
}
public class JavaCourse implements ICourse {
@Override
public void record() {
System.out.println("录制Java视频");
}
}
public class PythonCourse implements ICourse {
@Override
public void record() {
System.out.println("录制Python课程");
}
}
public class CourseFactory {
public ICourse create(String name) {
if("java".equals(name)) {
return new JavaCourse();
} else if("python".equals(name)) {
return new PythonCourse();
} else {
return null;
}
}
}
public class SimpleFactoryTest {
public static void main(String[] args) {
CourseFactory courseFactory = new CourseFactory();
courseFactory.create("java");
courseFactory.create("python");
}
}
如果这时候来了个新需求,比如增加大数据课程,那么工厂的创建课程方法需要修改,和违背了开闭原则,因此需要进一步优化。
public class CourseFactory {
public ICourse create(String className) {
try {
if (!(className == null || "".equals(className))) {
return (ICourse) Class.forName(className).newInstance();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
public class SimpleFactoryTest {
public static void main(String[] args) {
CourseFactory courseFactory = new CourseFactory();
courseFactory.create("com.gupao.factory.simplefactory.JavaCourse");
}
}
上面代码在可扩展性上有所提升,但是入参是一个字符串,可控性有待提高。
public class CourseFactory {
public ICourse create(Class extends ICourse> clazz) {
try {
if(null != clazz) {
return clazz.newInstance();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
public class SimpleFactoryTest {
public static void main(String[] args) {
CourseFactory courseFactory = new CourseFactory();
courseFactory.create(JavaCourse.class);
}
}
简单工厂的缺点:只有一个工厂类,职责过重,不易于扩展复杂的产品结构。
创建一个创建产品的工厂接口,工厂接口的实现类决定创建什么样的产品。
public interface ICourse {
/**
* 录制视频
*/
void record();
}
public class PythonCourse implements ICourse {
@Override
public void record() {
System.out.println("录制Python课程");
}
}
public class JavaCourse implements ICourse {
@Override
public void record() {
System.out.println("录制Java视频");
}
}
public interface ICourseFactory {
ICourse create();
}
public class PythonFactory implements ICourseFactory {
@Override
public ICourse create() {
return new PythonCourse();
}
}
public class JavaFactory implements ICourseFactory {
@Override
public ICourse create() {
return new JavaCourse();
}
}
public class FactoryMethodTest {
public static void main(String[] args) {
ICourseFactory factory = new JavaFactory();
ICourse course = factory.create();
course.record();
factory = new PythonFactory();
course = factory.create();
course.record();
}
}
工厂方法模式是工厂实现类只创建一个产品,职责是单一的,而抽象工模式是工厂的实现类可以创建多个产品。
/**
* 笔记类
*/
public interface INote {
void edit();
}
/**
* 视频类
*/
public interface IVideo {
void record();
}
/**
* Java笔记实现类
*/
public class JavaNote implements INote {
@Override
public void edit() {
System.out.println("编写Java笔记");
}
}
/**
* Java视频实现类
*/
public class JavaVideo implements IVideo {
@Override
public void record() {
System.out.println("录制Java视频");
}
}
/**
* Python笔记实现类
*/
public class PythonNote implements INote {
@Override
public void edit() {
System.out.println("编写Python笔记");
}
}
/**
* Python视频实现类
*/
public class PythonVideo implements IVideo {
@Override
public void record() {
System.out.println("录制Python视频");
}
}
public interface ICourseFactory {
INote createNode();
IVideo createVedio();
}
/**
* Java工厂实现类,方法会创建具体的产品实现类
*/
public class JavaFactory implements ICourseFactory {
@Override
public INote createNode() {
return new JavaNote();
}
@Override
public IVideo createVedio() {
return new JavaVideo();
}
}
/**
* Python工厂实现类,方法会创建具体的产品实现类
*/
public class PythonFactory implements ICourseFactory {
@Override
public INote createNode() {
return new PythonNote();
}
@Override
public IVideo createVedio() {
return new PythonVideo();
}
}