Android设计模式初学:组合模式
来源:互联网 发布:淘宝网商城男装裤子 编辑:程序博客网 时间:2024/04/29 16:29
Android中对组合模式的应用,可谓是泛滥成粥,随处可见,那就是View和ViewGroup类的使用。在android UI设计,几乎所有的widget和布局类都依靠这两个类。
组合模式,Composite Pattern,是一个非常巧妙的模式。几乎所有的面向对象系统都应用到了组合模式。
1.意图
将对象View和ViewGroup组合成树形结构以表示"部分-整体"的层次结构(View可以做为ViewGroup的一部分)。
组合模式使得用户对单个对象View和组合对象ViewGroup的使用具有一致性。
热点词汇: 部分-整体 容器-内容 树形结构 一致性 叶子 合成 安全性 透明性
2.结构
针对View和ViewGroup的实际情况,我们选择安全式的组合模式(在组合对象中添加add,remove,getChild方法),添加少许的注释,我们把上图修改为:
3.代码
View类的实现:
- public class View{
- //... ...
- //省略了无关的方法
- }
ViewGroup的实现:
- public abstract class ViewGroup extends View{
- /**
- * Adds a child view.
- */
- public void addView(View child) {
- //...
- }
- public void removeView(View view) {
- //...
- }
- /**
- * Returns the view at the specified position in the group.
- */
- public View getChildAt(int index) {
- try {
- return mChildren[index];
- } catch (IndexOutOfBoundsException ex) {
- return null;
- }
- }
- //other methods
- }
4.效果
(1).结构型模式
(2).定义了包含基本对象和组合对象的类层次结构。这种结构能够灵活控制基本对象与组合对象的使用。
(3).简化客户代码。基本对象和组合对象有一致性,用户不用区分它们。
(4).使得更容易添加新类型的组件。
(5).使你的设计变得更加一般化。
内容转自:http://mobile.51cto.com/android-419145.htm
0 0
- Android设计模式初学:组合模式
- 【初学设计模式】Composite (组合)
- android设计模式-组合模式
- android 设计模式-组合模式
- 初学javascript设计模式(三)组合模式第一遍遍阅读
- Android设计模式系列-组合模式
- Android设计模式系列-组合模式
- Android设计模式系列-组合模式
- Android设计模式系列-组合模式
- Android设计模式系列-组合模式
- Android设计模式系列-组合模式
- Android设计模式系列-组合模式
- Android设计模式系列-组合模式
- Android设计模式系列-组合模式
- Android 常用设计模式1 组合模式
- Android设计模式系列-组合模式
- Android设计模式应用-组合模式
- Android设计模式系列-组合模式
- android 增量更新
- 01.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/x
- 抽象类与接口
- 上市不是万能药,2014年,这些知名科技企业无奈选择了退市
- Can't find keyplane that supports type 4 for keyboard iPhone-Portrait-NumberPad; using 3876877096_Po
- Android设计模式初学:组合模式
- Paint 属性
- 基于androidpn客户端修改的AndroidPNClient
- ejb3.0中persistence.xml文件
- Linux下报 bind error: Address already in use 解决方案
- java序列化<一> 部分没有看懂
- 关于java文件下载文件名乱码问题解决方案
- 算法导论第10章 基本数据结构
- 定位、地理位置编码、地理位置反编码