zoukankan      html  css  js  c++  java
  • 适配器模式

    在类Source里有一个已存在的方法sourceMethod(),现在有一个接口Target想通过一个适配器Adapter类来调用sourceMethod方法:

    1.Source类

    1 public class Source {
    2     public void sourceMethod(){
    3         System.out.println("Source Method...");
    4     }
    5 }

    2.target接口

    1 public interface Target {
    2     public void sourceMethod();
    3     public void otherMethod();
    4 }

    3.Adapter适配器类

     1 public class Adapter implements Target{
     2     
     3     private Source source;
     4     
     5     public Adapter(Source source){
     6         this.source = source;
     7     }
     8 
     9     @Override
    10     public void sourceMethod() {
    11         // TODO Auto-generated method stub
    12         source.sourceMethod();
    13     }
    14 
    15     @Override
    16     public void otherMethod() {
    17         // TODO Auto-generated method stub
    18         System.out.println("otherMethod...");
    19     }
    20     
    21 }

    4.测试类

    1 public class Test {
    2     public static void main(String[] args) {
    3         Target target = new Adapter(new Source());
    4         target.sourceMethod();
    5         target.otherMethod();
    6     }
    7 }
  • 相关阅读:
    HTTP
    spark-architecture
    SPARK SQL
    《算法:第四版》课后练习 1.1 答案
    随机生成六位不重复数值
    【转】策略与机制分离
    【转】Linux内核源码分析方法
    【转】机制与策略
    软件工程中的现代方法
    编码通用缩写表
  • 原文地址:https://www.cnblogs.com/fenglanglang/p/5999154.html
Copyright © 2011-2022 走看看