JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Software Design » GoF  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 About Many States V.S Many Events
Millerlai





發文: 56
積分: 1
於 2005-10-06 21:55 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這兩天有一個問題想請教一下有經驗的人.想跟各位討論一下.

目前有許多states,而個別的states必須針對不同的 event做出對應的適當處理.

基於這種需求,不知道有沒有其它的Design Solutions?

以下的程式是目前某位朋友提出來的ㄧ種Design

大家參考一下這個程式碼,大概了解之後來討論一下還有怎樣的設計方式.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
abstract class BaseState {
    abstract void eventHandle ( EventA e);
    abstract void eventhandle ( EventB e);
    final void evenHandle(BaseEvent e){
         e.callback(this);
    }
}
 
class StateA extends BaseState{
     public void eventHandle(EventA e){
         System.out.println("StateA handle EventA");
     }
     public void eventhandle(EventB e){
         System.out.println("StateA handle EventB");
     }
}
 
class StateB extends BaseState{
    public void eventHandle(EventA e){
        System.out.println("StateB handle EventA");        
    }
    public void eventhandle(EventB e){
        System.out.println("StateB handle EventB");        
    }
}
 
abstract class BaseEvent{
    abstract void callback ( BaseState e);
}
 
class EventA extends BaseEvent{
    public void callback ( BaseState e ) { 
        e.eventHandle(this); 
    }
}
 
class EventB extends BaseEvent{
    public void callback ( BaseState e ) { 
        e.eventhandle(this); 
    }
}
 
public class TEST2 {
    public static void main(String[] args) {
        BaseState stateA = new StateA();
        BaseState stateB = new StateB();
 
        BaseEvent eventB = new EventA();
        BaseEvent eventA = new EventA();
        
        stateA.evenHandle(eventA);
        stateA.evenHandle(eventB);
        
        stateB.evenHandle(eventA);
        stateB.evenHandle(eventB);     
    }
}

附件是以上的原始碼.

以目前這段程式來看只有考慮到States V.S Events 的 2維 多對多關係.

還沒有考慮到: States V.S Events V.S Actions 的 3維 多對多關係.

如果對於3維多對多,有不錯的設計方法,也希望能夠請教一下各位.謝謝.

TEST2.java (1.4k)


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
3654 About Many States V.S Many Events Millerlai 1838 2005-10-06 21:55
2909 Re:About Many States V.S Many Events mue 453 2005-10-07 12:00
2759 Re:About Many States V.S Many Events ranchgirl 70 2005-12-21 19:00
2805 Re:About Many States V.S Many Events robinjam 24 2006-03-26 01:12
» JWorld@TW »  Software Design » GoF

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8