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


一、接口和抽线类

(1)抽象类abstract class

           Object

               |

          Context

               |

      ContextWapper(抽象类)

          |               |             |

Service          Activity      .......


(2)接口interface

public class Activity extends ContextThemeWrapper implements LayoutInflater.Factory, Window.Callback, KeyEvent.Callback, OnCreateContextMenuListener, ComponentCallbacks { ... }  


区别: (未写)

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


二、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就是用了组合模式。


import java.util.List; import java.util.ArrayList; /** "Component" */ interface Graphic { //Prints the graphic. public void print(); } /** "Composite" */ class CompositeGraphic implements Graphic { //Collection of child graphics. private List mChildGraphics = new ArrayList(); //Prints the graphic. public void print() { for (Graphic graphic : mChildGraphics) { graphic.print(); } } //Adds the graphic to the composition. public void add(Graphic graphic) { mChildGraphics.add(graphic); } //Removes the graphic from the composition. public void remove(Graphic graphic) { mChildGraphics.remove(graphic); } } /** "Leaf" */ class Ellipse implements Graphic { //Prints the graphic. public void print() { System.out.println("Ellipse"); } } /** Client */ public class Program { public static void main(String[] args) { //Initialize four ellipses Ellipse ellipse1 = new Ellipse(); Ellipse ellipse2 = new Ellipse(); Ellipse ellipse3 = new Ellipse(); Ellipse ellipse4 = new Ellipse(); //Initialize three composite graphics CompositeGraphic graphic = new CompositeGraphic(); CompositeGraphic graphic1 = new CompositeGraphic(); CompositeGraphic graphic2 = new CompositeGraphic(); //Composes the graphics graphic1.add(ellipse1); graphic1.add(ellipse2); graphic1.add(ellipse3); graphic2.add(ellipse4); graphic.add(graphic1); graphic.add(graphic2); //Prints the complete graphic (four times the string "Ellipse"). graphic.print(); } }

四、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方法,来定义当收到状态变化时,订阅者的行为;

... class TimelineReceiver extends BroadcastReceiver { // 1 @Override public void onReceive(Context context, Intent intent) { // 2 cursor.requery(); // 3 adapter.notifyDataSetChanged(); // 4 Log.d("TimelineReceiver", "onReceived"); } } ...  



(3)注册和反注册

registerReceiver(receiver, filter); ...... unregisterReceiver(receiver); 


六、工厂方法 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就是用了组合模式。


import java.util.List; import java.util.ArrayList; /** "Component" */ interface Graphic { //Prints the graphic. public void print(); } /** "Composite" */ class CompositeGraphic implements Graphic { //Collection of child graphics. private List mChildGraphics = new ArrayList(); //Prints the graphic. public void print() { for (Graphic graphic : mChildGraphics) { graphic.print(); } } //Adds the graphic to the composition. public void add(Graphic graphic) { mChildGraphics.add(graphic); } //Removes the graphic from the composition. public void remove(Graphic graphic) { mChildGraphics.remove(graphic); } } /** "Leaf" */ class Ellipse implements Graphic { //Prints the graphic. public void print() { System.out.println("Ellipse"); } } /** Client */ public class Program { public static void main(String[] args) { //Initialize four ellipses Ellipse ellipse1 = new Ellipse(); Ellipse ellipse2 = new Ellipse(); Ellipse ellipse3 = new Ellipse(); Ellipse ellipse4 = new Ellipse(); //Initialize three composite graphics CompositeGraphic graphic = new CompositeGraphic(); CompositeGraphic graphic1 = new CompositeGraphic(); CompositeGraphic graphic2 = new CompositeGraphic(); //Composes the graphics graphic1.add(ellipse1); graphic1.add(ellipse2); graphic1.add(ellipse3); graphic2.add(ellipse4); graphic.add(graphic1); graphic.add(graphic2); //Prints the complete graphic (four times the string "Ellipse"). graphic.print(); } }

四、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方法,来定义当收到状态变化时,订阅者的行为;

... class TimelineReceiver extends BroadcastReceiver { // 1 @Override public void onReceive(Context context, Intent intent) { // 2 cursor.requery(); // 3 adapter.notifyDataSetChanged(); // 4 Log.d("TimelineReceiver", "onReceived"); } } ...  



(3)注册和反注册

registerReceiver(receiver, filter); ...... unregisterReceiver(receiver); 


六、工厂方法 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就是用了组合模式。


import java.util.List; import java.util.ArrayList; /** "Component" */ interface Graphic { //Prints the graphic. public void print(); } /** "Composite" */ class CompositeGraphic implements Graphic { //Collection of child graphics. private List mChildGraphics = new ArrayList(); //Prints the graphic. public void print() { for (Graphic graphic : mChildGraphics) { graphic.print(); } } //Adds the graphic to the composition. public void add(Graphic graphic) { mChildGraphics.add(graphic); } //Removes the graphic from the composition. public void remove(Graphic graphic) { mChildGraphics.remove(graphic); } } /** "Leaf" */ class Ellipse implements Graphic { //Prints the graphic. public void print() { System.out.println("Ellipse"); } } /** Client */ public class Program { public static void main(String[] args) { //Initialize four ellipses Ellipse ellipse1 = new Ellipse(); Ellipse ellipse2 = new Ellipse(); Ellipse ellipse3 = new Ellipse(); Ellipse ellipse4 = new Ellipse(); //Initialize three composite graphics CompositeGraphic graphic = new CompositeGraphic(); CompositeGraphic graphic1 = new CompositeGraphic(); CompositeGraphic graphic2 = new CompositeGraphic(); //Composes the graphics graphic1.add(ellipse1); graphic1.add(ellipse2); graphic1.add(ellipse3); graphic2.add(ellipse4); graphic.add(graphic1); graphic.add(graphic2); //Prints the complete graphic (four times the string "Ellipse"). graphic.print(); } }

四、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方法,来定义当收到状态变化时,订阅者的行为;

... class TimelineReceiver extends BroadcastReceiver { // 1 @Override public void onReceive(Context context, Intent intent) { // 2 cursor.requery(); // 3 adapter.notifyDataSetChanged(); // 4 Log.d("TimelineReceiver", "onReceived"); } } ...  



(3)注册和反注册

registerReceiver(receiver, filter); ...... unregisterReceiver(receiver); 


六、工厂方法 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就是用了组合模式。


import java.util.List; import java.util.ArrayList; /** "Component" */ interface Graphic { //Prints the graphic. public void print(); } /** "Composite" */ class CompositeGraphic implements Graphic { //Collection of child graphics. private List mChildGraphics = new ArrayList(); //Prints the graphic. public void print() { for (Graphic graphic : mChildGraphics) { graphic.print(); } } //Adds the graphic to the composition. public void add(Graphic graphic) { mChildGraphics.add(graphic); } //Removes the graphic from the composition. public void remove(Graphic graphic) { mChildGraphics.remove(graphic); } } /** "Leaf" */ class Ellipse implements Graphic { //Prints the graphic. public void print() { System.out.println("Ellipse"); } } /** Client */ public class Program { public static void main(String[] args) { //Initialize four ellipses Ellipse ellipse1 = new Ellipse(); Ellipse ellipse2 = new Ellipse(); Ellipse ellipse3 = new Ellipse(); Ellipse ellipse4 = new Ellipse(); //Initialize three composite graphics CompositeGraphic graphic = new CompositeGraphic(); CompositeGraphic graphic1 = new CompositeGraphic(); CompositeGraphic graphic2 = new CompositeGraphic(); //Composes the graphics graphic1.add(ellipse1); graphic1.add(ellipse2); graphic1.add(ellipse3); graphic2.add(ellipse4); graphic.add(graphic1); graphic.add(graphic2); //Prints the complete graphic (four times the string "Ellipse"). graphic.print(); } }

四、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方法,来定义当收到状态变化时,订阅者的行为;

... class TimelineReceiver extends BroadcastReceiver { // 1 @Override public void onReceive(Context context, Intent intent) { // 2 cursor.requery(); // 3 adapter.notifyDataSetChanged(); // 4 Log.d("TimelineReceiver", "onReceived"); } } ...  



(3)注册和反注册

registerReceiver(receiver, filter); ...... unregisterReceiver(receiver); 


六、工厂方法 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


你可能感兴趣的:(Android源码中所使用到的设计模式design patterns)