<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-95  評(píng)論-31  文章-10  trackbacks-0
    一個(gè)樹分為:根節(jié)點(diǎn),樹枝,樹葉,點(diǎn)擊根節(jié)點(diǎn)觸發(fā)一個(gè)事件:展開所有樹枝;點(diǎn)擊樹枝觸發(fā)一個(gè)事件:展開所有樹葉;點(diǎn)擊樹葉觸發(fā)一個(gè)事件:屏幕右方顯示詳細(xì)信息。那么即可說明:樹是一個(gè)組件即被觀察者,事件對(duì)象即觀察者,只要事件源即用戶點(diǎn)擊操作發(fā)生,就通知事件對(duì)象觀察者做出相應(yīng)的動(dòng)作,首先組裝出樹結(jié)構(gòu),采用安全的合成模式,類示意圖如下:

    如果對(duì)樹組件增加監(jiān)聽事件,即增加觀察者ActionListener,那么綜合觀察者模式,增加鼠標(biāo)和鍵盤監(jiān)聽,第二版類圖如下:



    先給出代碼:
    package observer;

    import java.util.ArrayDeque;
    import java.util.Collection;
    import java.util.Iterator;

    public abstract class Component
    {
        
    // 監(jiān)聽隊(duì)列
        private static ArrayDeque<ActionListener> deque = new ArrayDeque<ActionListener>();

        
    private String componentName;

        
    public Component(String componentName)
        {
            
    this.componentName = componentName;
        }

        
    public String getComponentName()
        {
            
    return componentName;
        }

        
    public void setComponentName(String componentName)
        {
            
    this.componentName = componentName;
        }

        
    protected abstract Component getComponent();

        
    protected void addActionListener(ActionListener actionListener) throws NullPointerException
        {
            
    if (actionListener == null)
                
    throw new NullPointerException("the ActionListener is null");
            deque.offerLast(actionListener);
        }

        
    protected void removeActionListener(ActionListener actionLister)
        {
            deque.remove(actionLister);
        }

        
    protected Collection<ActionListener> getActionListeners()
        {
            
    return deque;
        }
        
        
    /**
         *  點(diǎn)擊組件事件
         
    */
        
    protected void clickOperation()
        {
            
    for (Iterator<ActionListener> it = deque.iterator(); it.hasNext();)
            {
                ActionListener listener 
    = it.next();
                
    if (listener instanceof MouseActionListener)
                {
                    ((MouseActionListener) listener).clickEvent(
    this);
                }
                
    if (listener instanceof KeyBoardActionListener)
                {
                    ((KeyBoardActionListener) listener).keyPressEvent(
    this);
                }
            }
        }

        
    /**
         * 雙擊組件事件
         
    */
        
    protected void doubleClickOperation()
        {
            
    for (Iterator<ActionListener> it = deque.iterator(); it.hasNext();)
            {
                ActionListener listener 
    = it.next();
                
    if (listener instanceof MouseActionListener)
                {
                    ((MouseActionListener) listener).doubleClickEvent(
    this);
                }
                
    if (listener instanceof KeyBoardActionListener)
                {
                    ((KeyBoardActionListener) listener).keyPressEvent(
    this);
                }
            }
        }

        
    /**
         * 拖拽組件事件
         
    */
        
    protected void dragOperation()
        {
            
    for (Iterator<ActionListener> it = deque.iterator(); it.hasNext();)
            {
                ActionListener listener 
    = it.next();
                
    if (listener instanceof MouseActionListener)
                {
                    ((MouseActionListener) listener).dragEvent(
    this);
                }
                
    if (listener instanceof KeyBoardActionListener)
                {
                    ((KeyBoardActionListener) listener).keyReleaseEvent(
    this);
                }
            }
        }
        
        
    /**
         * 組合模式葉子和樹枝通用方法,遍歷時(shí)候,可使葉子和樹枝相同對(duì)待
         
    */
        
    protected abstract void operation();

    }

    package observer;

    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;

    public class Branch extends Component
    {

        
    public Branch(String branchName)
        {
            
    super(branchName);
        }

        
    private ArrayList<Component> components = new ArrayList<Component>();

        
    public void addComponent(Component component)
        {
            
    this.components.add(component);
        }

        
    public void removeComponent(Component component)
        {
            
    this.components.remove(component);
        }

        
    public Collection<Component> getComponents()
        {
            
    return components;
        }

        @Override
        
    protected Component getComponent()
        {
            
    return this;
        }

        
    public void spreadLeaf()
        {
            System.out.println(
    "樹枝:" + getComponentName() + "展開節(jié)點(diǎn)");
        }

        @Override
        
    protected void operation()
        {
            
    for (Iterator<Component> it =  getComponents().iterator(); it.hasNext();)
            {    
                Component component 
    = it.next();
                System.out.println(
    "當(dāng)前節(jié)點(diǎn):"+component.getComponentName());
                component.operation();
            }
        }
    }

    package observer;

    public class Leaf extends Component
    {

        
    public Leaf(String leafName)
        {
            
    super(leafName);
        }

        @Override
        
    protected Component getComponent()
        {
            
    return this;
        }

        
    public void clickLeaf()
        {
            System.out.println(
    "點(diǎn)擊了" + getComponentName() );
        }

        @Override
        
    protected void operation()
        {
            System.out.println(
    "leafName:" + getComponentName() + " 節(jié)點(diǎn)");
        }

    }

    package observer;

    public interface ActionListener
    {
        
    void actionPerformer(Component component);
    }

    package observer;

    public interface MouseActionListener extends ActionListener
    {
        
    void clickEvent(Component component);

        
    void doubleClickEvent(Component component);

        
    void dragEvent(Component component);

    }

    package observer;

    public class MouseActionListenerAdapter implements MouseActionListener
    {

        @Override
        
    public void actionPerformer(Component component)
        {
            
    // TODO Auto-generated method stub

        }

        
    /** 增加一個(gè)默認(rèn)單擊事件實(shí)現(xiàn) */
        @Override
        
    public void clickEvent(Component component)
        {
            
    if (component instanceof Branch)
            {
                ((Branch) component).spreadLeaf();
            }
            
    if (component instanceof Leaf)
            {
                ((Leaf) component).clickLeaf();
            }
        }

        @Override
        
    public void doubleClickEvent(Component component)
        {
            
    // TODO Auto-generated method stub

        }

        @Override
        
    public void dragEvent(Component component)
        {
            
    // TODO Auto-generated method stub

        }
    }

    package observer;

    public interface KeyBoardActionListener extends ActionListener
    {
        
    void keyPressEvent(Component component);

        
    void keyReleaseEvent(Component component);
    }

    package observer;

    public class KeyBoardActionListenerAdapter implements KeyBoardActionListener
    {

        @Override
        
    public void actionPerformer(Component component)
        {
            
    // TODO Auto-generated method stub
            
        }

        @Override
        
    public void keyPressEvent(Component component)
        {
            
    // TODO Auto-generated method stub
            
        }

        @Override
        
    public void keyReleaseEvent(Component component)
        {
            
    // TODO Auto-generated method stub
            
        }
        
    }

    package observer;

    public class Client
    {
        
    public static void main(String[] args)
            {
                
    //根節(jié)點(diǎn)
                Branch root = new Branch("根節(jié)點(diǎn)");
                
    //葉子節(jié)點(diǎn)
                Leaf leaf1 = new Leaf("葉子1");
                
    //增加葉子1監(jiān)聽事件
                leaf1.addActionListener(new MouseActionListenerAdapter());
                
    //樹枝節(jié)點(diǎn)
                Branch branch1 = new Branch("樹枝1");
                
    //增加樹枝1監(jiān)聽事件
                branch1.addActionListener(new MouseActionListenerAdapter());
                
    //給樹枝1再增加一個(gè)葉子和一個(gè)樹枝
                Leaf leaf2 = new Leaf("葉子2");
                
    //leaf2增加監(jiān)聽事件
                leaf2.addActionListener(new MouseActionListenerAdapter());
                
                Branch branch2 
    = new Branch("樹枝1-1");
                Leaf leaf3 
    = new Leaf("葉子3");
                
    //增加監(jiān)聽事件
                leaf3.addActionListener(new MouseActionListenerAdapter());
                branch2.addComponent(leaf3);
                    
                
    //添加到樹枝1 下面
                branch1.addComponent(leaf2);
                branch1.addComponent(branch2);
                
    //添加到根節(jié)點(diǎn)下面
                root.addComponent(leaf1);
                root.addComponent(branch1);
                
    //先調(diào)用一次遍歷
                root.operation();
                
    //然后 觸發(fā)點(diǎn)擊葉子3事件
                System.out.println("開始觸發(fā)點(diǎn)擊事件");
                
    //葉子3事件連續(xù)觸發(fā)了4次,這就是事件冒泡原理,因?yàn)樯厦鎸?duì)Component組件加了4個(gè)不同監(jiān)聽
                
    //其他組件也捕獲了這次事件,如果阻止冒泡,那么獲取該事件,做一次判斷即可
                leaf3.clickOperation();
                
            }
    }
    輸出結(jié)果:
    當(dāng)前節(jié)點(diǎn):葉子1
    leafName:葉子1 節(jié)點(diǎn)
    當(dāng)前節(jié)點(diǎn):樹枝1
    當(dāng)前節(jié)點(diǎn):葉子2
    leafName:葉子2 節(jié)點(diǎn)
    當(dāng)前節(jié)點(diǎn):樹枝1-1
    當(dāng)前節(jié)點(diǎn):葉子3
    leafName:葉子3 節(jié)點(diǎn)
    ......開始觸發(fā)點(diǎn)擊事件......
    點(diǎn)擊了葉子3
    點(diǎn)擊了葉子3
    點(diǎn)擊了葉子3
    點(diǎn)擊了葉子3

    主站蜘蛛池模板: 成全动漫视频在线观看免费高清版下载| 无码av免费毛片一区二区| 国产免费黄色大片| 亚洲国产一成久久精品国产成人综合| 亚洲美女精品视频| 一级毛片全部免费播放| 亚洲视频免费在线观看| 每天更新的免费av片在线观看| 久久久久亚洲AV成人无码网站| 今天免费中文字幕视频| 亚洲综合视频在线| 亚洲第一网站免费视频| 亚洲第一成年网站大全亚洲| 91老湿机福利免费体验| 亚洲精品亚洲人成在线播放| 巨胸喷奶水视频www网免费| 亚洲av无码一区二区三区天堂| 免费一级毛片在播放视频| h片在线播放免费高清 | 在线看片无码永久免费aⅴ| 国产成人亚洲午夜电影| 国产成人精品日本亚洲专区61 | 无码日韩人妻AV一区免费l| 国产亚洲一区区二区在线| 久久久久久久岛国免费播放| 亚洲免费二区三区| 国产片免费在线观看| 亚洲国产免费综合| 亚洲综合久久综合激情久久| 成人免费无码大片a毛片| 男女交性无遮挡免费视频| 亚洲va中文字幕无码久久| 精品无码免费专区毛片| 亚洲av无码日韩av无码网站冲| 77777亚洲午夜久久多人| 久久精品国产免费观看三人同眠| 亚洲AV永久无码精品放毛片| 久久精品九九亚洲精品天堂| 久久久久国产精品免费免费搜索| 少妇亚洲免费精品| 亚洲欧洲日产韩国在线|