public interface SourceAble{
void method1();
void method2();
}
public class SourceAble2 extends ObjectWrapper {
@Override
public void method2() {
System.out.println("this is change method2");
}
}
class SourceAble1 extends ObjectWrapper {
@Override
public void method1() {
System.out.println("this is change method1");
}
}
class test{
public static void main(String[]args){
SourceAble1 sourceAble1 = new SourceAble1();
sourceAble1.method1();
sourceAble1.method2();
SourceAble2 sourceAble2 = new SourceAble2();
sourceAble2.method1();
sourceAble2.method2();
}
}
public abstract class ObjectWrapper implements SourceAble{
@Override
public void method1() {
System.out.println("this is original method1");
}
@Override
public void method2() {
System.out.println("this is original method2");
}
}
对象的适配器模式
对象的适配器模式 其他都不变 wrapper中持有source对象 通过该持有去调用原来的方法
public class ObjectAdapter {
public static void main(String[] args) {
TargetAble1 targetAble1=new Wrapper(new Source());
targetAble1.method1();
targetAble1.method2();
}
}
class Source {
public void method1() {
System.out.println("this is a oringinal method");
}
}
interface TargetAble1 {
public void method1();
public void method2();
}
class Wrapper implements TargetAble1{
private Source source;
public Wrapper(Source source) {
this.source = source;
}
@Override
public void method1() {
source.method1();
}
@Override
public void method2() {
System.out.println("this is a target method");
}
}