2008-05-05
Adapter模式(Object Adapter)
Adapter模式使原本由于接口不兼容而不能在一起工作的类可以一起工作,即为一个内容合适但接口不匹配的对象创建一个新的接口.Adapter模式让现存的对象适应新的类结构,而不受他们的接口限制.
例如现在,我们有一个球袋类BasketballBag负责完成装入篮球。
有一个专门负责装入操作的类的方法putBalls()来调用BasketballBag类中方法来放入篮球。
此时我们有一个足球类FootballBag负责装入完成装入足球。
前面提到的类中的方法putBalls()用来调用BaskballBag中的方法来放入篮球,我们能不能使用同一个方法来放入足球呢?我们试一试
运行报错,告诉我们参数类型不匹配。在上述程序中,如何使用
来执行呢?
那我们可以考虑使用一种办法是的pb.putBalls(fb) 中的 fb 参数类型能够和函数需要的类型相匹配。我们可以考虑让单独使用一个新的类,让他继承BasketballBag,并且加入扩展功能(调用/实例 FootballBag)。
类写好了,感觉不错,看看调试情况,修改一下刚刚的 Main类
ok,测试通过。通过这个例子,我们得到一种把接口不兼容的类混合在一起使用的方法,而这种方法可以被称作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模式。
评论
Ozone
2008-05-05
In your opinion bbag don't equal to fb ? I think ,BallBag is a help object,which have equal BallBag with BasketballBag.
duanbo
2008-05-05
Ozone 写道
Ok。No problem,next demo is class adapter.
pls see my update.
Ozone
2008-05-05
Ok。No problem,next demo is class adapter.
duanbo
2008-05-05
It's the Object Adapter.
Class Adapter need to be demo.
What's more, I think there's a problem with the last paragraph.
And the BallBag class override the putBasketball() fuction.
upwards is my understanding. I'm beginner of pattern. Talk about it together.
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.
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 6394 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
Adapter模式(Class Adap ...
我觉得不如改成这样: public class Put implements ...
-- by bloodrate -
Decorator模式
还是要修改啊,原来得dowork不实现接口啊
-- by bloodrate -
Adapter模式(Class Adap ...
putBall(); 哪儿呢?
-- by xql80329 -
Decorator模式
希望以后发的时候能够注意一下错误
-- by xql80329 -
Decorator模式
jbon 写道看上去更像代理模式 赞同!!
-- by weili788






评论排行榜