设计模式学习之模版方法模式
2017-04-21 15:05
423 查看
模板方法模式是一种基于继承的代码复用的行为型模式;在其结构中只存在父类与子类之间的继承关系。通过使用模板方法模式,可以将一些复杂流程的实现步骤封装在一系列基本方法中,在抽象父类中提供一个称之为模板方法的方法来定义这些基本方法的执行次序,而通过其子类来覆盖某些步骤,从而使得相同的算法框架可以有不同的执行结果。本篇博客我们一起来学习模版方法模式。
定义
模板方法模式:定义一个操作中算法的框架,而将一些步骤延迟到子类中。模板方法模式使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
ULM图
模板方法模式包含如下两个角色:
(1) AbstractClass(抽象类):在抽象类中定义了一系列基本操作(PrimitiveOperations),这些基本操作可以是具体的,也可以是抽象的,每一个基本操作对应算法的一个步骤,在其子类中可以重定义或实现这些步骤。同时,在抽象类中实现了一个模板方法(Template Method),用于定义一个算法的框架,模板方法不仅可以调用在抽象类中实现的基本方法,也可以调用在抽象类的子类中实现的基本方法,还可以调用其他对象中的方法。
(2) ConcreteClass(具体子类):它是抽象类的子类,用于实现在父类中声明的抽象基本操作以完成子类特定算法的步骤,也可以覆盖在父类中已经实现的具体基本操作。
对于模板方法模式,父类提供的构建步骤和顺序或者算法骨架,通常是不希望甚至是不允许子类去覆盖的,所以在某些场景中,可以直接将父类中提供骨架的方法声明为final类型。
模版方法模式可以说在我们项目中随处可见,最常见的就是我们平时写的各种Base类,BaseActivity,BaseFragment等,说到模板方法模式,ClassLoader类就使用了模板模式,去保证类加载过程中的唯一性
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
上面中只是展示了ClassLoader中与模版模式相关内容,从上面的代码中,可以看出,findClass这个方法,并不是必须实现的,所以ClassLoader选择留给程序员们自己选择是否要覆盖。
ClassLoader中定义的算法顺序是:
1,首先看是否有已经加载好的类。
2,如果父类加载器不为空,则首先从父类类加载器加载。
3,如果父类加载器为空,则尝试从启动加载器加载。
4,如果两者都失败,才尝试从findClass方法加载。
这是ClassLoader的双亲委派模型,即先从父类加载器加载,直到继承体系的顶层,否则才会采用当前的类加载器加载。这样做的目的是为了JVM中类的一致性。
无疑 ClassLoader 中就定义了模版方法,而ClassLoader 的子类BaseDexClassLoader中就覆盖了ClassLoader 的findClass方法
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
在Android源码中,View中的Draw()方法就是一个“模板方法”。它定义了一系列“Draw”过程,主要包括这几个步骤:
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
其中第三步( Step 3)Draw view’s content函数:
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
第四步( Step 4) draw children
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
从View的Draw()“模板方法”可以看出,当继承View子类中,如果要重写或者扩展这个方法时,整个方法流程和基本内容不能够修改,子类只能通过扩展onDraw(Canvas canvas)和dispatchDraw(Canvas canvas)两个函数,使子类自己的View显示效果和别的具体子类的不同。
我们可以看到,在TextView类中就重写了onDraw方法
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Android中的AsyncTask也典型的模板方法模式
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
继承AsyncTask的时候只需要根据需要重写上面几个方法就可以,它们就是AsyncTask类的可变部分,我们在子类中只需要实现可变部分就可以了,不变部分AsyncTask已经实现了,所以我们只需要根据这个模板进行使用就行。
总结模版方法模式的适用情况:
适用情况:
(1)一次性实现一个算法的不变部分,并将可变的行为留给子类去实现。
(2)各个子类中公共的行为应该被提取出来并且集中到一个公共的父类中去,这样避免了代码的重复。
(3)扩展子类的扩展。模板方法只在特定点调用操作,这样就只允许在这些点进行扩展。
定义与UML图
定义 模板方法模式:定义一个操作中算法的框架,而将一些步骤延迟到子类中。模板方法模式使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
ULM图
模板方法模式包含如下两个角色:
(1) AbstractClass(抽象类):在抽象类中定义了一系列基本操作(PrimitiveOperations),这些基本操作可以是具体的,也可以是抽象的,每一个基本操作对应算法的一个步骤,在其子类中可以重定义或实现这些步骤。同时,在抽象类中实现了一个模板方法(Template Method),用于定义一个算法的框架,模板方法不仅可以调用在抽象类中实现的基本方法,也可以调用在抽象类的子类中实现的基本方法,还可以调用其他对象中的方法。
(2) ConcreteClass(具体子类):它是抽象类的子类,用于实现在父类中声明的抽象基本操作以完成子类特定算法的步骤,也可以覆盖在父类中已经实现的具体基本操作。
对于模板方法模式,父类提供的构建步骤和顺序或者算法骨架,通常是不希望甚至是不允许子类去覆盖的,所以在某些场景中,可以直接将父类中提供骨架的方法声明为final类型。
模版方法模式的例子
模版方法模式可以说在我们项目中随处可见,最常见的就是我们平时写的各种Base类,BaseActivity,BaseFragment等,说到模板方法模式,ClassLoader类就使用了模板模式,去保证类加载过程中的唯一性public class ClassLoader { //这是一个重载方法 public Class<?> loadClass(String name) throws ClassNotFoundException { return loadClass(name, false); } //父类算法的定义 protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Class c = findLoadedClass(name); if (c == null) { try { if (parent != null) { c = parent.loadClass(name, false); } else { c = findBootstrapClass0(name); } } catch (ClassNotFoundException e) { c = findClass(name); } } if (resolve) { resolveClass(c); } return c; } //这里留了一个方法给子类选择性覆盖 protected Class<?> findClass(String name) throws ClassNotFoundException { throw new ClassNotFoundException(name); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
上面中只是展示了ClassLoader中与模版模式相关内容,从上面的代码中,可以看出,findClass这个方法,并不是必须实现的,所以ClassLoader选择留给程序员们自己选择是否要覆盖。
ClassLoader中定义的算法顺序是:
1,首先看是否有已经加载好的类。
2,如果父类加载器不为空,则首先从父类类加载器加载。
3,如果父类加载器为空,则尝试从启动加载器加载。
4,如果两者都失败,才尝试从findClass方法加载。
这是ClassLoader的双亲委派模型,即先从父类加载器加载,直到继承体系的顶层,否则才会采用当前的类加载器加载。这样做的目的是为了JVM中类的一致性。
无疑 ClassLoader 中就定义了模版方法,而ClassLoader 的子类BaseDexClassLoader中就覆盖了ClassLoader 的findClass方法
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { Class clazz = pathList.findClass(name); if (clazz == null) { throw new ClassNotFoundException(name); } return clazz; }1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
Android中的模版方法模式
在Android源码中,View中的Draw()方法就是一个“模板方法”。它定义了一系列“Draw”过程,主要包括这几个步骤:/* * Draw traversal performs several drawing steps which must be executed * in the appropriate order: * * 1. Draw the background * 2. If necessary, save the canvas' layers to prepare for fading * 3. Draw view's content * 4. Draw children * 5. If necessary, draw the fading edges and restore layers * 6. Draw decorations (scrollbars for instance) */1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
// Step 1, draw the background, if needed int saveCount; if (!dirtyOpaque) { drawBackground(canvas); } // skip step 2 & 5 if possible (common case) final int viewFlags = mViewFlags; boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0; boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0; if (!verticalEdges && !horizontalEdges) { // Step 3, draw the content if (!dirtyOpaque) onDraw(canvas); // Step 4, draw the children dispatchDraw(canvas); // Overlay is part of the content and draws beneath Foreground if (mOverlay != null && !mOverlay.isEmpty()) { mOverlay.getOverlayView().dispatchDraw(canvas); } // Step 6, draw decorations (foreground, scrollbars) onDrawForeground(canvas); // we're done... return; }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
其中第三步( Step 3)Draw view’s content函数:
/** * Implement this to do your drawing. * * @param canvas the canvas on which the background will be drawn */ protected void onDraw(Canvas canvas) { }1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
第四步( Step 4) draw children
/** * Called by draw to draw the child views. This may be overridden * by derived classes to gain control just before its children are drawn * (but after its own view has been drawn). * @param canvas the canvas on which to draw the view */ protected void dispatchDraw(Canvas canvas) { }1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
从View的Draw()“模板方法”可以看出,当继承View子类中,如果要重写或者扩展这个方法时,整个方法流程和基本内容不能够修改,子类只能通过扩展onDraw(Canvas canvas)和dispatchDraw(Canvas canvas)两个函数,使子类自己的View显示效果和别的具体子类的不同。
我们可以看到,在TextView类中就重写了onDraw方法
@Override protected void onDraw(Canvas canvas) { if (mPreDrawState == PREDRAW_DONE) { final ViewTreeObserver observer = getViewTreeObserver(); observer.removeOnPreDrawListener(this); mPreDrawState = PREDRAW_NOT_REGISTERED; } if (mCurrentAlpha <= ViewConfiguration.ALPHA_THRESHOLD_INT) return; restartMarqueeIfNeeded(); // Draw the background for this view super.onDraw(canvas); final int compoundPaddingLeft = getCompoundPaddingLeft(); final int compoundPaddingTop = getCompoundPaddingTop(); final int compoundPaddingRight = getCompoundPaddingRight(); final int compoundPaddingBottom = getCompoundPaddingBottom(); final int scrollX = mScrollX; final int scrollY = mScrollY; final int right = mRight; final int left = mLeft; final int bottom = mBottom; final int top = mTop; final Drawables dr = mDrawables; if (dr != null) { /* * Compound, not extended, because the icon is not clipped * if the text height is smaller. ....... }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Android中的AsyncTask也典型的模板方法模式
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { @Override protected void onPreExecute() { super.onPreExecute(); } protected Bitmap doInBackground(String... urls) { return loadImageFromNetwork(urls[0]); } @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } protected void onPostExecute(Bitmap result) { mImageView.setImageBitmap(result); } @Override protected void onCancelled() { super.onCancelled(); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
继承AsyncTask的时候只需要根据需要重写上面几个方法就可以,它们就是AsyncTask类的可变部分,我们在子类中只需要实现可变部分就可以了,不变部分AsyncTask已经实现了,所以我们只需要根据这个模板进行使用就行。
总结模版方法模式的适用情况:
适用情况:
(1)一次性实现一个算法的不变部分,并将可变的行为留给子类去实现。
(2)各个子类中公共的行为应该被提取出来并且集中到一个公共的父类中去,这样避免了代码的重复。
(3)扩展子类的扩展。模板方法只在特定点调用操作,这样就只允许在这些点进行扩展。
相关文章推荐
- 设计模式学习之模版方法模式
- 设计模式学习日志六:模版方法模式 (原文转载)
- 设计模式学习之模版方法模式
- 设计模式学习5--模版方法模式
- 学习设计模式-模版方法
- 设计模式学习笔记(五)模版方法模式
- 设计模式学习总结11 - 行为型6 - TemplateMethod模版方法模式
- C#设计模式学习小结之八 模版方法模式
- 设计模式与泡mm的关系之template method模版方法模式及再思考
- 设计模式学习笔记四:工厂方法(Factory Method)
- HeadFirst 设计模式学习笔记8--模板方法模式
- 设计模式--模版方法(TemplateMethod)
- 拨开“繁”云见天日 -- 学习设计模式模板方法有感
- 设计模式--模版方法(TemplateMethod)
- 设计模式简单学习(1)--虚方法(virtual)和抽象方法(abstract)的区别
- 设计模式学习(二)工厂方法
- 设计模式(三)之模版方法模式(Template method Pattern)
- 设计模式学习笔记(十六)——Template Method模板方法模式
- 设计模式学习笔记(十六)——Template Method模板方法模式
- 设计模式学习日志(14)-TemplateMethod模板方法(行为型模式)