您的位置:首页 > 移动开发 > Android开发

【转】UML建模與Android應用程式開發(下)

2011-06-02 08:02 267 查看
經過兩個觀點的互相核對與逐步修正後,的確呈現出極為完美的程式碼,如下:



Android應用程式Project



這包含了1個IA.java介面定義檔,及兩個應用子類定義檔:



應用程式碼

一致化的程式碼如下所示:



// IA.java接口



package com.misoo.pk01;

import android.os.Binder;

import android.os.IBinder;

import android.os.Parcel;

import android.os.RemoteException;



public interface IA {



int f1(int x)throws RemoteException;



public static abstract class Stub extends Binder implements IA {



@Override

public boolean onTransact(int code, Parcel data, Parcel reply, int flags)



throws android.os.RemoteException {

int x = data.readInt();

int y = this.f1(x);

reply.writeInt(y);

return true;

}



public abstract int f1(int x) throws RemoteException;



public static IA asInterface(IBinder obj){

return new Proxy(obj);

}



//---------------------------------------------



private static class Proxy implements IA{



private IBinder mRemote;



public Proxy(IBinder ibinder) {

mRemote = ibinder;

}



public int f1(int x) throws RemoteException {



// TODO Auto-generated method stub



Parcel data = Parcel.obtain();

data.writeInt(x);

Parcel reply = Parcel.obtain();

mRemote.transact(0, data, reply, 0);

return reply.readInt();

}

}

}

}



// myService.java



package com.misoo.pk01;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.os.RemoteException;



public class myService extends Service {



@Override

public IBinder onBind(Intent intent) {

return mBinder;

}



public int mySf1(int x){

return x + 1000;

}



//---------------------------------------------



private final IA.Stub mBinder = new IA.Stub() {



@Override

public int f1(int x) throws RemoteException {

return mySf1(x);

}

};

}



// myActivity.java



package com.misoo.pk01;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.content.ServiceConnection;

import android.graphics.Color;

import android.os.Bundle;

import android.os.IBinder;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;



public class myActivity extends Activity implements OnClickListener {



private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;

private final int FP = LinearLayout.LayoutParams.FILL_PARENT;



private Button btn, btn2;

private TextView tv;



private IA ia;

private int state_var_A = 0;



public void onCreate(Bundle icicle) {



super.onCreate(icicle);



if(state_var_A == 0){

show_layout_01();

goto_state_01();

}

}



private void show_layout_01(){

LinearLayout layout = new LinearLayout(this);

layout.setOrientation(LinearLayout.VERTICAL);

btn = new Button(this);

btn.setBackgroundResource(R.drawable.heart);



btn.setId(101);

btn.setText("Run");



btn.setOnClickListener(this);

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(120, 55);

param.topMargin = 10;

layout.addView(btn, param);



btn2 = new Button(this);

btn2.setBackgroundResource(R.drawable.heart);

btn2.setId(102);

btn2.setText("Exit");

btn2.setOnClickListener(this);

layout.addView(btn2, param);



tv = new TextView(this);

tv.setTextColor(Color.WHITE);

tv.setText("");

LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC);



param2.topMargin = 10;

layout.addView(tv, param2);

setContentView(layout);

}



private void goto_state_01(){

state_var_A = 1;

bindService(new Intent("com.misoo.pk01.REMOTE_SERVICE"), mConnection, Context.BIND_AUTO_CREATE);

}



private ServiceConnection mConnection = new ServiceConnection() {



public void onServiceConnected(ComponentName className, IBinder ibinder) {

ia = IA.Stub.asInterface(ibinder);

}



public void onServiceDisconnected(ComponentName className) { }

};





public void onClick(View v) {

int ret=0;

switch(v.getId()){

case 101:

if(state_var_A == 1){

try {

ret = ia.f1(188);

} catch (Exception e) {

e.printStackTrace();

}

tv.setText(String.valueOf(ret));

}

break;



case 102:

if(state_var_A == 1) {

stopService(new Intent("com.misoo.pk01.REMOTE_SERVICE"));

finish();

}

break;

}

}

}



結語

在傳統觀點裡,大多先繪製UML模型圖,然後才開始構思程式碼的撰寫,使得UML建模成為撰寫程式碼的前置工作,因此許多程式員將UML建模視為多餘的負擔。為了節省開發成本,就將省略掉UML建模的工作了。



在新潮的觀點裡,UML模型與程式碼是軟體系統本體的兩個觀點(或面向),兩者沒有先後順序關係,而是並存和兼具於同一個人的腦海裡。這就像兩隻眼睛看到的景象並存於一個人的腦海裡一般,如此才能看到更真實的世界,也能做出更完美的軟體系統來。從本文的範例,你可看到當UML模型與程式碼兩個觀點一致時,真的能讓軟體系統既可靠又高雅,不亦美哉!



原文链接:http://www.android1.net/Topic.aspx?BoardID=21&TopicID=1452
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: