zoukankan      html  css  js  c++  java
  • 依赖倒转原则

    普通方式

    public class DependecyInversionPutong {
    
        public static void main(String[] args) {
            new Person().receiver(new Email());
    
        }
    }
    
    
    class Email {
        public String getInfo() {
            return "email content is " + "hello world";
        }
    }
    
    
    class Person {
        public void receiver(Email email){
            System.out.println(email.getInfo());
        }
    }
    View Code

    升级方式

    public class DependecyInversion {
        public static void main(String[] args) {
            new Person().receiver(new Email());
        }
    }
    
    interface IReceiver {
        public String getInfo();
    }
    
    class Email implements IReceiver {
        public String getInfo() {
            return "email content is " + "hello world";
        }
    }
    
    
    class Person {
        public void receiver(IReceiver receiver) {
            System.out.println(receiver.getInfo());
        }
    }
    View Code

    依赖关系传递的三种方式

    public class DependencyPass {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ChangHong changHong = new ChangHong();
    //        OpenAndClose openAndClose = new OpenAndClose();
    //        openAndClose.open(changHong);
            
            //通过构造器进行依赖传递
    //        OpenAndClose openAndClose = new OpenAndClose(changHong);
    //        openAndClose.open();
            //通过setter方法进行依赖传递
            OpenAndClose openAndClose = new OpenAndClose();
            openAndClose.setTv(changHong);
            openAndClose.open();
    
        }
    
    }
    
    // 方式1: 通过接口传递实现依赖
    // 开关的接口
    // interface IOpenAndClose {
    // public void open(ITV tv); //抽象方法,接收接口
    // }
    //
    // interface ITV { //ITV接口
    // public void play();
    // }
    // 
    // class ChangHong implements ITV {
    //
    //    @Override
    //    public void play() {
    //        // TODO Auto-generated method stub
    //        System.out.println("长虹电视机,打开");
    //    }
    //     
    // }
    //// 实现接口
    // class OpenAndClose implements IOpenAndClose{
    // public void open(ITV tv){
    // tv.play();
    // }
    // }
    
    // 方式2: 通过构造方法依赖传递
    // interface IOpenAndClose {
    // public void open(); //抽象方法
    // }
    // interface ITV { //ITV接口
    // public void play();
    // }
    // class OpenAndClose implements IOpenAndClose{
    // public ITV tv; //成员
    // public OpenAndClose(ITV tv){ //构造器
    // this.tv = tv;
    // }
    // public void open(){
    // this.tv.play();
    // }
    // }
    
    
    // 方式3 , 通过setter方法传递
    interface IOpenAndClose {
        public void open(); // 抽象方法
    
        public void setTv(ITV tv);
    }
    
    interface ITV { // ITV接口
        public void play();
    }
    
    class OpenAndClose implements IOpenAndClose {
        private ITV tv;
    
        public void setTv(ITV tv) {
            this.tv = tv;
        }
    
        public void open() {
            this.tv.play();
        }
    }
    
    class ChangHong implements ITV {
    
        @Override
        public void play() {
            // TODO Auto-generated method stub
            System.out.println("长虹电视机,打开");
        }
         
    }

    依赖倒转原则的注意事项和细节

  • 相关阅读:
    [转载] <深入理解.NET> 导读
    32bit Assembler is Easy, why and how to develop using the assembler; start learning to program in Assembly now!
    一致代码段,非一致代码段
    Data Mining、Data Warehousing、OLAP三者关系 [收藏]
    对比Windows和Linux两系统的动态库
    Win32汇编开发环境介绍和RadAsm简明教程
    How to make a 32 bit protected mode boot sector.
    龙芯CPU 参数
    并发编程第一章简单介绍和环境准备
    并发编程第三章线程创建、原理、常用线程方法
  • 原文地址:https://www.cnblogs.com/cerofang/p/14105971.html
Copyright © 2011-2022 走看看