CSharp: Mediator Pattern

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;
}
}

输出:

CSharp: Mediator Pattern

Original: https://www.cnblogs.com/geovindu/p/16744839.html
Author: ®Geovin Du Dream Park™
Title: CSharp: Mediator Pattern

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/620007/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 面试官:说下你对方法区演变过程和内部结构的理解

    之前我们已经了解过”运行时数据区”的程序计数器、虚拟机栈、本地方法栈和堆空间,今天我们就来了解一下最后一个模块——方法区。 简介 《Java虚拟机规范》中明…

    Java 2023年6月5日
    071
  • 系统调用跟踪——分析(一)

    通过strace工具可跟踪用户进程与Linux内核的调用交互,可看到其中的System Call(系统调用)情况; &#x5B89;&#x88C5;strace&a…

    Java 2023年6月16日
    097
  • Java8特性详解 lambda表达式(一):使用篇

    ​ 在 Java 8之前,一个实现了只有一个抽象方法的接口的匿名类看起来更像Lambda 表达式。下面的代码中,anonymousClass方法调用waitFor方法,参数是一个实…

    Java 2023年5月29日
    081
  • current request is not a multipart request

    current request is not a multipart request posted on2022-05-14 14:56 水水头不秃 阅读(13 ) 评论() 编辑…

    Java 2023年6月5日
    0102
  • git学习(一)-fork操作

    fork操作 对于某一个项目来说,如果自己不属于开发者中的一员,那么只能先fork别人的代码,然后将代码拉取到本地进行修改之后,再向原来的项目发起pull request。 for…

    Java 2023年6月7日
    073
  • Oracle数据库😊

    sqlplus常用命令 连接数据库:conn 用户名/密码@网络服务标识[as sysdba] 断开数据库连接: 断开和oracle的连接但是不退出sqlplus窗口 区别:sta…

    Java 2023年6月15日
    052
  • Java代码中System.currentTimeMillis()方法具有什么功能呢?

    转自:http://java265.com/JavaCourse/202111/1749.html 下文笔者讲述System.currentTimeMillis()方法的具体功能,…

    Java 2023年6月15日
    0100
  • 设计模式之适配器模式

    本文通过老王使用纸质书籍阅读小王使用电子书籍的故事,详细说明设计模式中的结构型设计模式之适配器模式,分别对对象适配器和类适配器代码实现,最后为了加深理解,会列举适配器设计模式在JD…

    Java 2023年6月8日
    073
  • ORACLE错误码大全

    ORA-00001: 违反唯一约束条件 (.)错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。ORA-00017: 请求会话以设置跟踪事件ORA-00018: 超出最…

    Java 2023年6月8日
    098
  • Spring源码导入IDEA

    1、下载Spring源码 方法一: (1)下载并安装GIt,下载地址:https://git-scm.com/download/win (2)配置用户名和邮箱; (3)使用git命…

    Java 2023年5月30日
    090
  • 财务自由之路上的第一步:思想

    各位读者,今天吃汤圆或者饺子了吗?冬至快乐!!! 冬至新开系列 —— 财务自由之路 此为第一篇,你的思想决定你的穷富 理财 理财的定义:所有和钱有关的安排都是理财,例如:消费计划,…

    Java 2023年6月9日
    076
  • java JWT(json web token)三分钟快速掌握

    一:jwt 共有三部分: 令牌组成: 1.标头(header) 2.有效载荷(payload) 3.签名(Signature) 三部分以”.”点进行分割 生…

    Java 2023年6月5日
    075
  • day03-MySQL基础知识02

    MySQL基础知识02 4.CRUD 数据库CRUD语句:增(create)、删(delete)、改(update)、查(Retrieve) Insert 语句 (添加数据) Up…

    Java 2023年6月15日
    084
  • 创建Springboot工程接收acticemq消息

    1、JMSFactory配置 <beans xmlns="http://www.springframework.org/schema/beans" xml…

    Java 2023年5月30日
    0119
  • Typora的序列号免费分享

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    Java 2023年5月29日
    082
  • Stream流使用(Lamdba表达式重点场景)

    类型二: 数组:Arrays.stream(数组)或者使用Stream.of来创建 类型三:双列集合:转换成单列集合后创建 2)中间操作 filter( ) // 用作过滤集合元素…

    Java 2023年6月15日
    068
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球