设计模式之适配器模式

例子来自圣思园:


适配器模式将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间。有“两种”适配器:“对象”适配器和“类”适配器。


1)类适配器:使用继承,不需要重新实现整个被适配者。但是需要多重继承才能实现它,这在JAVA中是不可能的。

2)对象适配器:使用对象组合,以修改的接口包装被适配者,被适配者的任何子类都可以搭配着适配器使用。

3)默认适配器:


1、类适配器

package com.shengsiyuan.pattern.adapter;

public class Adaptee
{
	public void method2()
	{
		System.out.println("目标方法");
	}
}


package com.shengsiyuan.pattern.adapter;

public interface Target
{
	public void method1();
}

package com.shengsiyuan.pattern.adapter;

public class Adapter extends Adaptee implements Target
{
	@Override
	public void method1()
	{
		this.method2();
	}
}

package com.shengsiyuan.pattern.adapter;

public class Client
{
	public static void main(String[] args)
	{
		Target target = new Adapter();
		
		target.method1();
	}
}

2、对象适配器

package com.shengsiyuan.pattern.adapter2;

public class Adaptee
{
	public void method2()
	{
		System.out.println("执行方法");
	}
}

package com.shengsiyuan.pattern.adapter2;

public interface Target
{
	public void method1();
}

package com.shengsiyuan.pattern.adapter2;

public class Adapter implements Target
{
	private Adaptee adaptee;
	
	public Adapter(Adaptee adaptee)
	{
		this.adaptee = adaptee;
	}

	@Override
	public void method1()
	{
		adaptee.method2();
	}
}

package com.shengsiyuan.pattern.adapter2;

public class Client
{
	public static void main(String[] args)
	{
		Target target = new Adapter(new Adaptee());
		
		target.method1();
	}
}

3、默认适配器

package com.shengsiyuan.pattern.defaultadapter;

public interface AbstractService
{
	public void service1();
	
	public void service2();
	
	public void service3();
}

package com.shengsiyuan.pattern.defaultadapter;

public class ServiceAdapter implements AbstractService
{
	@Override
	public void service1()
	{
		
	}
	
	@Override
	public void service2()
	{
		
	}
	
	@Override
	public void service3()
	{
		
	}
}

package com.shengsiyuan.pattern.defaultadapter;

public class ConcreteService extends ServiceAdapter
{
	@Override
	public void service1()
	{
		System.out.println("执行业务方法");
	}
}

package com.shengsiyuan.pattern.defaultadapter;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class AwtApp
{
	public static void main(String[] args)
	{
		Frame frame = new Frame("title");

		frame.addMouseMotionListener(new MouseMotionAdapter()
		{
			@Override
			public void mouseMoved(MouseEvent e)
			{
				System.out.println("x: " + e.getX() + " y: " + e.getY());
			}
		});

		frame.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosing(WindowEvent e)
			{
				System.out.println("关闭窗口");

				System.exit(0);
			}
		});

		frame.setSize(new Dimension(50, 100));

		frame.setVisible(true);

	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值