适配器模式(Adapter Pattern)

 

适配器模式是就和现实一个样子,给你一个电脑适配器,它就能提供给你一个电源使用的稳定电压,不管外界是多少F,它的输出电压是不变的。

 

适配器模式分为4个部分
1.功能适配器(定义必须要用接口)

2.对象适配器(进行转换)

3.目标适配对象

4.源适配对象

 

如图,使用适配器之后,系统对待外来数据和内部数据没啥两样不用做任何更改。



适配器模式(Adapter Pattern)_第1张图片
 
下面就以抓取度娘和谷哥的网页数据来牛刀小试一把。

IHtml 类适配器

BaiduHtmlAdapter、GoogleHtmlAdapter 对象适配器

Html 目标适配对象

BaiduHtml、GoogleHtml 源适配对象

IHtml

 

package j2se.pattern;

/**
 * class Adapter
 * @author Lyon Yao
 *
 */
public interface IHtml {

	public abstract String getTitle();

	public abstract String getBody();

}

 

 
Html

 

package j2se.pattern;

/**
 * target object
 * @author Lyon Yao
 *
 */
public class Html implements IHtml {
	private String title;
	private String body;
	/* (non-Javadoc)
	 * @see j2se.pattern.IHtml#getTitle()
	 */
	@Override
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	/* (non-Javadoc)
	 * @see j2se.pattern.IHtml#getBody()
	 */
	@Override
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	
}

 

 GoogleHtml 

 

package j2se.pattern;

import java.util.HashMap;

/**
 * source object
 * @author Lyon Yao
 *
 */
public class GoogleHtml {
	protected HashMap<String, Object> data=new HashMap<String, Object>();
	
	public void setTitle(String title){
		data.put("title",title);
	}
	
	public void setBody(String body){
		 data.put("body",body);
	}
}

 

 BaiduHtml

 

package j2se.pattern;


/**
 * source object
 * @author Lyon Yao
 *
 */
public class BaiduHtml {
	protected String[] data=new String[2];
	
	public void setTitle(String title){
		data[0]=title;
	}
	
	public void setBody(String body){
		data[1]=body;
	}
}

 

 
GoogleHtmlAdapter

 

package j2se.pattern;

/**
 * object Adapter
 * @author Lyon Yao
 *
 */
public class GoogleHtmlAdapter extends GoogleHtml implements IHtml {

	public GoogleHtmlAdapter(GoogleHtml googleHtml) {
		// TODO Auto-generated constructor stub
		super.data=googleHtml.data;
	}

	@Override
	public String getTitle() {
		// TODO Auto-generated method stub
		return super.data.get("title").toString();
	}

	@Override
	public String getBody() {
		// TODO Auto-generated method stub
		return super.data.get("body").toString();
	}

}

 

 BaiduHtmlAdapter

 

package j2se.pattern;

/**
 * object Adapter
 * @author Lyon Yao
 *
 */
public class BaiduHtmlAdapter extends BaiduHtml implements IHtml{

	public BaiduHtmlAdapter(BaiduHtml baiduHtml) {
		// TODO Auto-generated constructor stub
		super.data=baiduHtml.data;
	}

	@Override
	public String getTitle() {
		// TODO Auto-generated method stub
		return super.data[0];
	}

	@Override
	public String getBody() {
		// TODO Auto-generated method stub
		return super.data[1];
	}
	
}

 

 
App

 

package j2se.pattern;

import java.util.ArrayList;

/**
 * Client
 * @author Lyon Yao
 *
 */
public class App {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		java.util.List<IHtml> list=new ArrayList<IHtml>(0);
		Html html=new Html();
		html.setTitle("this is our html");
		
		list.add(html);
		
		GoogleHtml googleHtml=new GoogleHtml();
		googleHtml.setTitle("this is googleHtml");
		GoogleHtmlAdapter googleHtmlAdapter=new GoogleHtmlAdapter(googleHtml);
		list.add(googleHtmlAdapter);
		

		BaiduHtml baiduHtml=new BaiduHtml();
		baiduHtml.setTitle("this is baiduHtml");
		
		BaiduHtmlAdapter baiduHtmlAdapter=new BaiduHtmlAdapter( baiduHtml);
		list.add(baiduHtmlAdapter);
		
		
		for(IHtml iHtml:list){
			System.out.println(iHtml.getTitle());
		}
		
	}

}

 


呵呵,大功告成,运行一下看看是不是没有什么问题吧?我们的App只抓title  没做任何更改。

 

 

你可能感兴趣的:(java,Pattern,Adapter)