zoukankan      html  css  js  c++  java
  • Adapter Pattern

    Introduction:you have a class with some functions which are not exactly what you can use directerly to sovle the problem you face with.so some adaption is needed to help you reuse the old class without any modification but you can also add some new functions to solve the fatal problem. In a word,we could build a new class with the old one.

    example: 

    You wanna to say "Hello world",before that you tend to do something else and clear the initialization after the class object died.

    So what you have is:

    class YouHave{
    public void hello(){
    System.out.print("Hello ");
    }
    public void world(){
    System.out.println("world.");
    }
    }

    Waht You wanna is:

    Do something of initialization.
    Hello world.
    Do something to release the source.

    We can use a interface to transit:

    abstract class YouWanna{
    abstract public void wanna();
    }

    Then build a new class(called adapter):

    class Surrogate extends YouWanna{
    private YouHave have;
    public Surrogate(YouHave have){
    this.have=have;
    }
    public void wanna(){
    System.out.println("Do something of initialization.");
    have.hello();
    have.world();
    System.out.println("Do something to release the source.");
    }
    }

    Now you can use it:

    public class AdapterPattern {
    public static void main(String[] args) {
    YouHave have=new YouHave();
    Surrogate surrogate=new Surrogate(have);
    surrogate.wanna();
    }
    }

    The Adapter parttern is often confused with Proxy parttern ,but when we use an adapter,
    we do not to keep the same interface as using the proxy one.
    The end.
  • 相关阅读:
    ASP获取客户端硬件信息(CPU、硬盘、主板、mac地址等)
    Java(多态)动手动脑
    每周进度条(第二周)
    Java(异常处理)动手动脑
    软件工程概论课后作业1
    mysqlmmm官方安装指南翻译
    Mysql 字符集的修改步骤
    Amoeba搞定mysql主从读写分离
    邮件系统postfix安装和设置
    mysqlmmm实现mysql高可用
  • 原文地址:https://www.cnblogs.com/ssMellon/p/6534275.html
Copyright © 2011-2022 走看看