2008-05-05
Adapter模式(Class Adapter)
昨天讨论了Adapter模式的一种情况Object Adapter,他比较适用于某些时候的二次开发,但我们拿到某一个组件的class文件,没有源码,只有功能列表的情况时,某些时候整合需要Object adapter。Adapter还有另外一种情况 Class Adapter。
我们现在的程序使用接口 PutBags
如果我们有一个类的class文件PutFootBall,而无法适用其源码。只能适用其中的putAllFootBall()方法。
而在我们现有的程序所适用的方式是
如果我们现在不想改变PutBags接口,并且不希望改变PutAllFootBall()此时
我们考虑,如何让Put类既有第三方类的功能,又不改变原有的接口继承?简
单的讲,要想某个类拥有其他的类的功能继承是最普遍的方法。所以我们使用
继承并实现的方式
我们现在的程序使用接口 PutBags
public interface PutBags {
public void putAllBags();
}
如果我们有一个类的class文件PutFootBall,而无法适用其源码。只能适用其中的putAllFootBall()方法。
public class PutFootBall {
public void putAllFootBall(){
System.out.println("Put Football....");
}
}
而在我们现有的程序所适用的方式是
public class Put implements PutBags{
public void putAllBags(){
System.out.println("Realize method....");
}
public void putOther(){
System.out.println("Other....");
}
}
如果我们现在不想改变PutBags接口,并且不希望改变PutAllFootBall()此时
我们考虑,如何让Put类既有第三方类的功能,又不改变原有的接口继承?简
单的讲,要想某个类拥有其他的类的功能继承是最普遍的方法。所以我们使用
继承并实现的方式
public class Put extends PutFootBall implements PutBags{
public void putAllBags(){
putBall();
}
}
评论
bloodrate
2008-06-11
我觉得不如改成这样:
public class Put implements PutBags{
PutFootBall pb = new PutFootBall()
public void putAllBags(){
putBall();
}
public void putAllFootBall(){
pb.putAllFootBall();
}
}
我总觉得adapter最佳用处是用来实现某些类接口不统一的情况得
public class Put implements PutBags{
PutFootBall pb = new PutFootBall()
public void putAllBags(){
putBall();
}
public void putAllFootBall(){
pb.putAllFootBall();
}
}
我总觉得adapter最佳用处是用来实现某些类接口不统一的情况得
xql80329
2008-06-03
putBall(); 哪儿呢?
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 6393 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
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






评论排行榜