23 design patterns in Android

#一、单例模型#
## 1. Definition ##
  A class has only one instance, and it is instantiated by itself, and this instance is provided to the entire system.

## 2. Use scenario ##
  Avoid multiple objects consuming too many resources; creating an object requires consuming too many resources; for example, accessing I/O, accessing the database, etc.

## 3. Singleton mode in Android ##
  LayoutInflaterApplication

## 4、More Reference ##
Java Design Pattern-Singleton Pattern

#二、Builder Mode #
## 1. Definition ##
  Separate the construction of a complex object from its representation, so that the same construction process can create different representations.

## 2. Usage scenario ##

  1. When the same method, different execution sequence, and different event results;

  2. Multiple components can be assembled into one object, but the resulting operating results are different;

  3. The product category is very complicated, or the calling sequence in the product category has different effects;

  4. Initializing an object is particularly complicated, such as many parameters.

## 3.Builder mode in Android ##
  AlertDialog.Builder

## 4、More Reference ##
Java uses Builder to solve the problem of too many constructor parameters

#三、prototype mode mode #
## 1. Definition ##
  Use prototype instances to specify the classes of objects to be created, and create new objects by copying (cloning) these prototypes.
  Similar to the copy constructor in C++.

## 2. Usage scenario ##

  1. Class initialization requires a lot of resources, which can be avoided through prototype replication;

  2. To newgenerate an object requires very cumbersome data preparation or access permissions;

  3. When an object needs to be provided to other objects for access, and each caller may need to modify its value, you can use the prototype mode to copy multiple objects for use by the caller, that is, Defensive Copy.

## 3. Prototype Mode in Android ##
  Intent

#四、Factory Method Pattern #
## 1. Definition ##
  Define an interface for creating objects and let the subclass decide which class to instantiate.

## 2. Use Scene ##
  Wherever complex objects need to be produced, the factory method pattern can be used.

## 3. The factory method pattern
  Activity in Android ## OnCreate()method

#五、Abstract Factory Method Pattern #
## 1. Definition ##
  Provide an interface for creating a group of related or interdependent objects without specifying their specific classes.

##2、使用场景##
  例如,用抽象工厂方法模式来产生Android、iOS下的拨号、短信程序。

##3、Android中的抽象工厂方法模式##
  MediaPlayer

#六、策略模式#
##1、定义##
  策略模式定义了一些列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。

##2、使用场景##

  1. 针对同一类型问题的多种处理方式,仅仅是具体行为有差别;

  2. 需要安全地封装多种同一类型的操作;

  3. 出现同一抽象类有多个子类,而又需要使用if-else或者switch-case来选择具体子类。

##3、Android中的策略模式##
  用于动画的时间插值器,Time Interpolator

#七、状态模式#
##1、定义##
  当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。

##2、使用场景##

  1. 一个对象的行为取决于它的状态,并且它必须在运行时根据状态改变它的行为;

  2. 代码中包含大量与这些对象状态有关的条件语句,并且这些分支依赖于该对象的状态。

##3、Android中的状态模式##
  WiFi管理

#八、责任链模式#
##1、定义##
  使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合。将这些对象连城一条链,并沿着这条链传递该请求,直到有对象处理它为止。

##2、使用场景##

  1. 多个对象可以处理同一个请求,但具体由哪个对象处理则在运行时动态决定;

  2. 在请求处理中不明确的情况下向多个对象中的一个提交一个请求;

  3. 需要动态指定一个组对象处理请求。

##3、Android中的责任链模式##
  对触屏事件(onTouchEvent)的分发、有序广播(Orderd Broadcast)

#九、解释器模式#
##1、定义##
  给定一个语言,定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子。

##2、使用场景##

  1. 如果某个简单的语言需要解释执行而且可以将语言中的语句表示为一个抽象语法树时;

  2. 在某些特定的领域出现不断重复的问题时, 可以将该领域的问题转化为一种语法规则下的语句,然后构建解释器来解释该语句。

##3、Android中的解释器模式##
  PackageParser类,用于读取Manifest.xml

#十、命令模式#
##1、定义##
  将一个请求封装成一个对象,从而让用户使用不同的请求把客户端参数化;对请求进行排队或者对请求日志进行记录,以及支持可撤销的操作。

##2、使用场景##

  1. 需要抽象出待执行的动作,然后以参数的形式提供出来;

  2. 在不同的时刻指定、排列和执行请求;

  3. 需要支持取消操作;

  4. 需要支持事务操作;

  5. 支持修改日志功能,这样当系统崩溃时,这些修改可以被重做一遍。

##3、Android中的命令模式##
  事件机制中,对事件的转发处理

#十一、观察者模式#
##1、定义##
  定义对象间一种一对多的依赖关系,使得每当一个对象改变状态时,所有依赖于它的对象都会得到通知并被自动更新。

##2、使用场景##

  1. 关联行为场景,需要注意的是,关联行为是可拆分的,而不是“组合”关系;

  2. 事件多级触发场景;

  3. 跨系统的消息交换场景,如消息队列、事件总线的处理机制。

##3、Android中的观察者模式##
  ListView中Adapter的notifyDataSetChanged()方法、BroadcastReceiver的onReceive()方法

#十二、备忘录模式#
##1、定义##
  在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样,以后就可以将该对象恢复到原先保存的状态。

##2、使用场景##

  1. 需要保存一个对象在某一时刻的全部或部分状态;

  2. 如果用一个接口来让其他对象得到这些状态,将会暴露对象的实现细节并破坏对象的封装性,一个对象不希望外界直接访问其内部状态,通过中间对象可以间接访问其内部状态。

##3、Android中的备忘录模式##
  Activity中的状态保存与恢复,即onSaveInstanceState()onRestoreInstanceState()方法

#十三、迭代器模式#
##1、定义##
  提供一种该方法顺序访问一个容器对象中的各个元素,而又不需要暴露该对象的内部表示。

##2、使用场景##

  遍历一个容器对象时。

##3、Android中的迭代器模式##
  使用SQLite Database的query方法查询数据库时,返回的Cursor游标对象

#十四、模板方法模式#
##1、定义##
  定义一个操作中的算法的框架,而将一些步骤延迟到子类中,使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。

##2、使用场景##

  1. 多个子类有公有的方法,并且逻辑基本相同时;

  2. 重要、复杂的算法,可以把核心算法设计为模板方法,周边的相关细节功能则由各个子类实现;

  3. 重构时,模板方法模式是一个经常使用的模式,把相同的代码抽取到父类中,然后通过钩子函数约束其行为。

##3、Android中的模板方法模式##
  AsyncTask类、Activity的各个生命周期函数

#十五、访问者模式#
##1、定义##
  封装一些作用于某种数据结构中的各元素的操作,它可以在不改变这个数据结构的前提下定义作用于这些元素的新的操作。

##2、使用场景##

  1. 对象结构比较稳定,但经常需要在此对象结构上定义新的操作;

  2. 需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而需要避免这些操作“污染”这些对象的类,也不希望在增加新操作时修改这些类。

##3、Android中的访问者模式##
  编译器注解

#十六、中介者模式#
##1、定义##
  中介者模式包装了一些列对象相互作用的方式,使得这些对象不必相互明显作用,从而使它们可以松散耦合。当某些对象之间的作用发生改变时,不会立即影响其他的一些对象之间的作用,保证这些作用可以彼此独立变化。中介者模式将多对多的相互作用转化为一对多的相互作用。

##2、使用场景##

  当对象之间的交互操作很多且每个对象的行为操作都依赖彼此时,为防止在修改一个对象的行为时,同时设计修改很多其他对象的行为,可采用中介者模式来解决紧耦合问题。中介者对象将系统从网状结构变成以调停者为中心的星状结构。

##3、Android中的中介者模式##
  锁屏KeyguardViewMediator类、Binder机制

#十七、代理模式#
##1、定义##
  为其他对象提供一种代理以控制对这个对象的访问。

##2、使用场景##

  当无法或不想直接访问某个对象或访问某个对象存在困难时可以通过一个代理对象来间接访问,为了保证客户端使用的透明性,委托对象与代理对象需要实现相同的接口。

##3、Android中的代理模式##
  ActivityManagerProxy类

#十八、组合模式#
##1、定义##
  将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。

##2、使用场景##

  1. 表示对象的部分-整体层次机构时;

  2. 从一个整体中能够独立出部分模块或功能的场景。

##3、Android中的组合模式##
  View和ViewGroup的组合

#十九、适配器模式#
##1、定义##
  适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起的两个类能够在一起工作。

##2、使用场景##

  1. 系统需要使用现有的类,而此类的接口不符合系统的需要,即接口不兼容;

  2. 想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作;

  3. 需要一个统一的输出接口,而输入端的类型不可预知。

##3、Android中的适配器模式##
  ListView的Adapter

#二十、装饰模式#
##1、定义##
  动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式生产子类更为灵活。

##2、使用场景##

  需要透明且动态地扩展类的功能时。

##3、Android中的装饰模式##
  Context类

#二十一、享元模式#
##1、定义##
  使用共享对象可以有效地支持大量的细粒度的对象。

##2、使用场景##

  1. 系统中存在大量的相似对象;

  2. 细粒度的对象都具备较接近的外部状态,而且内部状态与环境无关,也就是说对象没有特定身份;

  3. 需要缓冲池的场景。

## 3. Enjoyment mode in Android ##
  Message class in message queue

#二二、surface mode #
## 1. Definition ##
  requires that the communication between the outside of a subsystem and its inside must be carried out through a unified object. The appearance mode provides a high-level interface, making the subsystem easier to use.

## 2. Usage scenario ##

  1. Provide a simple interface for a complex subsystem;

  2. When you need to build a hierarchical structure of the subsystem, use the appearance model to define the entry point of each layer in the subsystem.

##   3.Appearance mode in Android ##
ContextImpl class

#十二三、桥接模型#
## 1. Definition ##
  Separate the abstract part from the implementation part so that they can be changed independently.

## 2. Usage scenario ##

  1. If a system needs to add more flexibility between the abstract role and the concrete role of the component, and avoid establishing a static integration connection between the two levels, you can use the bridge mode to make them establish an association relationship at the abstract layer.

  2. For those systems that do not want to use integration or the number of system classes increases sharply due to multi-level inheritance, you can also consider using the bridge mode;

  3. There are two independently changing dimensions for a class, and both dimensions need to be expanded.

## 3. Bridge mode in Android ##
  Between Adapter and AdapterView, between Window and WindowManager

Guess you like

Origin blog.csdn.net/michael_f2008/article/details/78106489