设计模式adapter

public class Adapter {

        public static void main(String agrs[]) {
                    Apple apple1 = new Apple();
                    apple1.getColor("red");

                    Orange orange = new Orange();
                    orange.getColor("green");

                    AppleAdapter aa = new AppleAdapter(orange);
                    aa.getColor("red");

                    OrangeAdapter oa = new OrangeAdapter(apple1);
                    oa.getColor("green");

        }

}

class Apple {

        void getColor(String s) {
                    System.out.println("apple " + s);
        }

}

class Orange {

        void getColor(String s) {
                    System.out.println("orange " + s);
        }

}

class AppleAdapter extends Apple {

        Orange orange;

        public AppleAdapter(Orange o) {
                    orange = o;
        }

        @Override
        public void getColor(String s) {
                    orange.getColor(s);
        }

}

class OrangeAdapter extends Orange {

        Apple apple;

        public OrangeAdapter(Apple apple1) {
                    apple = apple1;
        }

        @Override
        public void getColor(String s) {
                    apple.getColor(s);
        }

}

你可能感兴趣的:(java,设计模式)