zoukankan      html  css  js  c++  java
  • effective解读-第五条 使用依赖注入引入资源

    1. 当一个类依赖于其他资源的时候,使用依赖注入的方式,资源由客户端指定。,而不使用单例或者静态工厂的方式(因为它两会限制引入资源的灵活性)。

    2. 依赖注入的资源具有不可变性,所以多个客户端可以共享依赖对象

    3. 依赖注入的方式适合于构造器、静态工厂、构建器。例如Spring的依赖注入可以通过工厂方式的注入,构造注入等

    interface Resource{
        void method();
    }
    class Resource1 implements Resource {
        public static final Resource resource = new Resource1();
        private Resource1(){
        }
        @Override
        public void method(){
            System.out.println("Resorce.method1");
        }
    }
    class Resource2 implements Resource{
        public static final Resource resource = new Resource2();
        private Resource2(){}
        @Override
        public void method(){
            System.out.println("Resorce.method2");
        }
    }
    ​
    /**
     * 只能使用Resource1
     */
     class StaticDemo {
        private static final Resource resource = Resource1.resource;
        private StaticDemo(){}
        public static void handle(){
            resource.method();
        }
    }
    ​
    /**
     * 只能使用Resource1
     */
    class SingletonDemo {
        private static final Resource resource = Resource2.resource;
        public static SingletonDemo singletonDemo = new SingletonDemo();
        private SingletonDemo(){}
        public  void handle(){
            resource.method();
        }
    }
    //书中提到上面的单例、工厂中添加set方法,然后客户端动态的设置值。这样做显得笨拙、容易出错并且不支持并行工作。
    /**
     * 使用set修改值得方式
     */
    class SingletonSetDemo {
        private Resource resource;
        public void setResource(Resource resource) {
            this.resource = resource;
        }
        public static SingletonSetDemo singletonSetDemo = new SingletonSetDemo();
    ​
        private SingletonSetDemo() {
        }
        public void handle() {
            resource.method();
        }
    }
    public class Main{
        public static void main(String[] args) {
            SingletonSetDemo singletonSetDemo = SingletonSetDemo.singletonSetDemo;
            //报错,在没由set值得时候该方法无法使用,并行完成任务得时候会出错
            //注意该处只有一个资源,实际业务中可能多个资源都需要set
            singletonSetDemo.handle();
            singletonSetDemo.setResource(Resource2.resource);
            singletonSetDemo.handle();
        }
    }
    /**
     * 这里是构造注入的方式
     * 也可以使用依赖注入的框架例如Spring等
     */
    class AutowireSingletonDemo{
        private  final Resource resource;
        public AutowireSingletonDemo(Resource resource) {
            this.resource = Objects.requireNonNull(resource);
        }
        public void handle(){
            resource.method();
        }
    }
     
    作者:刘志红

    -------------------------------------------

    个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!

    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

  • 相关阅读:
    【CF932E】Team Work
    【ZJOI2011】看电影
    【CQOI2011】放棋子
    【HAOI2010】计数
    【HNOI2009】有趣的数列
    【ZJOI2010】排列计数
    【FJOI2016】建筑师
    【USACO10 OPEN】三角形计数
    【HNOI2012】排队
    【HNOI2008】越狱
  • 原文地址:https://www.cnblogs.com/chengxuyuan-liu/p/14582245.html
Copyright © 2011-2022 走看看