Mediator Mode (mediator) (object behavior mode)

Source: Internet
Author: User
Tags abstract gety

Reference Documentation:

1. Design mode-the basis of reusable object-oriented software

2.http://blog.csdn.net/hguisu/article/details/7554612 (design mode (15) Mediator mode Mediator (object behavior type))


Mediator Mode (mediator) (object behavior mode)

Theoretical knowledge, you can refer to the reference document.

Look at the structure of the mediator:






Code implementation:

Dialogdirector.java:
Package com.rick.designpattern.mediator;

/**
 * Created by MyPC on 2017/6/23.
 */Public
abstract class Dialogdirector {public


    dialogdirector () {
    } public

    abstract void ShowDialog () ;

    public abstract void widgetchanged (widget widget);

    public abstract void Createwidgets ();
}
Fontdialogdirector.java:
Package com.rick.designpattern.mediator;
 /** * Created by MyPC on 2017/6/23.
    */public class Fontdialogdirector extends Dialogdirector {private Button _ok, _cancel;
    Private ListBox ListBox;

    Private Entryfield Entryfield;
        @Override public void Createwidgets () {_OK = new Button (this);
        _cancel = new Button (this);
        ListBox = new ListBox (this);
    Entryfield = new Entryfield (this);
    } @Override public void ShowDialog () {System.out.println ("Show dialog Box");
            } @Override public void widgetchanged (Widget thechangedwidget) {if (Thechangedwidget = = ListBox) {
        Entryfield.settext (Listbox.getselection ());
        } else if (Thechangedwidget = = _OK) {System.out.println ("submit");
        } else if (Thechangedwidget = = _cancel) {System.out.println ("Cancel dialog box");
    }} public Button Get_ok () {return _OK; } public void Set_ok (Button _ok) {this._oK = _OK;
    } public Button Get_cancel () {return _cancel;
    } public void Set_cancel (Button _cancel) {this._cancel = _cancel;
    } public ListBox Getlistbox () {return listbox;
    } public void Setlistbox (ListBox listbox) {This.listbox = listbox;
    } public Entryfield Getentryfield () {return entryfield;
    } public void Setentryfield (Entryfield entryfield) {This.entryfield = Entryfield;
 }
}
Widget.java:
Package com.rick.designpattern.mediator;

/**
 * Created by MyPC on 2017/6/23.
 */Public
abstract class Widget {
    private dialogdirector dialogdirector;

    Public Widget (Dialogdirector dialogdirector) {
        this.dialogdirector = dialogdirector;
    }

    public void Changed () {
        dialogdirector.widgetchanged (this);
    }

    public abstract void Handlemouse (MouseEvent MouseEvent);

}
Listbox.java:
package com.rick.designpattern.mediator;
Import java.util.ArrayList;

Import java.util.List;
 /** * Created by MyPC on 2017/6/23.

    */public class ListBox extends Widget {private list<string> listItems;

    private int position;
        Public ListBox (Dialogdirector dialogdirector) {super (dialogdirector);
        ListItems = new arraylist<string> ();
        Listitems.add ("1");
        Listitems.add ("2");
        Listitems.add ("3");
        Listitems.add ("4");
    Listitems.add ("5");
        Public String getselection () {if (null = = ListItems | | listitems.size () <= 0) {return "";
        } else {return listitems.get (position);
    }} public void setlist (list<string> listItems) {this.listitems = ListItems;
    } @Override public void Handlemouse (MouseEvent MouseEvent) {position = Mouseevent.gety (); }
}
Entryfield.java:
Package com.rick.designpattern.mediator;

/**
 * Created by MyPC on 2017/6/23.
 */Public
class Entryfield extends Widget {
    private String text;

    Public Entryfield (Dialogdirector dialogdirector) {
        super (dialogdirector);
    }

    public void SetText (String text) {
        this.text = text;
        System.out.println (text);
    }

    Public String GetText () {
        return text;
    }

    @Override public
    void Handlemouse (MouseEvent MouseEvent) {

    }
}
Button.java:
Package com.rick.designpattern.mediator;

/**
 * Created by MyPC on 2017/6/23.
 */Public
class Button extends Widget {
    private String buttontext;

    Public Button (Dialogdirector dialogdirector) {
        super (dialogdirector);
    }

    public void SetText (String text) {
        this.buttontext = text;
        System.out.println (text);
    }

    @Override public
    void Handlemouse (MouseEvent MouseEvent) {
        changed ();
    }
}
Mouseevent.java:
Package com.rick.designpattern.mediator;

/**
 * Created by MyPC on 2017/6/23.
 */Public
class MouseEvent {

    private int x, y;

    public int GetX () {
        return x;
    }

    public void SetX (int x) {
        this.x = x;
    }

    public int GetY () {
        return y;
    }

    public void sety (int y) {
        this.y = y;
    }
}
Client.java:
Package com.rick.designpattern.mediator;

/**
 * Created by MyPC on 2017/6/23.
 *
/public class Client {public


    static void Main (string[] args) {

        Fontdialogdirector fontdialogdirector = new Fontdialogdirector ();
        Fontdialogdirector.createwidgets ();
        Fontdialogdirector.showdialog ();

        Fontdialogdirector.widgetchanged (FONTDIALOGDIRECTOR.GET_OK ());
        Fontdialogdirector.widgetchanged (Fontdialogdirector.get_cancel ());
        Fontdialogdirector.widgetchanged (Fontdialogdirector.getlistbox ());
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.