Android源码中所使用到的设计模式design patterns

这篇博客探讨了Android源码中使用的设计模式,包括接口和抽象类、MVC架构、MVVM、组合模式、装饰模式、观察者模式以及工厂方法。文中详细解释了这些模式如何在Android系统中实现,例如,ViewGroup和View利用了组合模式,BroadcastReceiver体现了观察者模式,而工厂方法在创建对象时提供了灵活性。


一、接口和抽线类

(1)抽象类abstract class

           Object

               |

          Context

               |

      ContextWapper(抽象类)

          |               |             |

Service          Activity      .......


(2)接口interface

 


区别: (未写)

关键字:层次,方法集,多继承


二、MVC

  • You define your user interface in various XML files by resolution/hardware etc.
  • You define your resources in various XML files by locale etc.
  • You extend clases like ListActivity, TabActivity and make use of the XML file by inflaters
  • You can create as many classes as you wish for your model
  • A lot of Utils have been already written for you. DatabaseUtils, Html

Model:data

View:UI

Cotrollor:logic






三、MVVM



四、组合模式Composite

Android 的ViewGroup 和View就是用了组合模式。



四、Decorator 装饰模式

WIKI

• 为对象动态地绑定附加行为 --- Attach additional behavior to an object dynamically
• 不同于子类,它在运行时增添行为 --- Unlike subclassing, it adds behavior at runtime

装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

* 装饰模式的一些显著特性

1. 装饰对象的接口必须与被装饰对象的接口一致,即它们应拥有共同的父类,通常该类是abstract;

2. 应尽量保持abstract的父类简洁,父类应集中于定义接口而不是储存数据,对数据表示的定义应延迟到子类中;

3. 可以使用1个或多个 decorator来装饰一个对象;

4. 对象可以随时被装饰,当然也包括运行时动态装饰。


五、观察者模式 | 发布订阅模式 observer/observable pattern | publish/subscribepattern

Broadcast receivers are Android’s implementation of the Publish/Subscribe messaging pattern

在android系统中“广播”是发布订阅模式的典型应用。

定义对象间的一种一对多的依赖关系,发布者不需要知道订阅者是谁。当一个状态发生改变时, 所有监听它的观察者对象都得到通知并被自动更新。

例子如下:

(1)TimelineReceiver是观察者;

(2)重写OnReceive方法,来定义当收到状态变化时,订阅者的行为;

 



(3)注册和反注册



六、工厂方法 Factory Method Pattern 

The factory method pattern is a creational pattern. You can use it when you don't know witch object you must useat runtime.Advantage of this pattern; you can delay assigning action code till more information is available. It's also easy to add other classes (in the code example below; color classes).


The Factory method pattern is implemented in Android itself, one of the examples of this implementation is the

SocketFactory class and its subclasses. The SSLSocketFactory is used to create a specific SSLSocket,using the createSocket method, which opens a secure network connection and returns the SSLSocket instance.There can be more open connections and the socket factory provides the required instances.


七、组件与架构 Component Frameworks




---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





三、MVVM



四、组合模式Composite

Android 的ViewGroup 和View就是用了组合模式。



四、Decorator 装饰模式

WIKI

• 为对象动态地绑定附加行为 --- Attach additional behavior to an object dynamically
• 不同于子类,它在运行时增添行为 --- Unlike subclassing, it adds behavior at runtime

装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

* 装饰模式的一些显著特性

1. 装饰对象的接口必须与被装饰对象的接口一致,即它们应拥有共同的父类,通常该类是abstract;

2. 应尽量保持abstract的父类简洁,父类应集中于定义接口而不是储存数据,对数据表示的定义应延迟到子类中;

3. 可以使用1个或多个 decorator来装饰一个对象;

4. 对象可以随时被装饰,当然也包括运行时动态装饰。


五、观察者模式 | 发布订阅模式 observer/observable pattern | publish/subscribepattern

Broadcast receivers are Android’s implementation of the Publish/Subscribe messaging pattern

在android系统中“广播”是发布订阅模式的典型应用。

定义对象间的一种一对多的依赖关系,发布者不需要知道订阅者是谁。当一个状态发生改变时, 所有监听它的观察者对象都得到通知并被自动更新。

例子如下:

(1)TimelineReceiver是观察者;

(2)重写OnReceive方法,来定义当收到状态变化时,订阅者的行为;

 



(3)注册和反注册



六、工厂方法 Factory Method Pattern 

The factory method pattern is a creational pattern. You can use it when you don't know witch object you must useat runtime.Advantage of this pattern; you can delay assigning action code till more information is available. It's also easy to add other classes (in the code example below; color classes).


The Factory method pattern is implemented in Android itself, one of the examples of this implementation is the

SocketFactory class and its subclasses. The SSLSocketFactory is used to create a specific SSLSocket,using the createSocket method, which opens a secure network connection and returns the SSLSocket instance.There can be more open connections and the socket factory provides the required instances.


七、组件与架构 Component Frameworks




---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





三、MVVM



四、组合模式Composite

Android 的ViewGroup 和View就是用了组合模式。



四、Decorator 装饰模式

WIKI

• 为对象动态地绑定附加行为 --- Attach additional behavior to an object dynamically
• 不同于子类,它在运行时增添行为 --- Unlike subclassing, it adds behavior at runtime

装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

* 装饰模式的一些显著特性

1. 装饰对象的接口必须与被装饰对象的接口一致,即它们应拥有共同的父类,通常该类是abstract;

2. 应尽量保持abstract的父类简洁,父类应集中于定义接口而不是储存数据,对数据表示的定义应延迟到子类中;

3. 可以使用1个或多个 decorator来装饰一个对象;

4. 对象可以随时被装饰,当然也包括运行时动态装饰。


五、观察者模式 | 发布订阅模式 observer/observable pattern | publish/subscribepattern

Broadcast receivers are Android’s implementation of the Publish/Subscribe messaging pattern

在android系统中“广播”是发布订阅模式的典型应用。

定义对象间的一种一对多的依赖关系,发布者不需要知道订阅者是谁。当一个状态发生改变时, 所有监听它的观察者对象都得到通知并被自动更新。

例子如下:

(1)TimelineReceiver是观察者;

(2)重写OnReceive方法,来定义当收到状态变化时,订阅者的行为;

 



(3)注册和反注册



六、工厂方法 Factory Method Pattern 

The factory method pattern is a creational pattern. You can use it when you don't know witch object you must useat runtime.Advantage of this pattern; you can delay assigning action code till more information is available. It's also easy to add other classes (in the code example below; color classes).


The Factory method pattern is implemented in Android itself, one of the examples of this implementation is the

SocketFactory class and its subclasses. The SSLSocketFactory is used to create a specific SSLSocket,using the createSocket method, which opens a secure network connection and returns the SSLSocket instance.There can be more open connections and the socket factory provides the required instances.


七、组件与架构 Component Frameworks




----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





三、MVVM



四、组合模式Composite

Android 的ViewGroup 和View就是用了组合模式。



四、Decorator 装饰模式

WIKI

• 为对象动态地绑定附加行为 --- Attach additional behavior to an object dynamically
• 不同于子类,它在运行时增添行为 --- Unlike subclassing, it adds behavior at runtime

装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

* 装饰模式的一些显著特性

1. 装饰对象的接口必须与被装饰对象的接口一致,即它们应拥有共同的父类,通常该类是abstract;

2. 应尽量保持abstract的父类简洁,父类应集中于定义接口而不是储存数据,对数据表示的定义应延迟到子类中;

3. 可以使用1个或多个 decorator来装饰一个对象;

4. 对象可以随时被装饰,当然也包括运行时动态装饰。


五、观察者模式 | 发布订阅模式 observer/observable pattern | publish/subscribepattern

Broadcast receivers are Android’s implementation of the Publish/Subscribe messaging pattern

在android系统中“广播”是发布订阅模式的典型应用。

定义对象间的一种一对多的依赖关系,发布者不需要知道订阅者是谁。当一个状态发生改变时, 所有监听它的观察者对象都得到通知并被自动更新。

例子如下:

(1)TimelineReceiver是观察者;

(2)重写OnReceive方法,来定义当收到状态变化时,订阅者的行为;

 



(3)注册和反注册



六、工厂方法 Factory Method Pattern 

The factory method pattern is a creational pattern. You can use it when you don't know witch object you must useat runtime.Advantage of this pattern; you can delay assigning action code till more information is available. It's also easy to add other classes (in the code example below; color classes).


The Factory method pattern is implemented in Android itself, one of the examples of this implementation is the

SocketFactory class and its subclasses. The SSLSocketFactory is used to create a specific SSLSocket,using the createSocket method, which opens a secure network connection and returns the SSLSocket instance.There can be more open connections and the socket factory provides the required instances.


七、组件与架构 Component Frameworks


评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值