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

来源:互联网 发布:淘宝卖家开通花呗流程 编辑:程序博客网 时间:2024/05/30 02:53


一、接口和抽线类

(1)抽象类abstract class

           Object

               |

          Context

               |

      ContextWapper(抽象类)

          |               |             |

Service          Activity      .......


(2)接口interface

[java] view plaincopy
  1. public class Activity extends ContextThemeWrapper  
  2.         implements LayoutInflater.Factory,  
  3.         Window.Callback, KeyEvent.Callback,  
  4.         OnCreateContextMenuListener, ComponentCallbacks {  
  5. ...  
  6. }  
 


区别: (未写)

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


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


[java] view plaincopy
  1. import java.util.List;  
  2. import java.util.ArrayList;  
  3.    
  4. /** "Component" */  
  5. interface Graphic {  
  6.    
  7.     //Prints the graphic.  
  8.     public void print();  
  9. }  
  10.    
  11. /** "Composite" */  
  12. class CompositeGraphic implements Graphic {  
  13.    
  14.     //Collection of child graphics.  
  15.     private List<graphic> mChildGraphics = new ArrayList<graphic>();  
  16.    
  17.     //Prints the graphic.  
  18.     public void print() {  
  19.         for (Graphic graphic : mChildGraphics) {  
  20.             graphic.print();  
  21.         }  
  22.     }  
  23.    
  24.     //Adds the graphic to the composition.  
  25.     public void add(Graphic graphic) {  
  26.         mChildGraphics.add(graphic);  
  27.     }  
  28.    
  29.     //Removes the graphic from the composition.  
  30.     public void remove(Graphic graphic) {  
  31.         mChildGraphics.remove(graphic);  
  32.     }  
  33. }  
  34.    
  35. /** "Leaf" */  
  36. class Ellipse implements Graphic {  
  37.    
  38.     //Prints the graphic.  
  39.     public void print() {  
  40.         System.out.println("Ellipse");  
  41.     }  
  42. }  
  43.    
  44. /** Client */  
  45. public class Program {  
  46.    
  47.     public static void main(String[] args) {  
  48.         //Initialize four ellipses  
  49.         Ellipse ellipse1 = new Ellipse();  
  50.         Ellipse ellipse2 = new Ellipse();  
  51.         Ellipse ellipse3 = new Ellipse();  
  52.         Ellipse ellipse4 = new Ellipse();  
  53.    
  54.         //Initialize three composite graphics  
  55.         CompositeGraphic graphic = new CompositeGraphic();  
  56.         CompositeGraphic graphic1 = new CompositeGraphic();  
  57.         CompositeGraphic graphic2 = new CompositeGraphic();  
  58.    
  59.         //Composes the graphics  
  60.         graphic1.add(ellipse1);  
  61.         graphic1.add(ellipse2);  
  62.         graphic1.add(ellipse3);  
  63.    
  64.         graphic2.add(ellipse4);  
  65.    
  66.         graphic.add(graphic1);  
  67.         graphic.add(graphic2);  
  68.    
  69.         //Prints the complete graphic (four times the string "Ellipse").  
  70.         graphic.print();  
  71.     }  
  72. }  
  73. </graphic></graphic>  

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

[java] view plaincopy
  1. ...  
  2. class TimelineReceiver extends BroadcastReceiver { // 1  
  3.   @Override  
  4.   public void onReceive(Context context, Intent intent) { // 2  
  5.     cursor.requery(); // 3  
  6.     adapter.notifyDataSetChanged(); // 4  
  7.     Log.d("TimelineReceiver""onReceived");  
  8.   }  
  9. }  
  10. ...  
 



(3)注册和反注册

[java] view plaincopy
  1. registerReceiver(receiver, filter);   
  2. ......  
  3. 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就是用了组合模式。


[java] view plaincopy
  1. import java.util.List;  
  2. import java.util.ArrayList;  
  3.    
  4. /** "Component" */  
  5. interface Graphic {  
  6.    
  7.     //Prints the graphic.  
  8.     public void print();  
  9. }  
  10.    
  11. /** "Composite" */  
  12. class CompositeGraphic implements Graphic {  
  13.    
  14.     //Collection of child graphics.  
  15.     private List<graphic> mChildGraphics = new ArrayList<graphic>();  
  16.    
  17.     //Prints the graphic.  
  18.     public void print() {  
  19.         for (Graphic graphic : mChildGraphics) {  
  20.             graphic.print();  
  21.         }  
  22.     }  
  23.    
  24.     //Adds the graphic to the composition.  
  25.     public void add(Graphic graphic) {  
  26.         mChildGraphics.add(graphic);  
  27.     }  
  28.    
  29.     //Removes the graphic from the composition.  
  30.     public void remove(Graphic graphic) {  
  31.         mChildGraphics.remove(graphic);  
  32.     }  
  33. }  
  34.    
  35. /** "Leaf" */  
  36. class Ellipse implements Graphic {  
  37.    
  38.     //Prints the graphic.  
  39.     public void print() {  
  40.         System.out.println("Ellipse");  
  41.     }  
  42. }  
  43.    
  44. /** Client */  
  45. public class Program {  
  46.    
  47.     public static void main(String[] args) {  
  48.         //Initialize four ellipses  
  49.         Ellipse ellipse1 = new Ellipse();  
  50.         Ellipse ellipse2 = new Ellipse();  
  51.         Ellipse ellipse3 = new Ellipse();  
  52.         Ellipse ellipse4 = new Ellipse();  
  53.    
  54.         //Initialize three composite graphics  
  55.         CompositeGraphic graphic = new CompositeGraphic();  
  56.         CompositeGraphic graphic1 = new CompositeGraphic();  
  57.         CompositeGraphic graphic2 = new CompositeGraphic();  
  58.    
  59.         //Composes the graphics  
  60.         graphic1.add(ellipse1);  
  61.         graphic1.add(ellipse2);  
  62.         graphic1.add(ellipse3);  
  63.    
  64.         graphic2.add(ellipse4);  
  65.    
  66.         graphic.add(graphic1);  
  67.         graphic.add(graphic2);  
  68.    
  69.         //Prints the complete graphic (four times the string "Ellipse").  
  70.         graphic.print();  
  71.     }  
  72. }  
  73. </graphic></graphic>  

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

[java] view plaincopy
  1. ...  
  2. class TimelineReceiver extends BroadcastReceiver { // 1  
  3.   @Override  
  4.   public void onReceive(Context context, Intent intent) { // 2  
  5.     cursor.requery(); // 3  
  6.     adapter.notifyDataSetChanged(); // 4  
  7.     Log.d("TimelineReceiver""onReceived");  
  8.   }  
  9. }  
  10. ...  
 



(3)注册和反注册

[java] view plaincopy
  1. registerReceiver(receiver, filter);   
  2. ......  
  3. 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就是用了组合模式。


[java] view plaincopy
  1. import java.util.List;  
  2. import java.util.ArrayList;  
  3.    
  4. /** "Component" */  
  5. interface Graphic {  
  6.    
  7.     //Prints the graphic.  
  8.     public void print();  
  9. }  
  10.    
  11. /** "Composite" */  
  12. class CompositeGraphic implements Graphic {  
  13.    
  14.     //Collection of child graphics.  
  15.     private List<graphic> mChildGraphics = new ArrayList<graphic>();  
  16.    
  17.     //Prints the graphic.  
  18.     public void print() {  
  19.         for (Graphic graphic : mChildGraphics) {  
  20.             graphic.print();  
  21.         }  
  22.     }  
  23.    
  24.     //Adds the graphic to the composition.  
  25.     public void add(Graphic graphic) {  
  26.         mChildGraphics.add(graphic);  
  27.     }  
  28.    
  29.     //Removes the graphic from the composition.  
  30.     public void remove(Graphic graphic) {  
  31.         mChildGraphics.remove(graphic);  
  32.     }  
  33. }  
  34.    
  35. /** "Leaf" */  
  36. class Ellipse implements Graphic {  
  37.    
  38.     //Prints the graphic.  
  39.     public void print() {  
  40.         System.out.println("Ellipse");  
  41.     }  
  42. }  
  43.    
  44. /** Client */  
  45. public class Program {  
  46.    
  47.     public static void main(String[] args) {  
  48.         //Initialize four ellipses  
  49.         Ellipse ellipse1 = new Ellipse();  
  50.         Ellipse ellipse2 = new Ellipse();  
  51.         Ellipse ellipse3 = new Ellipse();  
  52.         Ellipse ellipse4 = new Ellipse();  
  53.    
  54.         //Initialize three composite graphics  
  55.         CompositeGraphic graphic = new CompositeGraphic();  
  56.         CompositeGraphic graphic1 = new CompositeGraphic();  
  57.         CompositeGraphic graphic2 = new CompositeGraphic();  
  58.    
  59.         //Composes the graphics  
  60.         graphic1.add(ellipse1);  
  61.         graphic1.add(ellipse2);  
  62.         graphic1.add(ellipse3);  
  63.    
  64.         graphic2.add(ellipse4);  
  65.    
  66.         graphic.add(graphic1);  
  67.         graphic.add(graphic2);  
  68.    
  69.         //Prints the complete graphic (four times the string "Ellipse").  
  70.         graphic.print();  
  71.     }  
  72. }  
  73. </graphic></graphic>  

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

[java] view plaincopy
  1. ...  
  2. class TimelineReceiver extends BroadcastReceiver { // 1  
  3.   @Override  
  4.   public void onReceive(Context context, Intent intent) { // 2  
  5.     cursor.requery(); // 3  
  6.     adapter.notifyDataSetChanged(); // 4  
  7.     Log.d("TimelineReceiver""onReceived");  
  8.   }  
  9. }  
  10. ...  
 



(3)注册和反注册

[java] view plaincopy
  1. registerReceiver(receiver, filter);   
  2. ......  
  3. 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就是用了组合模式。


[java] view plaincopy
  1. import java.util.List;  
  2. import java.util.ArrayList;  
  3.    
  4. /** "Component" */  
  5. interface Graphic {  
  6.    
  7.     //Prints the graphic.  
  8.     public void print();  
  9. }  
  10.    
  11. /** "Composite" */  
  12. class CompositeGraphic implements Graphic {  
  13.    
  14.     //Collection of child graphics.  
  15.     private List<graphic> mChildGraphics = new ArrayList<graphic>();  
  16.    
  17.     //Prints the graphic.  
  18.     public void print() {  
  19.         for (Graphic graphic : mChildGraphics) {  
  20.             graphic.print();  
  21.         }  
  22.     }  
  23.    
  24.     //Adds the graphic to the composition.  
  25.     public void add(Graphic graphic) {  
  26.         mChildGraphics.add(graphic);  
  27.     }  
  28.    
  29.     //Removes the graphic from the composition.  
  30.     public void remove(Graphic graphic) {  
  31.         mChildGraphics.remove(graphic);  
  32.     }  
  33. }  
  34.    
  35. /** "Leaf" */  
  36. class Ellipse implements Graphic {  
  37.    
  38.     //Prints the graphic.  
  39.     public void print() {  
  40.         System.out.println("Ellipse");  
  41.     }  
  42. }  
  43.    
  44. /** Client */  
  45. public class Program {  
  46.    
  47.     public static void main(String[] args) {  
  48.         //Initialize four ellipses  
  49.         Ellipse ellipse1 = new Ellipse();  
  50.         Ellipse ellipse2 = new Ellipse();  
  51.         Ellipse ellipse3 = new Ellipse();  
  52.         Ellipse ellipse4 = new Ellipse();  
  53.    
  54.         //Initialize three composite graphics  
  55.         CompositeGraphic graphic = new CompositeGraphic();  
  56.         CompositeGraphic graphic1 = new CompositeGraphic();  
  57.         CompositeGraphic graphic2 = new CompositeGraphic();  
  58.    
  59.         //Composes the graphics  
  60.         graphic1.add(ellipse1);  
  61.         graphic1.add(ellipse2);  
  62.         graphic1.add(ellipse3);  
  63.    
  64.         graphic2.add(ellipse4);  
  65.    
  66.         graphic.add(graphic1);  
  67.         graphic.add(graphic2);  
  68.    
  69.         //Prints the complete graphic (four times the string "Ellipse").  
  70.         graphic.print();  
  71.     }  
  72. }  
  73. </graphic></graphic>  

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

[java] view plaincopy
  1. ...  
  2. class TimelineReceiver extends BroadcastReceiver { // 1  
  3.   @Override  
  4.   public void onReceive(Context context, Intent intent) { // 2  
  5.     cursor.requery(); // 3  
  6.     adapter.notifyDataSetChanged(); // 4  
  7.     Log.d("TimelineReceiver""onReceived");  
  8.   }  
  9. }  
  10. ...  
 



(3)注册和反注册

[java] view plaincopy
  1. registerReceiver(receiver, filter);   
  2. ......  
  3. 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


原创粉丝点击