zoukankan      html  css  js  c++  java
  • 代理模式 自己写的

    package test;
    
    public interface Person {
    	
    	public void work();
    
    }
    

      

    package test;
    
    public class People implements Person{
    
    	public void work() {
    		System.out.println("People work()");
    		
    	}
    
    }
    

      

    package test;
    
    public interface Study {
    
    	public void study();
    }
    

      

    package test;
    
    public class GoodStudy implements Study{
    
    	public void study() {
    		System.out.println("goodstudy study()");
    	}
    
    }
    

      适配器:

    package test;
    
    public class Adapter implements Person{
    	
    	private Study study ;
    	
    	private Person person ;
    
    	public Adapter(Study study2){
    		this.study = study2;
    	}
    	
    	public Adapter(People people){
    		this.person = person;
    	}
    	
    	public Adapter(Study study, Person person) {
    		super();
    		this.study = study;
    		this.person = person;
    	}
    
    	public void work() {
    		System.out.println("adapter work()");
    		person.work();
    		study.study();
    	}
    
    	
    	
    
    }
    

      

    package test;
    
    public class AdapterTest {
    	
    	public static void main(String[] args) {
    		Person person = new Adapter(new GoodStudy(),new People());
    		person.work();
    	}
    
    }
    

      结果:

    adapter work()
    People work()
    goodstudy study()
    

      Person为新功能 study是需要适配的功能

    双向适配器

    package test;
    
    public class AdapterTwo implements Person , Study {
    	
    	private Person person;
    	private Study study;
    	
    	
    
    	public AdapterTwo(Person person, Study study) {
    		super();
    		this.person = person;
    		this.study = study;
    	}
    
    	public void study() {
    		person.work();
    	}
    
    	public void work() {
    		study.study();
    	}
    
    }
    

      

    package test;
    
    import org.junit.Test;
    
    public class AdapterTest {
    	
    	@Test
    	public void test() {
    		Person person = new Adapter(new GoodStudy(),new People());
    		person.work();
            输出:

    adapter work()
    People work()
    goodstudy study()

    	}
    	
    	@Test
    	public void test2(){
    		Person person = new AdapterTwo(new People(),new GoodStudy());
    		person.work();
             
           输出:goodstudy study()
    	}
    
    }
    

      

    类适配器

    package test;
    
    import org.junit.Test;
    
    public class AdapterClass extends GoodStudy implements Person {
    
    	public void work() {
    		this.study();
    	}
    	
    	@Test
    	public void test(){
             //方式1 AdapterClass adapterClass = new AdapterClass(); adapterClass.work();
             //方式2
    work();

    } }

      结果:

    goodstudy study()

  • 相关阅读:
    线段树、最短路径、最小生成树、并查集、二分图匹配、最近公共祖先--C++模板
    数据结构中各种树
    你必须知道的基本位运算技巧(状压DP、搜索优化都会用到)
    memset为int型数组初始化问题
    Dev-C++添加代码格式化(format source code)工具Artistic Style
    让vs IIS Express支持本地静态Json文件
    什么是「供给侧改革」? 原标题:中央提的“供给侧改革”是啥意思?有谁能解说下吗?
    PowerDesigner用法和技巧
    php 调用 web Server(短信接口示例)
    sae相关配置
  • 原文地址:https://www.cnblogs.com/a757956132/p/4431289.html
Copyright © 2011-2022 走看看