csharp;gutter:true;
///
/// Summary description for Rectangle.</p>
<pre><code>/// Mediator Pattern中介者模式
///20220918
/// geovindu,Geovin Du,涂聚文
///
public class Rectangle
{
///
///
///
private int xp, yp, wr, hr;
///
///
///
///
///
///
///
public Rectangle(int x, int y, int w, int h)
{
xp = x;
yp = y;
wr = w;
hr = h;
}
///
///
///
///
///
///
///
public Rectangle(float x, float y, float w, float h)
{
xp = (int)x;
yp = (int)y;
wr = (int)w;
hr = (int)h;
}
///
///
///
///
///
///
public bool contains(int x, int y)
{
bool cn = xp
///
///
public int x
{
get
{
return xp;
}
set
{
xp = value;
}
}
///
///
///
public int y
{
get
{
return yp;
}
set
{
yp = value;
}
}
///
///
///
public int w
{
get
{
return wr;
}
set
{
wr = value;
}
}
///
///
///
public int h
{
get
{
return hr;
}
set
{
hr = value;
}
}
}
</code></pre>
<pre><code>
;gutter:true;
///
/// Summary description for Memento.
/// Mediator Pattern中介者模式
///20220918
/// geovindu,Geovin Du,涂聚文
///
public class Memento
{
private int x, y, w, h;
///
///
///
private Rectangle rect;
///
///
///
private VisRectangle visRect;
///
///
///
///
public Memento(VisRectangle vrect)
{
visRect = vrect;
rect = visRect.rects;
x = rect.x;
y = rect.y;
w = rect.w;
h = rect.h;
}
///
///
///
public void restore()
{
rect.x = x;
rect.y = y;
rect.h = h;
rect.w = w;
visRect.rects = rect;
}
}
csharp;gutter:true;
///
/// Summary description for VisRectangle.'
/// Mediator Pattern中介者模式
///20220918
/// geovindu,Geovin Du,涂聚文
///
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);
}</p>
<pre><code> ///
/// used by Memento for saving and restoring state
///
internal Rectangle rects
{
get
{
return rect;
}
set
{
x = value.x;
y = value.y;
w = value.w;
h = value.h;
saveAsRect();
}
}
///
///
///
///
public void setSelected(bool b)
{
selected = b;
}
///
/// move to new position
///
///
///
public void move(int xp, int yp)
{
x = xp;
y = yp;
saveAsRect();
}
///
///
///
///
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);
}
}
///
/// return whether point is inside rectangle
///
///
///
///
public bool contains(int x, int y)
{
return rect.contains(x, y);
}
///
/// create Rectangle object from new position
///
private void saveAsRect()
{
rect = new Rectangle(x, y, w, h);
}
}
</code></pre>
<pre><code>
;gutter:true;
///
/// Mediates events between buttonsb
/// Mediator Pattern中介者模式
///20220918
/// geovindu,Geovin Du,涂聚文
///
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
///
///
///
///
public Mediator(PictureBox p)
{
startRect = false;
rectSelected = false;
drawings = new ArrayList();
caretakr = new CareTaker(drawings);
canvas = p;
}
///
///
///
public void startRectangle()
{
startRect = true;
}
///
///
///
///
///
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
}
///
///
///
///
public void registerRectButton(RectButton rb)
{
rect = rb;
}
///
///
///
public void unpick()
{
if (rectSelected && (selectedIndex >= 0) && (selectedIndex < drawings.Count))
{
VisRectangle vis = (VisRectangle)drawings[selectedIndex];
vis.setSelected(false);
selectedIndex = -1;
rectSelected = false;
canvas.Refresh();
}
}
///
///
///
///
///
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
}
}
}
///
///
///
public void clear()
{
drawings = new ArrayList();
caretakr.clear(drawings);
rectSelected = false;
selectedIndex = 0;
repaint();
}
///
///
///
private void repaint()
{
canvas.Refresh();
}
///
///
///
public void undo()
{
caretakr.undo();
repaint();
}
///
///
///
///
public void reDraw(Graphics g)
{
for (int i = 0; i < drawings.Count; i++)
{
VisRectangle v = (VisRectangle)drawings[i];
v.draw(g);
}
}
///
///
///
///
///
public void drag(int x, int y)
{
if (rectSelected)
{
VisRectangle v = (VisRectangle)drawings[selectedIndex];
if (v.contains(x, y))
{
v.move(x, y);
repaint();
}
}
}
}
csharp;gutter:true;
///
/// Command interface
/// Mediator Pattern中介者模式
///20220918
/// geovindu,Geovin Du,涂聚文
///
public interface Command
{
void Execute();
}</p>
<pre><code>
;gutter:true;
///
/// Summary description for CareTaker.
/// Mediator Pattern中介者模式
///20220918
/// geovindu,Geovin Du,涂聚文
///
public class CareTaker
{
private ArrayList drawings, undoList;
private Memento mem;
private VisRectangle[] draw_ings; //used only to make UML clearer
///
///
///
///
public CareTaker(ArrayList dcol)
{
clear(dcol);
}
///
///
///
///
public void rememberPosition(VisRectangle vr)
{
mem = new Memento(vr);
undoList.Add(mem);
}
///
///
///
///
public void clear(ArrayList drw)
{
drawings = drw;
undoList = new ArrayList();
}
///
///
///
///
public void Add(int intg)
{
undoList.Add(intg);
}
///
///
///
public void removeDrawing()
{
drawings.RemoveAt(drawings.Count – 1);
}
///
///
///
///
public void remove(Memento mem)
{
mem.restore();
}
///
///
///
///
public void remove(int intg)
{
removeDrawing();
}
///
///
///
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);
}
}
}
csharp;gutter:true;
///
/// Mediator Pattern中介者模式
///20220918
/// geovindu,Geovin Du,涂聚文
///
public partial class ClrButton : System.Windows.Forms.Button, Command
{
///
///
///
private Mediator med;
///
///
///
///
public ClrButton(Mediator md)
{
InitializeComponent();
med = md;</p>
<pre><code> }
///
///
///
public void Execute()
{
med.clear();
}
///
///
///
///
public ClrButton(IContainer container)
{
container.Add(this);
InitializeComponent();
}
}
</code></pre>
<pre><code>
;gutter:true;
///
/// Mediator Pattern中介者模式
///20220918
/// geovindu,Geovin Du,涂聚文
///
public partial class RectButton : System.Windows.Forms.ToolBarButton, Command
{
private ToolBarButton bt;
private Mediator med;
///
///
///
///
///
public RectButton(Mediator md, ToolBarButton tb)
{
InitializeComponent();
med = md;
bt = tb;
}
///
///
///
///
public void setSelected(bool sel)
{
bt.Pushed = sel;
}
///
///
///
public void Execute()
{
if (bt.Pushed)
med.startRectangle();
}
///
///
///
///
public RectButton(IContainer container)
{
container.Add(this);
InitializeComponent();
}
}
csharp;gutter:true;
///
/// Mediator Pattern中介者模式
///20220918
/// geovindu,Geovin Du,涂聚文
///
public partial class UndoButton : System.Windows.Forms.ToolBarButton, Command
{</p>
<pre><code> private Mediator med;
private ToolBarButton ubutton;
///
///
///
///
///
public UndoButton(Mediator md, ToolBarButton but)
{
InitializeComponent();
med = md;
ubutton = but;
}
///
///
///
public void Execute()
{
med.undo();
}
///
///
///
///
public UndoButton(IContainer container)
{
container.Add(this);
InitializeComponent();
}
}
</code></pre>
<pre><code>
调用:
;gutter:true;
///
/// Mediator Pattern
/// Mediator Pattern中介者模式
///20220918
/// geovindu,Geovin Du,涂聚文
///
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);
}
///
///
///
///
///
private void paintHandler(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
med.reDraw(g);
}
///
///
///
public MediatorPatternForm()
{
InitializeComponent();
init();
}
///
///
///
///
///
private void MediatorPatternForm_Load(object sender, EventArgs e)
{
}
///
///
///
///
///
private void tbar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
ToolBarButton tbutn = e.Button;
Command comd = (Command)commands[tbutn.GetHashCode()];
comd.Execute();
}
///
///
///
///
///
private void pic_MouseDown(object sender, MouseEventArgs e)
{
mouse_down = true;
med.createRect(e.X, e.Y);
}
///
///
///
///
///
private void pic_MouseMove(object sender, MouseEventArgs e)
{
if (mouse_down)
med.drag(e.X, e.Y);
}
///
///
///
///
///
private void pic_MouseUp(object sender, MouseEventArgs e)
{
mouse_down = false;
}
}
输出:
Original: https://www.cnblogs.com/geovindu/p/16744839.html
Author: ®Geovin Du Dream Park™
Title: CSharp: Mediator Pattern
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/620007/
转载文章受原作者版权保护。转载请注明原作者出处!