Skip to content

Commit 07efe74

Browse files
committed
添加装饰者模式
1 parent 8845a8f commit 07efe74

File tree

29 files changed

+486
-0
lines changed

29 files changed

+486
-0
lines changed

decorator/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

decorator/build.gradle

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
6+
7+
defaultConfig {
8+
applicationId "com.tast.decorator"
9+
minSdkVersion 15
10+
targetSdkVersion 25
11+
versionCode 1
12+
versionName "1.0"
13+
14+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15+
16+
}
17+
buildTypes {
18+
release {
19+
minifyEnabled false
20+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21+
}
22+
}
23+
}
24+
25+
dependencies {
26+
compile fileTree(dir: 'libs', include: ['*.jar'])
27+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
28+
exclude group: 'com.android.support', module: 'support-annotations'
29+
})
30+
compile 'com.android.support:appcompat-v7:25.2.0'
31+
testCompile 'junit:junit:4.12'
32+
compile project(path: ':toast')
33+
}

decorator/doc/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## 装饰模式 用星巴兹的例子来体现 ##
2+
介绍:装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
3+
4+
特点:—装饰对象和真实对象有相同的接口。这样客户端对象就能以和真实对象相同的方式和装饰对象交互。
5+
—装饰对象包含一个真实对象的引用(reference)
6+
—装饰对象接受所有来自客户端的请求。它把这些请求转发给真实的对象。
7+
—装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。在
8+
面向对象的设计中,通常是通过继承来实现对给定类的功能扩展。
9+
10+
应用场景:1、 需要扩展一个类的功能,或给一个类添加附加职责。
11+
2、 需要动态的给一个对象添加功能,这些功能可以再动态的撤销。
12+
3、 需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。
13+
4、 当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情
14+
况可能是因为类定义被隐藏,或类定义不能用于生成子类。
15+
16+
优缺点:优点:
17+
1. Decorator模式与继承关系的目的都是要扩展对象的功能,但是Decorator可以提供比继承更多的灵活性。
18+
2. 通过使用不同的具体装饰类以及这些装饰类的排列组合,设计师可以创造出很多不同行为的组合。
19+
20+
缺点:
21+
1. 这种比继承更加灵活机动的特性,也同时意味着更加多的复杂性。
22+
2. 装饰模式会导致设计中出现许多小类,如果过度使用,会使程序变得很复杂。
23+
3. 装饰模式是针对抽象组件(Component)类型编程。但是,如果你要针对具体组件编程时,就应该重新思考你的应用架构,以及装饰者是否合适。当然也可以改变Component接口,
24+
增加新的公开的行为,实现“半透明”的装饰者模式。在实际项目中要做出最佳选择。

decorator/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in D:\sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.tast.decorator;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.tast.decorator", appContext.getPackageName());
25+
}
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tast.decorator">
3+
4+
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
5+
android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
6+
<activity android:name=".MainActivity">
7+
<intent-filter>
8+
<action android:name="android.intent.action.MAIN" />
9+
10+
<category android:name="android.intent.category.LAUNCHER" />
11+
</intent-filter>
12+
</activity>
13+
</application>
14+
15+
</manifest>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.tast.decorator;
2+
3+
/**
4+
* 描述:抽象类
5+
*
6+
* @author wmm
7+
* @time 2017/8/22 10:39
8+
*/
9+
10+
public abstract class Beverage {
11+
12+
public String description = "Unknown Beverage";
13+
14+
public String getDescription() {
15+
return description;
16+
}
17+
18+
public abstract double cost();
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.tast.decorator;
2+
3+
/**
4+
* 描述:装饰者类
5+
*
6+
* @author wmm
7+
* @time 2017/8/22 10:47
8+
*/
9+
10+
public abstract class CondimentDecorator extends Beverage {
11+
public abstract String getDescription();
12+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.tast.decorator;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
import android.os.Bundle;
5+
6+
import com.design_patterns.toast.ToastUtils;
7+
import com.tast.decorator.coffee.DarkRoast;
8+
import com.tast.decorator.coffee.Espresso;
9+
import com.tast.decorator.coffee.HouseBlend;
10+
import com.tast.decorator.seasoning.Mocha;
11+
import com.tast.decorator.seasoning.Soy;
12+
import com.tast.decorator.seasoning.Whip;
13+
14+
/**
15+
* 装饰者模式(最好要配合“工厂”和“生成器”
16+
* 设计模式时将有更好的方式建立装饰者对象)
17+
*/
18+
public class MainActivity extends AppCompatActivity {
19+
20+
@Override
21+
protected void onCreate(Bundle savedInstanceState) {
22+
super.onCreate(savedInstanceState);
23+
setContentView(R.layout.activity_main);
24+
ToastUtils.init(this);
25+
26+
Beverage beverage = new Espresso();
27+
ToastUtils.showToasts(beverage.getDescription() + " ¥" + beverage.cost());
28+
29+
Beverage beverage2 = new DarkRoast();
30+
beverage2 = new Mocha(beverage2);//用Mocha装饰它
31+
beverage2 = new Mocha(beverage2);//用第二个Mocha装饰它
32+
beverage2 = new Whip(beverage2);//用Whip装饰它
33+
ToastUtils.showToasts(beverage2.getDescription() + " ¥" + beverage2.cost());
34+
35+
//最后再来一杯调料为豆浆、摩卡、奶泡的HouseBlend咖啡
36+
Beverage beverage3 = new HouseBlend();
37+
beverage3 = new Soy(beverage3);
38+
beverage3 = new Mocha(beverage3);
39+
beverage3 = new Whip(beverage3);
40+
ToastUtils.showToasts(beverage3.getDescription() + " ¥" + beverage3.cost());
41+
}
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.tast.decorator.coffee;
2+
3+
import com.tast.decorator.Beverage;
4+
5+
/**
6+
* 描述:一种咖啡类型
7+
*
8+
* @author wmm
9+
* @time 2017/8/22 10:49
10+
*/
11+
12+
public class DarkRoast extends Beverage {
13+
14+
public DarkRoast() {
15+
description = "Dark Roast Coffee";
16+
}
17+
18+
@Override
19+
public double cost() {
20+
return 2.89;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.tast.decorator.coffee;
2+
3+
import com.tast.decorator.Beverage;
4+
5+
/**
6+
* 描述:一种咖啡类型
7+
*
8+
* @author wmm
9+
* @time 2017/8/22 10:49
10+
*/
11+
12+
public class Decaf extends Beverage {
13+
14+
public Decaf() {
15+
description = "Decaf Coffee";
16+
}
17+
18+
@Override
19+
public double cost() {
20+
return 1.39;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.tast.decorator.coffee;
2+
3+
import com.tast.decorator.Beverage;
4+
5+
/**
6+
* 描述:一种咖啡类型
7+
*
8+
* @author wmm
9+
* @time 2017/8/22 10:49
10+
*/
11+
12+
public class Espresso extends Beverage {
13+
14+
public Espresso() {
15+
description = "Espresso";
16+
}
17+
18+
@Override
19+
public double cost() {
20+
return 1.99;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.tast.decorator.coffee;
2+
3+
import com.tast.decorator.Beverage;
4+
5+
/**
6+
* 描述:一种咖啡类型
7+
*
8+
* @author wmm
9+
* @time 2017/8/22 10:49
10+
*/
11+
12+
public class HouseBlend extends Beverage {
13+
14+
public HouseBlend() {
15+
description = "House Blend Coffee";
16+
}
17+
18+
@Override
19+
public double cost() {
20+
return 0.89;
21+
}
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.tast.decorator.seasoning;
2+
3+
import com.tast.decorator.Beverage;
4+
import com.tast.decorator.CondimentDecorator;
5+
6+
/**
7+
* 描述:一种咖啡调料
8+
*
9+
* @author wmm
10+
* @time 2017/8/22 11:00
11+
*/
12+
13+
public class Milk extends CondimentDecorator {
14+
15+
Beverage beverage;
16+
17+
public Milk(Beverage beverage) {
18+
this.beverage = beverage;
19+
}
20+
21+
/**
22+
* 不只是描述饮料(例如“DarkRoast”),
23+
* 而是能完整的连调料都描述出来(例如"“DarkRoast,Milk”)
24+
*/
25+
@Override
26+
public String getDescription() {
27+
return beverage.getDescription() + ", Milk";
28+
}
29+
30+
/**
31+
* 计算带Milk饮料的价钱
32+
*/
33+
@Override
34+
public double cost() {
35+
return 0.56 + beverage.cost();
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.tast.decorator.seasoning;
2+
3+
import com.tast.decorator.Beverage;
4+
import com.tast.decorator.CondimentDecorator;
5+
6+
/**
7+
* 描述:一种咖啡调料
8+
*
9+
* @author wmm
10+
* @time 2017/8/22 11:00
11+
*/
12+
13+
public class Mocha extends CondimentDecorator {
14+
15+
Beverage beverage;
16+
17+
public Mocha(Beverage beverage) {
18+
this.beverage = beverage;
19+
}
20+
21+
/**
22+
* 不只是描述饮料(例如“DarkRoast”),
23+
* 而是能完整的连调料都描述出来(例如"“DarkRoast,Mocha”)
24+
*/
25+
@Override
26+
public String getDescription() {
27+
return beverage.getDescription() + ", Mocha";
28+
}
29+
30+
/**
31+
* 计算带Mocha饮料的价钱
32+
*/
33+
@Override
34+
public double cost() {
35+
return 0.20 + beverage.cost();
36+
}
37+
}

0 commit comments

Comments
 (0)