设计模式之适配器模式

设计模式之--适配器模式

1,定义:将一个类的接口转换成客户希望的另外一个接口;

2,分类:

        对象适配器模式:不是通过继承方式,而是通过对象组合方式来进行处理;

        类适配器模式:通过继承的方法实现,将旧系统的方法进行封装。对象适配器在进行适配器之间的转换过程时,无疑使用类适配器也能完成,但是依赖性会变大,并且根据适配的要求的灵活性,可能通过继承系统会膨胀到难以控制;

3,适用情况:

    1):当想要使用一个已经存在的类,但是该类的接口不符合现有的需求时;

    2):当需要创建一个可以被复用的子类,该类能够与其他无关的类甚至无法预见的类协同工作时;

    3):当需要使用一些已经存在的子类,但是不可能对所有的子类都进行子类化以匹配他们的我接口时,对象适配器可以对其父类接口进行适配;

4,代码:

     1),基本实现代码:

        主类:Main.class:

        public class Main {

           public static void main(String[] args) {

              Target target = new Target();

              target.Request();

              target = new Adapter();

              target.Request();

           }

        }

        客户希望的接口类:Target.class:

        public class Target {

           public void Request(){

              System.out.println("客户发起了普通基本需求");

           }

        }

        希望被复用的类:Adaptee.class:

        public class Adaptee {

           public void SpecificRequest() {

             // TODO Auto-generated method stub

              System.out.println("客户发起了特殊的需求");

           }

        }

        适配器类:Adapter.class:

        public class Adapter extends Target{

           private Adaptee adaptee = new Adaptee();

           @Override

           public void Request() {

              // TODO Auto-generated method stub

              adaptee.SpecificRequest();

           }

        }

        运行结果:

        

    2),实例代码:

        主类:Main.class:

        public class Main {

           public static void main(String[] args) {

              System.out.println("小面出国之前使用笔记本:");

              Adapter adapter = new Adapter220V();

              adapter.PowerSupply();

              System.out.println("小面出国之后使用笔记本:");

              adapter = new Adapter110V();

              adapter.PowerSupply();

           }

        }

        要被复用的类--110V电源接口类:PowerPort110V.class:

        /*110V电源接口*/

        public class PowerPort110V {

           public void PowerSupply() {//适配器提供电源

              // TODO Auto-generated method stub

              System.out.println("110V电源接口向外输出了110V的电压");

           }

        }

        要被复用的类--220V电源接口类:PowerPort220V.class:

        /*220V电源接口*/

        public class PowerPort220V {

           public void powerSupply(){//适配器提供电源

              System.out.println("110V的电源接口向外输出了220V的电压");

           }

        }

        适配器接口类:Adapter.class:

        /*适配器接口*/

        public interface Adapter {

           void PowerSupply();

        }

        适配器类:110V接口的适配器类:Adapter110V.class:

        /*110V接口的适配器类*/

        public class Adapter110V extends PowerPort110V implements Adapter{

           NoteBook noteBook = new NoteBook();

           @Override

           public void PowerSupply() {//适配器提供电源

              // TODO Auto-generated method stub

              super.PowerSupply();

              System.out.println("适配器将电压转换成了笔记本需要的");

              noteBook.Work();

           }

        }

        适配器类:220V接口的适配器类:Adapter220V.class:

        /*220V接口的适配器类*/

        public class Adapter220V extends PowerPort220V implements Adapter{

           NoteBook noteBook = new NoteBook();

           @Override

           public void PowerSupply() {//适配器提供电源

              // TODO Auto-generated method stub

              super.powerSupply();

              System.out.println("适配器将电压转换成了笔记本所需要的");

              noteBook.Work();

           }

        }

        要使用复用代码的类:NoteBook.class:

        /*笔记本类*/

        public class NoteBook {

           public void Work(){

              System.out.println("笔记本开始工作了");

           }

        }

运行结果:

    

 

posted @ 2015-08-11 10:52  依凡王子  阅读(186)  评论(0编辑  收藏  举报