标签:style blog http color 使用 os
定义
class Mediator {
public:
virtual void showDialog() = 0;
virtual void widgetChanged(Widget*) = 0;
protected:
Mediator();
virtual void createWidgets() = 0;
};
class Widget {
public:
Widget(Mediator* mediator) { m_mediator = mediator; }
virtual void changed(){ }
Mediator* mediator() const { return m_mediator; }
private:
Mediator* m_mediator;
};
class LineEdit : public Widget {
public:
LineEdit(Mediator*);
virtual void changed();
void setText(const string& text);
string text() const;
private:
string m_text;
};
void LineEdit::changed()
{
mediator()->widgetChanged(this);
}
class DialogMediator : public Mediator {
public:
virtual void widgetChanged(Widget* widget);
virtual void showDialog();
protected:
virtual void createWidgets();
private:
LineEdit* m_lineEdit;
ListBox* m_listBox;
Button* m_button;
};
void DialogMediator::createWidgets()
{
m_lineEdit = new LineEdit();
...
}
void DialogMediator::widgetChanged(Widget* widget)
{
if(widget == m_lineEdit)
{
m_listBox->setSelection(m_lineEdit->text());
m_button->setEnable(true);
}
else if(m_listBox)
{
m_lineEdit->setText(m_listBox->getSeletion());
m_button->setEnable(true);
}
else
{
m_lineEdit->setText("");
m_listBox->setSelection(lastOne);
}
}
DialogMediator mediator;
mediator.createWidgets();
mediator.showDialog();
Mediator - 中介者模式,布布扣,bubuko.com
标签:style blog http color 使用 os
原文地址:http://blog.csdn.net/harrising/article/details/38024895