命令模式
定義:將“請(qǐng)求”封裝成對(duì)象,以便使用不同的請(qǐng)求、隊(duì)列或者日志來(lái)參數(shù)化其他對(duì)象。命令模式也可以支持撤銷(xiāo)的操作。
個(gè)人疑問(wèn):當(dāng)我第一眼看到這定義的時(shí)候當(dāng)時(shí)就TM懵逼了,心里有一萬(wàn)只草泥馬涌現(xiàn)出來(lái),不過(guò)后來(lái)通過(guò)仔細(xì)看書(shū)和看其他人的博客貌似是稍微懂了一點(diǎn),個(gè)人感覺(jué)這個(gè)命令模式實(shí)際上就相當(dāng)于去餐廳點(diǎn)餐,我們不需要去和廚師直接溝通也不需要和廚師見(jiàn)面甚至廚師是人還是機(jī)器人我們都不用管,我們需要做的就是叫服務(wù)員然后把想點(diǎn)的菜告訴服務(wù)員即可。這就相當(dāng)于命令模式的將“對(duì)象的請(qǐng)求者”和“對(duì)象的執(zhí)行者”解耦。
案例:餐廳點(diǎn)餐

餐廳案例(來(lái)自《head first設(shè)計(jì)模式》)
代碼實(shí)現(xiàn)(在Android中實(shí)現(xiàn),個(gè)人感覺(jué)能看到界面的變化比較舒服_)
1,定義廚師類(lèi)(案例中命令的執(zhí)行者)
public class CookerActivity extends AppCompatActivity {
ImageView mImageView;
private Customer customer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView=findViewById(R.id.img);
//服務(wù)員對(duì)象
WaiterDo waiterDo =new WaiterDo(this);
//顧客(命令發(fā)出者)
customer = new Customer(waiterDo);
}
//做美食
public void cook(){
//加載美食圖片
Glide.with(this).load("http://img4.imgtn.bdimg.com/it/u=2998362675,1571491295&fm=200&gp=0.jpg").into(mImageView);
}
//取消做美食
public void unCook(){
//去除美食圖片
Glide.with(this).load("").into(mImageView);
}
public void onClick(View view){
switch (view.getId()){
case R.id.order:
customer.order();
break;
case R.id.unorder:
customer.unOrder();
break;
}
}
}
布局代碼展示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.example.commandmodle.CookerActivity">
<LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:text="點(diǎn)餐"
android:id="@+id/order"
android:onClick="onClick"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:text="取消點(diǎn)餐"
android:id="@+id/unorder"
android:onClick="onClick"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:id="@+id/img"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_height="wrap_content" />
</LinearLayout>
2,定義服務(wù)員
(1)首先需要定義一個(gè)命令接口,里面含有execute()方法和unDo()方法分別代表執(zhí)行和撤銷(xiāo)。
public interface Command {
void excute();
void undo();
}
(2)定義服務(wù)員類(lèi)
public class WaiterDo implements Command {
private CookerActivity mCookerActivity;
public WaiterDo(CookerActivity mCookerActivity) {
this.mCookerActivity = mCookerActivity;
}
@Override
public void excute() {
//點(diǎn)餐
mCookerActivity.cook();
}
@Override
public void undo() {
//取消點(diǎn)餐
mCookerActivity.unCook();
}
}
3,定義點(diǎn)餐者(案例中命令的發(fā)出者)
public class Customer {
private Command mWaiterDo;
public Customer(WaiterDo mWaiterDo) {
this.mWaiterDo = mWaiterDo;
}
//點(diǎn)餐
public void order(){
mWaiterDo.excute();
}
//取消點(diǎn)餐
public void unOrder(){
mWaiterDo.undo();
}
}
4,效果展示

效果展示