`
大力水手
  • 浏览: 13326 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
社区版块
存档分类
最新评论

Adapter(适配器)

 
阅读更多
定义:
将两个不兼容的类纠合在一起使用,属于结构型模式,需要有Adaptee(被适配者)和Adaptor(适配器)两个身份

适用范围:
一般是需要复用两个或两个以上的类或接口中的某些方法.

package test;

import java.util.List;

public class Client {

public int getClienCount(){
return 1000;
}
         /**
*
* orther month .....
*
*/


}

package test;

import java.util.List;

public class Employee {

public int getEmployeeCount(){
return 100;
}
         /**
*
* orther month .....
*
*/
}

//如果我门需要Client 中的getClienCount(),Employee 中的getEmployeeCount()方法

package test.adapter;

public interface IEmployee {

public int getEmployeeCount();
}


package test.adapter.impl;

import test.Client;
import test.Employee;
import test.adapter.IEmployee;

public class EmployeeImpl extends Client implements IEmployee{

private Employee employee;

public EmployeeImpl(Employee employee){
this.employee = employee;
}

public int getEmployeeCount() {
return employee.getEmployeeCount();
}

public int getClientCount(){
return super.getClienCount();
}

}

//这样我们就在不改变某些类原代码 或在不知道源代码的情况下,成功的提取了类中的某些方法

package test.adapter;

import test.Employee;
import test.adapter.impl.EmployeeImpl;

public class AdapterTest {

public static void main(String args[]){
EmployeeImpl empl = new EmployeeImpl(new Employee());
System.out.println("eCount: " + empl.getClienCount());
System.out.println("cCount: " + empl.getEmployeeCount());
}
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics