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()

  • 相关阅读:
    三种编程命名规则(匈牙利法,小驼峰法,大驼峰法)
    javaSE_Java第一周总结:有难度题目集合
    javaSE_06Java中的数组(array)-练习
    javaSE_06Java中的数组(array)-思维导图
    javaSE_06Java中的数组(array)
    Python10_代码规范和可读性
    URL&URI
    Python9_类
    Python8_关于编码解码和utf-8
    Python7_内置函数总结
  • 原文地址:https://www.cnblogs.com/a757956132/p/4431289.html
Copyright © 2011-2022 走看看