文章目录
适配器模式 1.引出适配器模式
2.类适配器 1.基本介绍 2.类图 3.代码实现 1.Voltage220V.java 2.Voltage5V.java 3.VoltageAdapter.java 4.Phone.java 5.Client.java 6.结果
4.类适配器的注意事项
3.对象适配器 1.基本介绍 2.使用对象适配器改进类适配器
3.代码实现 1.修改VoltageAdapter.java 构造器聚合被适配的对象 2.修改Client.java 传入被适配的对象
4.接口适配器模式 1.基本介绍 2.类图 3.目录结构 4.代码实现 1.Interface.java 2.AbsAdapter.java 3.ClassA.java 4.结果
适配器模式 1.引出适配器模式
2.类适配器 1.基本介绍 2.类图 3.代码实现 1.Voltage220V.java 2.Voltage5V.java 3.VoltageAdapter.java 4.Phone.java 5.Client.java 6.结果
4.类适配器的注意事项
3.对象适配器 1.基本介绍 2.使用对象适配器改进类适配器
3.代码实现 1.修改VoltageAdapter.java 构造器聚合被适配的对象 2.修改Client.java 传入被适配的对象
4.接口适配器模式 1.基本介绍 2.类图 3.目录结构 4.代码实现 1.Interface.java 2.AbsAdapter.java 3.ClassA.java 4.结果
适配器模式
1.引出适配器模式
1.多功能转换插头
2.基本介绍
3.工作原理
2.类适配器
1.基本介绍
2.类图
3.代码实现
1.Voltage220V.java
package com. sun. classadapter ;
public class Voltage220V {
public int output220V ( ) {
int src = 220 ;
System . out. println ( "电压输出为" + src + "伏" ) ;
return src;
}
}
2.Voltage5V.java
package com. sun. classadapter ;
public interface Voltage5V {
public int output5V ( ) ;
}
3.VoltageAdapter.java
package com. sun. classadapter ;
public class VoltageAdapter extends Voltage220V implements Voltage5V {
@Override
public int output5V ( ) {
int srcV = super . output220V ( ) ;
int disV = srcV / 44 ;
return disV;
}
}
4.Phone.java
package com. sun. classadapter ;
public class Phone {
public void charging ( Voltage5V voltage5V) {
if ( voltage5V. output5V ( ) == 5 ) {
System . out. println ( "电压为5V,适配成功,可以开始充电" ) ;
} else {
System . out. println ( "电压不为5V,无法充电!" ) ;
}
}
}
5.Client.java
package com. sun. classadapter ;
public class Client {
public static void main ( String [ ] args) {
Phone phone = new Phone ( ) ;
phone. charging ( new VoltageAdapter ( ) ) ;
}
}
6.结果
4.类适配器的注意事项
3.对象适配器
1.基本介绍
2.使用对象适配器改进类适配器
1.类图
2.介绍
3.代码实现
1.修改VoltageAdapter.java 构造器聚合被适配的对象
package com. sun. objectadapter ;
public class VoltageAdapter implements Voltage5V {
private Voltage220V voltage220V;
public VoltageAdapter ( Voltage220V voltage220V) {
this . voltage220V = voltage220V;
}
@Override
public int output5V ( ) {
int disV = 0 ;
if ( voltage220V != null ) {
int srcV = voltage220V. output220V ( ) ;
disV = srcV / 44 ;
}
return disV;
}
}
2.修改Client.java 传入被适配的对象
package com. sun. objectadapter ;
public class Client {
public static void main ( String [ ] args) {
Phone phone = new Phone ( ) ;
phone. charging ( new VoltageAdapter ( new Voltage220V ( ) ) ) ;
}
}
4.接口适配器模式
1.基本介绍
2.类图
3.目录结构
4.代码实现
1.Interface.java
package com. sun. interfaceadapter ;
public interface Interface {
public void m1 ( ) ;
public void m2 ( ) ;
public void m3 ( ) ;
public void m4 ( ) ;
}
2.AbsAdapter.java
package com. sun. interfaceadapter ;
public abstract class AbsAdapter implements Interface {
@Override
public void m4 ( ) {
}
@Override
public void m3 ( ) {
}
@Override
public void m2 ( ) {
}
@Override
public void m1 ( ) {
}
}
3.ClassA.java
package com. sun. interfaceadapter ;
public class ClassA {
public static void main ( String [ ] args) {
AbsAdapter absAdapter = new AbsAdapter ( ) {
@Override
public void m1 ( ) {
System . out. println ( "重写了m1方法" ) ;
}
} ;
absAdapter. m1 ( ) ;
}
}
4.结果
适配器模式
1.引出适配器模式
1.多功能转换插头
2.基本介绍
3.工作原理
2.类适配器
1.基本介绍
2.类图
3.代码实现
1.Voltage220V.java
package com. sun. classadapter ;
public class Voltage220V {
public int output220V ( ) {
int src = 220 ;
System . out. println ( "电压输出为" + src + "伏" ) ;
return src;
}
}
2.Voltage5V.java
package com. sun. classadapter ;
public interface Voltage5V {
public int output5V ( ) ;
}
3.VoltageAdapter.java
package com. sun. classadapter ;
public class VoltageAdapter extends Voltage220V implements Voltage5V {
@Override
public int output5V ( ) {
int srcV = super . output220V ( ) ;
int disV = srcV / 44 ;
return disV;
}
}
4.Phone.java
package com. sun. classadapter ;
public class Phone {
public void charging ( Voltage5V voltage5V) {
if ( voltage5V. output5V ( ) == 5 ) {
System . out. println ( "电压为5V,适配成功,可以开始充电" ) ;
} else {
System . out. println ( "电压不为5V,无法充电!" ) ;
}
}
}
5.Client.java
package com. sun. classadapter ;
public class Client {
public static void main ( String [ ] args) {
Phone phone = new Phone ( ) ;
phone. charging ( new VoltageAdapter ( ) ) ;
}
}
6.结果
4.类适配器的注意事项
3.对象适配器
1.基本介绍
2.使用对象适配器改进类适配器
1.类图
2.介绍
3.代码实现
1.修改VoltageAdapter.java 构造器聚合被适配的对象
package com. sun. objectadapter ;
public class VoltageAdapter implements Voltage5V {
private Voltage220V voltage220V;
public VoltageAdapter ( Voltage220V voltage220V) {
this . voltage220V = voltage220V;
}
@Override
public int output5V ( ) {
int disV = 0 ;
if ( voltage220V != null ) {
int srcV = voltage220V. output220V ( ) ;
disV = srcV / 44 ;
}
return disV;
}
}
2.修改Client.java 传入被适配的对象
package com. sun. objectadapter ;
public class Client {
public static void main ( String [ ] args) {
Phone phone = new Phone ( ) ;
phone. charging ( new VoltageAdapter ( new Voltage220V ( ) ) ) ;
}
}
4.接口适配器模式
1.基本介绍
2.类图
3.目录结构
4.代码实现
1.Interface.java
package com. sun. interfaceadapter ;
public interface Interface {
public void m1 ( ) ;
public void m2 ( ) ;
public void m3 ( ) ;
public void m4 ( ) ;
}
2.AbsAdapter.java
package com. sun. interfaceadapter ;
public abstract class AbsAdapter implements Interface {
@Override
public void m4 ( ) {
}
@Override
public void m3 ( ) {
}
@Override
public void m2 ( ) {
}
@Override
public void m1 ( ) {
}
}
3.ClassA.java
package com. sun. interfaceadapter ;
public class ClassA {
public static void main ( String [ ] args) {
AbsAdapter absAdapter = new AbsAdapter ( ) {
@Override
public void m1 ( ) {
System . out. println ( "重写了m1方法" ) ;
}
} ;
absAdapter. m1 ( ) ;
}
}
4.结果