zoukankan      html  css  js  c++  java
  • 设计模式

    适配器模式(adapter pattern) 详细解释


    本文地址: http://blog.csdn.net/caroline_wendy


    适配器模式(adapter pattern): 将一个类的接口, 转换成客户期望的还有一个接口. 适配器让原本不兼容的类能够合作无间.


    适配器模式(adapter pattern)主要包含:

    1. 被适配者接口(adaptee interface): 须要被适配的接口.

    2. 适配器(adapter): 转换的类;

    3. 目标接口(target interface): 须要转换成为的接口.

    4. 客户(client): 详细使用的类.


    详细方法:

    1. 被适配者(adaptee interface)接口, 和实现接口详细的类.

    /**
     * @time 2014年6月17日
     */
    package adapter;
    
    /**
     * @author C.L.Wang
     *
     */
    public interface Turkey {
    	public void gobble();
    	public void fly();
    }
    
    
    /**
     * @time 2014年6月17日
     */
    package adapter;
    
    /**
     * @author C.L.Wang
     *
     */
    public class WildTurkey implements Turkey {
    
    	/* (non-Javadoc)
    	 * @see adapter.Turkey#gobble()
    	 */
    	@Override
    	public void gobble() {
    		// TODO Auto-generated method stub
    		System.out.println("Gobble gobble! ");
    	}
    
    	/* (non-Javadoc)
    	 * @see adapter.Turkey#fly()
    	 */
    	@Override
    	public void fly() {
    		// TODO Auto-generated method stub
    		System.out.println("I'm flying a short distance! ");
    	}
    
    }
    

    2. 目标接口(target interface), 和实现接口详细的类.

    /**
     * @time 2014年6月17日
     */
    package adapter;
    
    /**
     * @author C.L.Wang
     *
     */
    public interface Duck {
    	public void quack();
    	public void fly();
    }
    
    
    /**
     * @time 2014年6月17日
     */
    package adapter;
    
    /**
     * @author C.L.Wang
     *
     */
    public class MallardDuck implements Duck {
    
    	/* (non-Javadoc)
    	 * @see adapter.Duck#quack()
    	 */
    	@Override
    	public void quack() {
    		// TODO Auto-generated method stub
    		System.out.println("Quack! ");
    	}
    
    	/* (non-Javadoc)
    	 * @see adapter.Duck#fly()
    	 */
    	@Override
    	public void fly() {
    		// TODO Auto-generated method stub
    		System.out.println("I'm flying! ");
    	}
    
    }
    

    3. 适配器(adapter): 被适配者接口(adaptee interface) 转换 目标接口(target interface).

    组合被适配者类接口, 实现目标类接口.

    /**
     * @time 2014年6月17日
     */
    package adapter;
    
    /**
     * @author C.L.Wang
     *
     */
    public class TurkeyAdapter implements Duck {
    
    	Turkey turkey;
    	
    	public TurkeyAdapter (Turkey turkey) {
    		this.turkey = turkey;
    	}
    	
    	/* (non-Javadoc)
    	 * @see adapter.Duck#quack()
    	 */
    	@Override
    	public void quack() {
    		// TODO Auto-generated method stub
    		turkey.gobble();
    	}
    
    	/* (non-Javadoc)
    	 * @see adapter.Duck#fly()
    	 */
    	@Override
    	public void fly() {
    		// TODO Auto-generated method stub
    		for (int i=0; i<5; ++i) { //多次飞行
    			turkey.fly(); 
    		}
    	}
    
    }
    

    4. 客户(client)方法须要使用目标类, 

    把被适配者(adaptee)的详细类, 通过适配器(adapter), 转换为目标(target)类, 进行调用:

    /**
     * @time 2014年6月17日
     */
    package adapter;
    
    /**
     * @author C.L.Wang
     *
     */
    public class DuckTestDrive {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		MallardDuck duck = new MallardDuck();
    		
    		WildTurkey turkey = new WildTurkey();
    		Duck turkeyAdapter = new TurkeyAdapter(turkey);
    		
    		System.out.println("The Turkey says ...");
    		turkey.gobble();
    		turkey.fly();
    		
    		System.out.println("
    The Duck says ...");
    		testDuck(duck);
    		
    		System.out.println("
    The TurkeyAdapter says ...");
    		testDuck(turkeyAdapter);
    	}
    	
    	static void testDuck (Duck duck) {
    		duck.quack();
    		duck.fly();
    	}
    
    }
    

    5. 输出.

    The Turkey says ...
    Gobble gobble! 
    I'm flying a short distance! 
    
    The Duck says ...
    Quack! 
    I'm flying! 
    
    The TurkeyAdapter says ...
    Gobble gobble! 
    I'm flying a short distance! 
    I'm flying a short distance! 
    I'm flying a short distance! 
    I'm flying a short distance! 
    I'm flying a short distance! 
    






  • 相关阅读:
    apache安装
    docker搭建redis主从
    docker安装
    sklearn工具-绪论
    数学基础-矩阵和线性代数
    数学基础-概率论与贝叶斯先验
    数学基础-数学分析
    数据科学包——Matplotlib
    数据科学包——pandas
    数据科学包——numpy
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5183487.html
Copyright © 2011-2022 走看看