CSharp: Mediator Pattern
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | /// <summary> /// Summary description for Rectangle. /// Mediator Pattern中介者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class Rectangle { /// <summary> /// /// </summary> private int xp, yp, wr, hr; /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="w"></param> /// <param name="h"></param> public Rectangle( int x, int y, int w, int h) { xp = x; yp = y; wr = w; hr = h; } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="w"></param> /// <param name="h"></param> public Rectangle( float x, float y, float w, float h) { xp = ( int )x; yp = ( int )y; wr = ( int )w; hr = ( int )h; } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public bool contains( int x, int y) { bool cn = xp <= x && x <= xp + wr; cn = cn && yp <= y && y <= yp + hr; return cn; } /// <summary> /// /// </summary> public int x { get { return xp; } set { xp = value; } } /// <summary> /// /// </summary> public int y { get { return yp; } set { yp = value; } } /// <summary> /// /// </summary> public int w { get { return wr; } set { wr = value; } } /// <summary> /// /// </summary> public int h { get { return hr; } set { hr = value; } } } |
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 | /// <summary> /// Summary description for Memento. /// Mediator Pattern中介者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class Memento { private int x, y, w, h; /// <summary> /// /// </summary> private Rectangle rect; /// <summary> /// /// </summary> private VisRectangle visRect; /// <summary> /// /// </summary> /// <param name="vrect"></param> public Memento(VisRectangle vrect) { visRect = vrect; rect = visRect.rects; x = rect.x; y = rect.y; w = rect.w; h = rect.h; } /// <summary> /// /// </summary> public void restore() { rect.x = x; rect.y = y; rect.h = h; rect.w = w; visRect.rects = rect; } } |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | /// <summary> /// Summary description for VisRectangle.' /// Mediator Pattern中介者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class VisRectangle { private int x, y, w, h; private const int VSIZE = 30; private const int HSIZE = 50; private Rectangle rect; private bool selected; private Pen bPen; private SolidBrush bBrush; //、 public VisRectangle( int xp, int yp) { x = xp; y = yp; w = HSIZE; h = VSIZE; saveAsRect(); bPen = new Pen(Color.Black); bBrush = new SolidBrush(Color.Black); } /// <summary> /// used by Memento for saving and restoring state /// </summary> internal Rectangle rects { get { return rect; } set { x = value.x; y = value.y; w = value.w; h = value.h; saveAsRect(); } } /// <summary> /// /// </summary> /// <param name="b"></param> public void setSelected( bool b) { selected = b; } /// <summary> /// move to new position /// </summary> /// <param name="xp"></param> /// <param name="yp"></param> public void move( int xp, int yp) { x = xp; y = yp; saveAsRect(); } /// <summary> /// /// </summary> /// <param name="g"></param> public void draw(Graphics g) { //draw rectangle g.DrawRectangle(bPen, x, y, w, h); if (selected) { //draw handles g.FillRectangle(bBrush, x + w / 2, y - 2, 4, 4); g.FillRectangle(bBrush, x - 2, y + h / 2, 4, 4); g.FillRectangle(bBrush, x + (w / 2), y + h - 2, 4, 4); g.FillRectangle(bBrush, x + (w - 2), y + (h / 2), 4, 4); } } /// <summary> /// return whether point is inside rectangle /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public bool contains( int x, int y) { return rect.contains(x, y); } /// <summary> /// create Rectangle object from new position /// </summary> private void saveAsRect() { rect = new Rectangle(x, y, w, h); } } |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | /// <summary> /// Mediates events between buttonsb /// Mediator Pattern中介者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class Mediator { private bool startRect; private bool rectSelected; private ArrayList drawings; private PictureBox canvas; private int selectedIndex; private CareTaker caretakr; private RectButton rect; private VisRectangle v; private VisRectangle[] draw_ings; //used only to make clearer UML diagram /// <summary> /// /// </summary> /// <param name="p"></param> public Mediator(PictureBox p) { startRect = false ; rectSelected = false ; drawings = new ArrayList(); caretakr = new CareTaker(drawings); canvas = p; } /// <summary> /// /// </summary> public void startRectangle() { startRect = true ; } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void createRect( int x, int y) { unpick(); //make sure no rectangle is selected if (startRect) { //if rect button is depressed int count = drawings.Count; caretakr.Add(count); //Save previous drawing list size v = new VisRectangle(x, y); //create a rectangle drawings.Add(v); //add new element to list startRect = false ; //done with this rectangle rect.setSelected( false ); //unclick button canvas.Refresh(); } else pickRect(x, y); //if not pressed look for rect to select } /// <summary> /// /// </summary> /// <param name="rb"></param> public void registerRectButton(RectButton rb) { rect = rb; } /// <summary> /// /// </summary> public void unpick() { if (rectSelected && (selectedIndex >= 0) && (selectedIndex < drawings.Count)) { VisRectangle vis = (VisRectangle)drawings[selectedIndex]; vis.setSelected( false ); selectedIndex = -1; rectSelected = false ; canvas.Refresh(); } } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void pickRect( int x, int y) { //save current selected rectangle //to avoid double save of undo int lastPick = -1; if (selectedIndex >= 0) { lastPick = selectedIndex; } unpick(); //undo any selection //see if one is being selected for ( int i = 0; i < drawings.Count; i++) { v = (VisRectangle)drawings[i]; if (v.contains(x, y)) { //did click inside a rectangle selectedIndex = i; //save it rectSelected = true ; if (selectedIndex != lastPick) { //but don't save twice caretakr.rememberPosition(v); } v.setSelected( true ); //turn on handles repaint(); //and redraw } } } /// <summary> /// /// </summary> public void clear() { drawings = new ArrayList(); caretakr.clear(drawings); rectSelected = false ; selectedIndex = 0; repaint(); } /// <summary> /// /// </summary> private void repaint() { canvas.Refresh(); } /// <summary> /// /// </summary> public void undo() { caretakr.undo(); repaint(); } /// <summary> /// /// </summary> /// <param name="g"></param> public void reDraw(Graphics g) { for ( int i = 0; i < drawings.Count; i++) { VisRectangle v = (VisRectangle)drawings[i]; v.draw(g); } } /// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void drag( int x, int y) { if (rectSelected) { VisRectangle v = (VisRectangle)drawings[selectedIndex]; if (v.contains(x, y)) { v.move(x, y); repaint(); } } } } |
1 2 3 4 5 6 7 8 9 10 | /// <summary> /// Command interface /// Mediator Pattern中介者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public interface Command { void Execute(); } |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | /// <summary> /// Summary description for CareTaker. /// Mediator Pattern中介者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class CareTaker { private ArrayList drawings, undoList; private Memento mem; private VisRectangle[] draw_ings; //used only to make UML clearer /// <summary> /// /// </summary> /// <param name="dcol"></param> public CareTaker(ArrayList dcol) { clear(dcol); } /// <summary> /// /// </summary> /// <param name="vr"></param> public void rememberPosition(VisRectangle vr) { mem = new Memento(vr); undoList.Add(mem); } /// <summary> /// /// </summary> /// <param name="drw"></param> public void clear(ArrayList drw) { drawings = drw; undoList = new ArrayList(); } /// <summary> /// /// </summary> /// <param name="intg"></param> public void Add( int intg) { undoList.Add(intg); } /// <summary> /// /// </summary> public void removeDrawing() { drawings.RemoveAt(drawings.Count - 1); } /// <summary> /// /// </summary> /// <param name="mem"></param> public void remove(Memento mem) { mem.restore(); } /// <summary> /// /// </summary> /// <param name="intg"></param> public void remove( int intg) { removeDrawing(); } /// <summary> /// /// </summary> public void undo() { if (undoList.Count > 0) { int last = undoList.Count - 1; object obj = undoList[last]; try { Memento mem = (Memento)obj; remove(mem); } catch (Exception) { removeDrawing(); } undoList.RemoveAt(last); } } } |
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 | /// <summary> /// Mediator Pattern中介者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class ClrButton : System.Windows.Forms.Button, Command { /// <summary> /// /// </summary> private Mediator med; /// <summary> /// /// </summary> /// <param name="md"></param> public ClrButton(Mediator md) { InitializeComponent(); med = md; } /// <summary> /// /// </summary> public void Execute() { med.clear(); } /// <summary> /// /// </summary> /// <param name="container"></param> public ClrButton(IContainer container) { container.Add( this ); InitializeComponent(); } } |
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 | /// <summary> /// Mediator Pattern中介者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class RectButton : System.Windows.Forms.ToolBarButton, Command { private ToolBarButton bt; private Mediator med; /// <summary> /// /// </summary> /// <param name="md"></param> /// <param name="tb"></param> public RectButton(Mediator md, ToolBarButton tb) { InitializeComponent(); med = md; bt = tb; } /// <summary> /// /// </summary> /// <param name="sel"></param> public void setSelected( bool sel) { bt.Pushed = sel; } /// <summary> /// /// </summary> public void Execute() { if (bt.Pushed) med.startRectangle(); } /// <summary> /// /// </summary> /// <param name="container"></param> public RectButton(IContainer container) { container.Add( this ); InitializeComponent(); } } |
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 | /// <summary> /// Mediator Pattern中介者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class UndoButton : System.Windows.Forms.ToolBarButton, Command { private Mediator med; private ToolBarButton ubutton; /// <summary> /// /// </summary> /// <param name="md"></param> /// <param name="but"></param> public UndoButton(Mediator md, ToolBarButton but) { InitializeComponent(); med = md; ubutton = but; } /// <summary> /// /// </summary> public void Execute() { med.undo(); } /// <summary> /// /// </summary> /// <param name="container"></param> public UndoButton(IContainer container) { container.Add( this ); InitializeComponent(); } } |
调用:
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | /// <summary> /// Mediator Pattern /// Mediator Pattern中介者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class MediatorPatternForm : Form { private bool mouse_down; private Mediator med; private Hashtable commands; //----- private void init() { med = new Mediator(pic); //create Mediator commands = new Hashtable(); //and Hash table //create the command objectsb RectButton rbutn = new RectButton(med, tbar.Buttons[0]); UndoButton ubutn = new UndoButton(med, tbar.Buttons[1]); ClrButton clrbutn = new ClrButton(med); med.registerRectButton(rbutn); //add them to the hashtable using the button hash values commands.Add(btRect.GetHashCode(), rbutn); commands.Add(btUndo.GetHashCode(), ubutn); commands.Add(btClear.GetHashCode(), clrbutn); pic.Paint += new PaintEventHandler(paintHandler); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void paintHandler( object sender, PaintEventArgs e) { Graphics g = e.Graphics; med.reDraw(g); } /// <summary> /// /// </summary> public MediatorPatternForm() { InitializeComponent(); init(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MediatorPatternForm_Load( object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tbar_ButtonClick( object sender, ToolBarButtonClickEventArgs e) { ToolBarButton tbutn = e.Button; Command comd = (Command)commands[tbutn.GetHashCode()]; comd.Execute(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pic_MouseDown( object sender, MouseEventArgs e) { mouse_down = true ; med.createRect(e.X, e.Y); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pic_MouseMove( object sender, MouseEventArgs e) { if (mouse_down) med.drag(e.X, e.Y); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pic_MouseUp( object sender, MouseEventArgs e) { mouse_down = false ; } } |
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!