赞
踩
目录
3)代码实现
被适配的类
- //被适配的类
- public class Voltage220V {
- //输出220V的电压
- public int output220V() {
- int src = 220;
- System.out.println("电压=" + src + "伏");
- return src;
- }
- }
适配器类
- //适配器类
- public class VoltageAdapter extends Voltage220V implements IVoltage5V {
-
- @Override
- public int output5V() {
- // TODO Auto-generated method stub
- //获取到220V电压
- int srcV = output220V();
- int dstV = srcV / 44 ; //转成 5v
- return dstV;
- }
-
- }
适配接口
- //适配接口
- public interface IVoltage5V {
- public int output5V();
- }
手机类,有一个充电方法,参数为适配接口
- public class Phone {
-
- //充电
- public void charging(IVoltage5V iVoltage5V) {
- if(iVoltage5V.output5V() == 5) {
- System.out.println("电压为5V, 可以充电~~");
- } else if (iVoltage5V.output5V() > 5) {
- System.out.println("电压大于5V, 不能充电~~");
- }
- }
- }
创建手机进行充电
- public class Client {
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println(" === 类适配器模式 ====");
- Phone phone = new Phone();
- phone.charging(new VoltageAdapter());
- }
-
- }
被适配的类
- //被适配的类
- public class Voltage220V {
- //输出220V的电压
- public int output220V() {
- int src = 220;
- System.out.println("电压=" + src + "伏");
- return src;
- }
- }
适配器类
- //适配器类
- public class VoltageAdapter implements IVoltage5V {
-
- private Voltage220V voltage220V; // 关联关系-聚合
-
-
- //通过构造器,传入一个 Voltage220V 实例
- public VoltageAdapter(Voltage220V voltage220v) {
-
- this.voltage220V = voltage220v;
- }
-
-
-
- @Override
- public int output5V() {
-
- int dst = 0;
- if(null != voltage220V) {
- int src = voltage220V.output220V();//获取220V 电压
- System.out.println("使用对象适配器,进行适配~~");
- dst = src / 44;
- System.out.println("适配完成,输出的电压为=" + dst);
- }
-
- return dst;
-
- }
-
- }
适配接口
- //适配接口
- public interface IVoltage5V {
- public int output5V();
- }
手机类,有一个充电方法,参数为适配接口
- public class Phone {
-
- //充电
- public void charging(IVoltage5V iVoltage5V) {
- if(iVoltage5V.output5V() == 5) {
- System.out.println("电压为5V, 可以充电~~");
- } else if (iVoltage5V.output5V() > 5) {
- System.out.println("电压大于5V, 不能充电~~");
- }
- }
- }
创建手机进行充电
- public class Client {
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println(" === 类适配器模式 ====");
- Phone phone = new Phone();
- phone.charging(new VoltageAdapter());
- }
-
- }
与类适配器对比,就是接口不继承被适配类了,而是直接持有被适配类对象(作为接口中的一个属性)
- ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);
- valueAnimator.addListener(new Animator.AnimatorListener(){
- @Override
- public void onAnimationStart (Animator animation){
- }
- @Override
- public void onAnimationEnd (Animator animation){
- }
- @Override
- public void onAnimationCancel (Animator animation){
- }
- @Override
- public void onAnimationRepeat (Animator animation){
- }
- }
- );
- valueAnimator.start();
- ValueAnimator valueAnimator = ValueAnimator.ofInt(0,100);
- valueAnimator.addListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationStart(Animator animation) {
- //xxxx具体实现
- }
- });
- valueAnimator.start()
- public static interface AnimatorListener {
- void onAnimationStart(Animator animation);
- void onAnimationEnd(Animator animation);
- void onAnimationCancel(Animator animation);
- void onAnimationRepeat(Animator animation);
- }
-
- ==================================================
- public abstract class AnimatorListenerAdapter implements Animator.AnimatorListener
- Animator.AnimatorPauseListener {
- @Override //默认实现
- public void onAnimationCancel(Animator animation) {
- }
- @Override
- public void onAnimationEnd(Animator animation) {
- }
- @Override
- public void onAnimationRepeat(Animator animation) {
- }
- @Override
- public void onAnimationStart(Animator animation) {
- }
- @Override
- public void onAnimationPause(Animator animation) {
- }
- @Override
- public void onAnimationResume(Animator animation) {
- }
- }
- new AnimatorListenerAdapter() {
- @Override
- public void onAnimationStart(Animator animation) {
- //xxxx具体实现
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。