ITeye博客

Adapter模式(Object Adapter)

Ozone · 2008-05-05 00:12 · 设计模式 · 企业架构 · Markdown

Adapter模式使原本由于接口不兼容而不能在一起工作的类可以一起工作,即为一个内容合适但接口不匹配的对象创建一个新的接口.Adapter模式让现存的对象适应新的类结构,而不受他们的接口限制. 例如现在,我们有一个球袋类BasketballBag负责完成装入篮球。


public class BasketballBag{
    public void putBasketball(){
        System.out.println("Put in BasketBall....");
    }
}

有一个专门负责装入操作的类的方法putBalls()来调用BasketballBag类中方法来放入篮球。


public class PutBall{
    public void putBalls(BasketballBag bb){
        bb.putBasketball();
    }
}

此时我们有一个足球类FootballBag负责装入完成装入足球。


public class FootballBag{
    public void putFootball(){
        System.out.println("Put in FootBall....");
    }
}

前面提到的类中的方法putBalls()用来调用BaskballBag中的方法来放入篮球,我们能不能使用同一个方法来放入足球呢?我们试一试


public class Main {
    public static void main(String[] args){
        PutBall pb = new PutBall();
		
        System.out.println("BasketBall");
        BasketballBag bb = new BasketballBag();
        pb.putBalls(bb);
		
        System.out.println("FootBall")
        FootballBag fb = new FootballBag();
        pb.putBalls(fb);
    }

}

运行报错,告诉我们参数类型不匹配。在上述程序中,如何使用


FootballBag fb = new FootballBag();
pb.putBalls(fb);

来执行呢? 那我们可以考虑使用一种办法是的pb.putBalls(fb) 中的 fb 参数类型能够和函数需要的类型相匹配。我们可以考虑让单独使用一个新的类,让他继承BasketballBag,并且加入扩展功能(调用/实例 FootballBag)。


public class BallBag extends BasketballBag{
    FootballBag fb;
    public BallBag(FootballBag fb){
        this.fb = fb;
    }
    public void putFootballs(){
        fb.putFootball();
    }
} 

类写好了,感觉不错,看看调试情况,修改一下刚刚的 Main类


public class Main {
    public static void main(String[] args){
        PutBall pb = new PutBall();
		
        System.out.println("BasketBall");
        BasketballBag bb = new BasketballBag();
        pb.putBalls(bb);
		
        System.out.println("FootBall")
        FootballBag fb = new FootballBag();
        BallBag bbag = new BallBag(fb);
        pb.putBalls(fb);
    }

}

ok,测试通过。通过这个例子,我们得到一种把接口不兼容的类混合在一起使用的方法,而这种方法可以被称作Adapter模式。

评论(6)

  1. #6 楼 · qingfengjushi1 · 2008-11-21 13:38

    LZ的例子测试过吗?

  2. #5 楼 · liuleigang · 2008-11-20 10:01

    楼主举的例子能测试通过?在BallBag的putBaseketBall方法中要调用putFootball。

  3. #4 楼 · Ozone · 2008-05-05 09:55

    In your opinion bbag don't equal to fb ? I think ,BallBag is a help object,which have equal BallBag with BasketballBag.

  4. #3 楼 · duanbo · 2008-05-05 09:32

    Ozone writes:

    Ok。No problem,next demo is class adapter.

    pls see my update.

  5. #2 楼 · Ozone · 2008-05-05 09:25

    Ok。No problem,next demo is class adapter.

  6. #1 楼 · duanbo · 2008-05-05 09:14

    It's the Object Adapter. Class Adapter need to be demo.

    What's more, I think there's a problem with the last paragraph.

    public class Main
    {
        public static void main(String[] args){   
            PutBall pb = new PutBall();   
               
            System.out.println("BasketBall");   
            BasketballBag bb = new BasketballBag();   
            pb.putBalls(bb);   
               
            System.out.println("FootBall");
            FootballBag fb = new FootballBag();   
            BallBag bbag = new BallBag(fb);   
            pb.putBalls(bbag);  //shouldn't be fb , in my opinion. 
        }
    }
    

    And the BallBag class override the putBasketball() fuction.

    class BallBag extends BasketballBag
    {
        FootballBag fb;
    
        public BallBag(FootballBag fb)
        {
            this.fb = fb;
        }
    
        public void putBasketball() //It will be better if the name is "putBall"
        {
            fb.putFootball();
        }
    }
    

    upwards is my understanding. I'm beginner of pattern. Talk about it together.

评论功能维护中,当前仅展示历史评论。