// 引擎部份
package DS_Engine;
import DS_Framework.Count;
public class Calculator {
private Count ref;
public int run() {
int n;
n = getN();
int sum = 0;
for(int i=0; i<=n; i++) { sum += i; }
return sum;
}
private int getN() {
return ref.getCount();
}
public void setRef(Count co){
ref = co;
}
}
// DSF小框架部份
package DS_Framework;
public abstract class Count {
public int getCount() {
int cc = onCount();
return cc;
}
protected abstract int onCount();
}
// AP部分
// myCount.java
package com.misoo.ppxx;
import DS_Framework.Count;
public class myCount extends Count{
protected int onCount(){ return 10; }
}
// myActivity.java
package com.misoo.ppxx;
import DS_Engine.Calculator;
import DS_Framework.Count;
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 myActivity extends Activity implements
OnClickListener {
private Button btn, btn2;
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(100, 55);
param.leftMargin = 1; param.topMargin = 3;
btn = new Button(this);
btn.setId(101); btn.setText("Run");
btn.setOnClickListener(this); btn.setBackgroundResource(R.drawable.x_jude);
layout.addView(btn, param);
btn2 = new Button(this); btn2.setId(102);
btn2.setText("Exit"); btn2.setOnClickListener(this);
btn2.setBackgroundResource(R.drawable.x_sky);
layout.addView(btn2, param);
setContentView(layout);
}
public void onClick(View v) {
switch(v.getId()) {
case 101:
Calculator cal = new Calculator();
Count co = new myCount();
cal.setRef(co);
int result = cal.run();
setTitle(String.valueOf(result));
break;
case 102: finish(); break;
}}
} |