package com.adapter;
import java.util.ArrayList;
import java.util.List;
import com.adapter.android.Android;
import com.adapter.android.AndroidAdapter;
import com.adapter.iphone.Iphone;
import com.adapter.iphone.IphoneAdapter;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Android android=new Android();
Iphone iphone=new Iphone();
Object work[]= {android,iphone};//放入充电设备
for(Object object:work) {//寻找
Adapter adapter=getChrgeadpter(object);//获取到该充电器类型
if(adapter!=null) {
System.out.println("找到适合该设备的适配器"+adapter);
String content=adapter.work(object);
if(content!=null&&!content.equals(""))
System.out.println(""+content);
}
}
}
private static Adapter getChrgeadpter(Object object) {
System.out.println("去寻找是否存在该设备类型的适配器");
List<Adapter> adapters=getAlladapter();//获取所有的充电器类型来做判断
System.out.println("适配器共有"+adapters.size()+"个");
for(Adapter adapter:adapters) {
if(adapter.support(object)) {//支持
return adapter;
}
}
return null;
}
public static List<Adapter> getAlladapter() {//返回所有充电适配器
System.out.println("返回所有充电适配器");
IphoneAdapter ipone=new IphoneAdapter();
AndroidAdapter android=new AndroidAdapter();
List<Adapter> adapters=new ArrayList();
adapters.add(ipone);
adapters.add(android);
return adapters;
}
}
package com.adapter;
import java.util.ArrayList;
import java.util.List;
public interface Adapter {
public boolean support(Object object);//是否支持
public String work(Object work);//调用设备功能 :充电
}
package com.adapter.iphone;
import com.adapter.function;
public class Iphone implements function{
public String charge() {
return "我可以给iphone充电";
}
}
package com.adapter.iphone;
import com.adapter.Adapter;
public class IphoneAdapter implements Adapter{
@Override
public boolean support(Object object) {//判断是否支持
// TODO Auto-generated method stub
System.out.println("当前需要充电的设备对象是"+object);
return object instanceof Iphone;
}
@Override
public String work(Object work) {//如果可以就调用设备功能
// TODO Auto-generated method stub
System.out.println("我是iphone充电器");
return ((Iphone) work).charge();
}
}
package com.adapter.android;
import com.adapter.Adapter;
import com.adapter.iphone.Iphone;
public class AndroidAdapter implements Adapter{
@Override
public boolean support(Object object) {
// TODO Auto-generated method stub
System.out.println("当前需要充电的设备对象是"+object);
return object instanceof Android;
}
@Override
public String work(Object work) {
// TODO Auto-generated method stub
System.out.println("我是Android充电器");
return ((Android)work).charge();
}
}
package com.adapter;
public interface function {
public String charge();
}
package com.adapter.android;
import com.adapter.Adapter;
import com.adapter.function;
import com.adapter.iphone.Iphone;
public class Android implements function{
public String charge() {
return "我可以给Android充电";
}
}
去寻找是否存在该设备类型的适配器
返回所有充电适配器
适配器共有2个
当前需要充电的设备对象是com.adapter.android.Android@659e0bfd
当前需要充电的设备对象是com.adapter.android.Android@659e0bfd
找到适合该设备的适配器com.adapter.android.AndroidAdapter@2a139a55
我是Android充电器
我可以给Android充电
去寻找是否存在该设备类型的适配器
返回所有充电适配器
适配器共有2个
当前需要充电的设备对象是com.adapter.iphone.Iphone@15db9742
找到适合该设备的适配器com.adapter.iphone.IphoneAdapter@6d06d69c
我是iphone充电器
我可以给iphone充电