在Android平台上来实践上述范例的架构,兹建立Android开发项目(Project)如下:
代码范例:Framework部分
// IPlugin.java接口
package Framework;
public interface IPlugin{
boolean execute(int code, int arg);
}
// Plugin.java类
package Framework;
public abstract class Plugin implements IPlugin{
@Override public boolean execute(int code, int arg) {
return onExecute(code, arg);
}
protected abstract boolean onExecute(int code, int arg);
}
代码范例:App部分
// UC1_Stub.java类
package Service;
import Framework.Plugin;
public abstract class UC1_Stub extends Plugin {
@Override protected boolean onExecute(int code, int arg) {
if(code == 0) play(arg); //播放目前音樂
else if(code == 1) stop(); //停止播放
return false;
}
public abstract void play(int arg);
public abstract void stop();
}
这曹操类的主要任务是:
- 实现了天子的接口,也就是onExecute()抽象函数。
- 提供了自己的新接口,也就是play()和stop()两个函数。
// UC1_Plugin.java类
package Service;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
public class UC1_Plugin extends UC1_Stub{
private MediaPlayer mPlayer = null;
private Context ctx;
public UC1_Plugin(Context context){ ctx = context; }
@Override public void play(int arg) {
if(mPlayer != null) return;
mPlayer = MediaPlayer.create(ctx, arg);
try { mPlayer.start();
} catch (Exception e) { Log.e("StartPlay", "error: " + e.getMessage(), e); }
}
@Override public void stop() {
if (mPlayer != null) {
mPlayer.stop(); mPlayer.release(); mPlayer = null;
}
}}
这个App类(及子民类)只需要实现曹操的接口就行了,也就是实现play()和stop()函数。
// MainActivity.java类
package com.example.cx01;
import Framework.IPlugin;
import Service.UC1_Plugin;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener {
private Button btn, btn2, btn3;
private IPlugin ip;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
btn = new Button(this); btn.setId(101);
btn.setBackgroundResource(R.drawable.paint);
btn.setText("play"); btn.setOnClickListener(this);
LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(150, 75);
param.topMargin = 10; param.leftMargin = 10;
layout.addView(btn, param);
btn2 = new Button(this); btn2.setId(102);
btn2.setBackgroundResource(R.drawable.heart);
btn2.setText("stop"); btn2.setOnClickListener(this);
layout.addView(btn2, param);
btn3 = new Button(this); btn3.setId(103);
btn3.setBackgroundResource(R.drawable.heart);
btn3.setText("exit"); btn3.setOnClickListener(this);
layout.addView(btn3, param);
setContentView(layout);
//-------------------------------
ip = new UC1_Plugin(this);
}
public void onClick(View v) {
switch(v.getId()){
case 101: play(); break;
case 102: stop(); break;
case 103: stop(); finish(); break;
}
}
public void play(){ ip.execute(0, R.raw.jp_song); }
public void stop(){ ip.execute(1, -1); }
}