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

» JWorld@TW » Software Design » GoF  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded 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
作者 Re:About Many States V.S Many Events [Re:Millerlai]
mue





發文: 1
積分: 0
於 2005-10-07 12:00 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
我覺得這個作法和Visitor很像,不曉得是不是可以說完全一樣?
看起來可以說State端就是Visitor,而Event accept state。
不過發動者卻又是從State端發動,我想這應該與原先的Visitor有些差別?

這裡稱為State的原因是,它實際上會因為Event而改變State。
即:
public void switchFire(FireSwitch sw, EventA e) {
//.............
sw.setState(new StateXXX());
}
所以對FireSwitch來說,它的內部state型態是abstract class(BaseState)。
而這個Event的產生對於FireSwitch來說,也只能知道其base class而已(此例甚至用interface就足夠)。

所以才會有這樣的解法。不曉得這樣是否妥當,又或有其他的patterns是較為合適的?


reply to postreply to post
作者 Re:About Many States V.S Many Events [Re:Millerlai]
ranchgirl





發文: 5
積分: 0
於 2005-12-21 19:00 user profilesend a private message to usersend email to ranchgirlreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Mediator is usually a better way to handle many-to-many relationships.

reply to postreply to post
作者 Re:About Many States V.S Many Events [Re:Millerlai]
robinjam





發文: 34
積分: 0
於 2006-03-26 01:12 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
用bridge pattern會比較好一點~~~

reply to postreply to post
» JWorld@TW »  Software Design » GoF

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8