• 2.设计模式工厂模式(创建型) Java


    资料:https://gitee.com/chuanqi1995/java

    如果没有工厂模式我们写的代码

    场景

    现在有一个课程,里面有Python、Java、Go语言等课程,并且每个课程都有以下业务

    1. 观看
    2. 录播
    package start.service;
    
    public interface CourseService {
        // 观看
        void view();
        // 录屏
        void record();
    }
    
    package start.service.impl;
    
    import start.service.CourseService;
    
    public class JavaCourseServiceImpl implements CourseService
    {
        @Override
        public void view() {
            System.out.println("观看Java");
        }
    
        @Override
        public void record() {
            System.out.println("录屏Java");
        }
    }
    
    package start.service.impl;
    
    import start.service.CourseService;
    
    public class PythonCourseServiceImpl implements CourseService {
        @Override
        public void view() {
            System.out.println("观看Python");
        }
    
        @Override
        public void record() {
            System.out.println("录屏Python");
        }
    }
    
    package start;
    
    import start.service.CourseService;
    import start.service.impl.JavaCourseServiceImpl;
    import start.service.impl.PythonCourseServiceImpl;
    
    public class Test {
        public static void main(String[] args) {
            // Java、Python、Go
            // 他们都有这些业务:可以观看、可以录屏
    
            CourseService courseService = new JavaCourseServiceImpl();
            courseService.view();
            courseService.record();
    
            CourseService courseService2 = new PythonCourseServiceImpl();
            courseService2.view();
            courseService2.record();
        }
    }
    

    如果后面课程业务加了很多个课程,比如Go、C#,他们都属于课程,但是每次都要new一次对象,我们可不可以用什么方式给管理起来呢?这个时候工厂模式出现了!

    简单工厂

    package simple.service;
    
    public interface CourseService {
        // 观看
        void view();
        // 录屏
        void record();
    }
    
    package simple.service.impl;
    
    import simple.service.CourseService;
    
    public class PythonCourseServiceImpl implements CourseService {
        @Override
        public void view() {
            System.out.println("观看Python");
        }
    
        @Override
        public void record() {
            System.out.println("录屏Python");
        }
    }
    
    package simple.service.impl;
    
    import simple.service.CourseService;
    
    public class JavaCourseServiceImpl implements CourseService
    {
        @Override
        public void view() {
            System.out.println("观看Java");
        }
    
        @Override
        public void record() {
            System.out.println("录屏Java");
        }
    }
    
    package simple.factory;
    
    import simple.service.CourseService;
    import simple.service.impl.GoCourseServiceImpl;
    import simple.service.impl.JavaCourseServiceImpl;
    import simple.service.impl.PythonCourseServiceImpl;
    
    public class CourseServiceFactory {
        public static CourseService get(String name){
            if("Java".equals(name)){
                return new JavaCourseServiceImpl();
            }else if("Python".equals(name)){
                return new PythonCourseServiceImpl();
            }else if("Go".equals(name)){
                return new GoCourseServiceImpl();
            }
            return null;
        }
    }
    
    package simple.factory;
    
    import simple.service.CourseService;
    
    public class Test {
        public static void main(String[] args) {
            CourseService courseService = CourseServiceFactory.get("Go");
            courseService.record();
            courseService.view();
        }
    }
    

    虽然用简单工厂管理起来了,但是也有缺点,缺点就是每次加课程,都需要在CourseServiceFactory里添加大量的逻辑代码,所以工厂方法模式出现了!

    工厂方法

    package function.service;
    
    public interface CourseService {
        // 观看
        void view();
        // 录屏
        void record();
        // 编写笔记
        void edit();
        // 下载笔记
        void download();
    }
    
    package function.service.impl;
    
    import function.service.CourseService;
    
    public class PythonCourseServiceImpl implements CourseService {
        @Override
        public void view() {
            System.out.println("观看Python");
        }
    
        @Override
        public void record() {
            System.out.println("录屏Python");
        }
    
        @Override
        public void edit() {
    
        }
    
        @Override
        public void download() {
    
        }
    }
    
    package function.service.impl;
    
    import function.service.CourseService;
    
    public class JavaCourseServiceImpl implements CourseService
    {
        @Override
        public void view() {
            System.out.println("观看Java");
        }
    
        @Override
        public void record() {
            System.out.println("录屏Java");
        }
    
        @Override
        public void edit() {
    
        }
    
        @Override
        public void download() {
    
        }
    }
    
    package function.service.impl;
    
    import function.service.CourseService;
    
    public class GoCourseServiceImpl implements CourseService
    {
        @Override
        public void view() {
            System.out.println("观看Go");
        }
    
        @Override
        public void record() {
            System.out.println("录屏Go");
        }
    
        @Override
        public void edit() {
    
        }
    
        @Override
        public void download() {
    
        }
    }
    
    package function.factory;
    
    import function.service.CourseService;
    
    public interface CourseServiceFactory {
        CourseService create();
    }
    
    package function.factory;
    
    import function.service.CourseService;
    import function.service.impl.GoCourseServiceImpl;
    
    public class GoCourseServiceFactory implements CourseServiceFactory{
        @Override
        public CourseService create() {
            return new GoCourseServiceImpl();
        }
    }
    
    package function.factory;
    
    import function.service.CourseService;
    import function.service.impl.JavaCourseServiceImpl;
    
    public class JavaCourseServiceFactory implements CourseServiceFactory{
        @Override
        public CourseService create() {
            return new JavaCourseServiceImpl();
        }
    }
    
    package function.factory;
    
    import function.service.CourseService;
    import function.service.impl.PythonCourseServiceImpl;
    
    public class PythonCourseServiceFactory implements CourseServiceFactory{
        @Override
        public CourseService create() {
            return new PythonCourseServiceImpl();
        }
    }
    
    package function.factory;
    
    import function.service.CourseService;
    
    public class Test {
        public static void main(String[] args) {
            CourseServiceFactory courseServiceFactory = new JavaCourseServiceFactory();
            CourseService courseService = courseServiceFactory.create();
            courseService.record();
            courseService.view();
    
            // Java、Python、Go
            // 他们都有这些业务:可以观看、可以录屏、编写笔记、下载笔记
        }
    }
    

    虽然每个课程都用不同的工厂去进行创建,但是他也有缺点,就是每个课程有可能有不同的业务,比如新增了编写笔记、下载笔记等等,我们都需要去进行每个课程的方法实现。并且呢,如果课程多,我们也需要创建大量的Java类,创建好多个课程共产类,越来越负责,所以这个时候抽象工厂出现了!

    抽象工厂

    我们给观看、录屏分组,分为视频组。编写笔记和下载笔记分为笔记组

    package abstracts.service;
    
    public interface VideoCourseService {
        // 观看
        void view();
        // 录屏
        void record();
    }
    
    package abstracts.service;
    
    public interface NoteCourseService {
        // 编写笔记
        void edit();
        // 下载笔记
        void download();
    }
    
    package abstracts.service.Impl;
    
    import abstracts.service.VideoCourseService;
    
    public class PythonVideoCourseServiceImpl implements VideoCourseService
    {
        @Override
        public void view() {
            System.out.println("观看Python");
        }
    
        @Override
        public void record() {
            System.out.println("录制Python");
        }
    }
    
    package abstracts.service.Impl;
    
    import abstracts.service.NoteCourseService;
    
    public class PythonNoteCourseServiceImpl implements NoteCourseService
    {
        @Override
        public void edit() {
            System.out.println("编写Python笔记");
        }
    
        @Override
        public void download() {
            System.out.println("下载Python笔记");
        }
    }
    
    package abstracts.service.Impl;
    
    import abstracts.service.VideoCourseService;
    
    public class JavaVideoCourseServiceImpl implements VideoCourseService
    {
        @Override
        public void view() {
            System.out.println("观看Java");
        }
    
        @Override
        public void record() {
            System.out.println("录制Java");
        }
    }
    
    package abstracts.service.Impl;
    
    import abstracts.service.NoteCourseService;
    
    public class JavaNoteCourseServiceImpl implements NoteCourseService
    {
        @Override
        public void edit() {
            System.out.println("编写Java笔记");
        }
    
        @Override
        public void download() {
            System.out.println("下载Java笔记");
        }
    }
    
    package abstracts.factory;
    
    import abstracts.service.NoteCourseService;
    import abstracts.service.VideoCourseService;
    
    public abstract class CourseFactory {
        protected abstract NoteCourseService createNoteService();
        protected abstract VideoCourseService createVideoService();
    }
    
    package abstracts.factory;
    
    import abstracts.service.Impl.JavaNoteCourseServiceImpl;
    import abstracts.service.Impl.JavaVideoCourseServiceImpl;
    import abstracts.service.NoteCourseService;
    import abstracts.service.VideoCourseService;
    
    public class JavaCourseFactory extends CourseFactory{
        @Override
        protected NoteCourseService createNoteService() {
            return new JavaNoteCourseServiceImpl();
        }
    
        @Override
        protected VideoCourseService createVideoService() {
            return new JavaVideoCourseServiceImpl();
        }
    }
    
    package abstracts.factory;
    
    import abstracts.service.Impl.PythonNoteCourseServiceImpl;
    import abstracts.service.Impl.PythonVideoCourseServiceImpl;
    import abstracts.service.NoteCourseService;
    import abstracts.service.VideoCourseService;
    
    public class PythonCourseFactory extends CourseFactory{
        @Override
        protected NoteCourseService createNoteService() {
            return new PythonNoteCourseServiceImpl();
        }
    
        @Override
        protected VideoCourseService createVideoService() {
            return new PythonVideoCourseServiceImpl();
        }
    }
    
    package abstracts.factory;
    
    import abstracts.factory.JavaCourseFactory;
    
    public class Test {
        public static void main(String[] args) {
    
            // Java、Python、Go
            // 他们都有这些业务:可以观看、可以录屏、编写笔记、下载笔记
            // 视频:可以观看、可以录屏
            // 笔记:编写笔记、下载笔记
    
            JavaCourseFactory javaCourseFactory = new JavaCourseFactory();
            javaCourseFactory.createNoteService().edit();
            javaCourseFactory.createVideoService().record();
        }
    }
    
  • 相关阅读:
    [SAM4N学习笔记]UART的使用
    [SAN4N学习笔记]使用SysTick精准延时
    [SAM4N学习笔记]LED点灯程序
    [SAM4N学习笔记]SAM4N工程模板搭建
    C#用大石头Xcode做数据底层注意事项
    C#MVC中@HTML中的方法
    js比较时间大小(时间为以-分割的字符串时)
    C#后台Post提交XML 及接收该XML的方法
    jq对象和DOM对象的互换
    获取和设置HTML标签中的数据
  • 原文地址:https://www.cnblogs.com/chuanqi1995/p/16014125.html
Copyright © 2020-2023  润新知