23种设计模式之-工厂模式


工厂顾名思义就是创建产品,根据产品是具体产品还是具体工厂可以分为简单工厂和工厂方法模式,根据工厂的抽象程序可以分为工厂方法模式和抽象工厂模式。

1.简单工厂模式

简单的工厂模式,或称静态工厂方法模式,是不同的工厂方法模式的一种特殊实现。所于创建型模式,但它不属于 23 种设计模式
在这里插入图片描述

public interface Course {
    public void record();
}
public class JavaCourse implements Course {

    @Override
    public void record() {
        System.out.println("Java course");
    }
}
public class PythonCourse implements Course {

    @Override
    public void record() {
        System.out.println("Python Course");
    }
}
public class CourseFactory {

    public Course create(Class<? extends Course> clazz)
    {
        if(clazz != null )
        {
            try {
                return clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return null;

    }
}
public class FactoryTest {

    public static void main(String[] args) {
        Course course = new CourseFactory().create(JavaCourse.class);
        course.record();
    }
}

2.工厂方法模式(Factory Method)

定义一个创建对象的接口,但让实现这个接口的类来决定实例化哪个类,工厂方法让类的实例化推迟到子类中进行
在这里插入图片描述

public interface Course {

    public void record();
}
public class JavaCourse implements Course {

    @Override
    public void record() {
        System.out.println("Java course");
    }
}
public class PythonCourse implements Course {

    @Override
    public void record() {
        System.out.println("Python Course");
    }
}
public interface CourseFactory {

    Course create();
}
public class JavaCourseFactory implements CourseFactory {

    @Override
    public Course create() {
        return new JavaCourse();
    }
}
public class PythonCourseFactory implements CourseFactory {

    @Override
    public Course create() {
        return new PythonCourse();
    }
}

public class FactoryTest {

    public static void main(String[] args) {

        CourseFactory courseFactory = new JavaCourseFactory();
        Course course = courseFactory.create();
        course.record();
    }
}

3.抽象工厂模式(Abstract Factory)

是指提供一个创建一系列相关或相互依赖对象的接口,无须指定他们具体的类。
在这里插入图片描述

public interface Node {

    public void edit();
}
public class JavaNode implements Node {

    @Override
    public void edit() {
        System.out.println("java edit");
    }
}
public class PythonNode implements Node {

    @Override
    public void edit() {
        System.out.println("python edit");
    }
}
public interface Video {

    public void record();
}
public class JavaVideo implements Video {

    @Override
    public void record() {
        System.out.println("java record");
    }
}
public class PythonVideo implements Video {

    @Override
    public void record() {
        System.out.println("python record");
    }
}
public abstract class CourseFactory {

    public void init()
    {
        System.out.println("初始化数据。");
    }

    protected abstract Node createNode();

    protected abstract Video createVideo();
}
public class JavaCourseFactory extends CourseFactory {

    @Override
    protected Node createNode() {
        super.init();
        return new JavaNode();
    }

    @Override
    protected Video createVideo() {
        super.init();
        return new JavaVideo();
    }
}
public class PythonCourseFactory extends CourseFactory {

    @Override
    protected Node createNode() {
        super.init();
        return new PythonNode();
    }

    @Override
    protected Video createVideo() {
        super.init();
        return new PythonVideo();
    }
}
public class FactoryTest {

    public static void main(String[] args) {
        CourseFactory courseFactory = new JavaCourseFactory();
        courseFactory.createNode().edit();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值