Mm cor

COR iAtft Chain Of Responsibility fiA'HAR®' "Cham"tL®ftAA-i ' -ffiS-ffiffi^-MA ' #OT"#S#WResponsibility" ° AftA

ABAA^A'MMAMM'Affllti* S^Mfr-AMM^AM fr's^^MM* S^AMfr °A-^AMMffi'AAMAMMM' M^AHAMMtAAMflAMmM =

AMA® ' «-MKACORiA ' fMMfrAi^A ° A^AW m ' AmM^ifrfAWMMfrffira-iMCham) = ^^«M^A(^getPower) ' ' ^AA« ' MfAAA«

MAAgAMffAiitlAAfgAMMMfr ° '

AAfS^Chain Of Responsibility ° $PAH:

^^ffiMffi ' BM COR fi^XKS ac01 * Activity_1 * Activity_2 ^ Activity_3

' Task1 * Task2 ^ Task3 ' S Task1 f^fW^ startActivity()

Activity_1 ^^Wffl^ Intent l&Sffi Activity = fcfaMM ' S TaskN

^ startActivityOX^m Activity_N ^^Wffl^ Intent ^Sffi Activity °

mn^m Task1 * Task2 Activity_1 * Activity_2

g^AMMM^ ' AMWffl^ Intent ft Activity ° ©M&M—

B-<3 sic

- 0 com.misoci.kxD9

1+

^ acOl.java

1+

^ Activity_l .java

1+

^ Activity_2.java

1+

^ Activity_3.java

1+

^ Factoiy.java

1+

[JJ R.java

1+

^ SuperTask.java

1+

^ Taskl.java

1+

^ Task2.java

1+

^ Task3.java

+ s, Android Library

+ s, Android Library

package com.misoo.kx09; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem;

public class ac01 extends Activity {

public static final int VIEW ID = Menu.FIRST; public static final int EDIT ID = Menu.FIRST + 1; public static final int PICKID = Menu.FIRST + 2; public static final int EXITID = Menu.FIRST + 3;

private Factory fac; private SuperTask head; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout./wain); fac = new Factory(this); head = fac.get_task_list();

@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);

menu.add(0, VIEWID, 0, "View"); menu.add(0, EDITID, 1, "Edit"); menu.add(0, PICKID, 2, "Pick"); menu.add(0, EXITID, 3, "Exit"); return true;

@Override public boolean onOptionsItemSelected(MenuItem item) { boolean ret;

switch (item.getItemId()) {

case VIEWID: ret = head.pass("VIEW"); if(!ret) setTitle("false"); break;

case EDITID: ret = head.pass("EDIT"); if(!ret) setTitle("false"); break;

case PICKID: ret = head.pass("PICK"); if(!ret) setTitle("false"); break;

return super.onOptionsItemSelected(item);

Step-3: MMA^ : SuperTask > SMAMA : // — SuperTask.java MA® —

package com.misoo.kx09; public abstract class SuperTask {

protected SuperTask successor;

abstract boolean pass(String job);

public void add(SuperTask nx){ successor = nx; }

// — Facrory.java fMAfif — package com.misoo.kx09; import android.content.Context;

public class Factory {

private SuperTask head; Factory(Context ctx) {

SuperTask get_task_list(){ return head; }

// — Taskl.java fMAfif — package com.misoo.kx09; import android.content.Context; import android.content.Intent;

public class Taskl extends SuperTask { private Context ctx; Taskl (Context tx){ ctx = tx; } public boolean pass(String job) {

if(job == "VIEW"){ this.perform(); return true; } else { if(successor == null) return false; else return successor.pass(job);

private void perform(){

// Intent intent = new Intent(Intent.VIEW_ACTION, null);

Intent intent = new Intent(ctx, Activity_1.class); ctx.startActivity(intent);

// — Task2.java fMAfif — package com.misoo.kx09; import android.content.Context; import android.content.Intent;

public class Task2 extends SuperTask { private Context ctx; Task2(Context tx){ ctx = tx; }

public boolean pass(String job) {

if(job == "EDIT") { this.perform(); return true; } else { if(successor == null) return false; else return successor.pass(job);

private void perform(){

//Intent intent = new Intent(Intent.EDIT_ACTION, null); Intent intent = new Intent(ctx, Activity_2. class); ctx.startActivity(intent);

// — Task3.java MA® — package com.misoo.kx09; import android.content.Context; import android.content.Intent;

public class Task3 extends SuperTask { private Context ctx; Task3 (Context tx){ ctx = tx; } public boolean pass(String job) {

if(job == "PICK") { this.perform(); return true; } else { if(successor == null) return false; else return successor.pass(job);

private void perform(){ // Intent intent = new Intent(Intent.PICK_ ACTION, null); Intent intent = new Intent(ctx, Activity_3. class); ctx.startActivity(intent);

package com.misoo.kx09;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class Activity_1 extends Activity implements OnClickListener { @Override public void onCreate(Bundle icicle) {

super.onCreate(icicle); setContentView(R. layout. act); Button btn = (Button)findViewById(R.id.£tn); btn. setBackgroundResource(R. drawable .xjude); btn.setText("Exit from action.VIEW"); btn.setOnClickListener(this); } public void onClick(View arg0) { finish(); }

Step-9: MM Activity AA^A : Activity_2 > AfMA®$PA :

package com.misoo.kx09;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class Activity_2 extends Activity implements OnClickListener { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R. layout. act); Button btn = (Button)findViewById(R.id.6tn);

btn.setBackgroundResource(R.drawable.x_Wue); btn.setText("Exit from action.EDIT");

btn.setOnClickListener(this); } public void onClick(View arg0) { finish(); }

Step-10: MM Activity AA^A : Activity_3 ' AfMA®$PA :

package com.misoo.kx09;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class Activity_3 extends Activity implements OnClickListener { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.act); Button btn = (Button)findViewById(R.id.£tn);

btn.setBackgroundResource(R.drawable.x_5^y); btn.setText("Exit from action.PICK");

btn.setOnClickListener(this); } public void onClick(View arg0) { finish(); }

1. ^ Gamma ft"Design Patterns" COR(Chain Of Responsibility)!!

MMfMA^f^TOHMISffft °

public boolean pass(String job) { if(job == "VIEW"){

this.perform(); return true; }

if(successor == null) return false; else return successor.pass(job);

^AMitAftjobil"VIEW"B# ' MmftgBft*ffi(Responsibity) ' ^ ®WgBftperform()A^iTfi^ ' MMActivity_i ° MMitAftjob ii^l "VIEW" ' ^ffi job mm^nmwm (Successor) = -fe^ftW successor.pass(job) ° ^—^xWMIl#(fiPsuccessor == null)^®f$false ' ^A'/x mm^mm^mnmjob °

private void perform(){

// Intent intent = new Intent(Intent.VIEW_ACTION, null); Intent intent = new Intent(ctx, Activity_1. class); ctx. startActivity(intent);

S^mMM^ Intent ft Activity > ^S^bA^M Activity_3 =

' AMMAJSAAA ° HAAMA lAAP^S ' MAAW^ * A^A ° M^AfiA ' SMME ' MA^mA^ = itAIS^ ' fiAAMTOfi#AiI»ontext)AlfAASAP^S ' ^

is^A^AfiM^AAM(Soiution) = mm' row« * MWAti * aaw®

° fiAAMAMA * * AAAA «SA ' M#^APAAAA*(Solution) = * WSP

AA* ° AAEEAEAMAA(Christopher Alexander) ftAmAAA®:

r#^(Pattern)^^^^^^(Context) T ' ## ^M(Problem) &Mfffl«^(Solution) = j

° A^WM ' ALtWAAAfiAfiAAS ° m m ' ' WAMAAAfiA * »HA * AA»l£lffiA(Design

Pattern)^ = 1991 AAA ' AMAAAfiAffl^MMAMAISIfA '

AISffMfMS ' AtÄ^AAS^AMA ' AgAHAffiAMAAfg tSffiffi^IMI^WiASi°OTAKMfiAAAMA^AünAilAn

1964 A ' Christopher Alexander SA ' MIS

MffiiBSASAB^M riAj (Pattern) > iAf^iiAil^Itmiffi ' AfftiSffii^AIi ° ^T 1970 AA ' Alexander ffi&AM 'AffiAAAP ' ffiTO^^W^ Pattern SA °

1980 Aftffl ' ' mm Alexander lAiASA

AS^W^P^^AAA ° 1987 A ' Ward Cunningham ffl Kent Beck

- A 1995 A ' Erich Gamma ffi Ralph

Johnson -Design Patterns:Elements of Reusable Object-Oriented

MMMM ° Gamma ^"Design Pattern"-*AAAM ' «aM^Wla®

0 0

Post a comment