[Description] adds new functions to the Code through the decorator without modifying the structure of the original code.
[UML diagram]
Figure 1 UML diagram
(1) the original code is a Component class that provides operation operations;
(2) The Decorator class provides an extended operation function;
(3) Pay attention to the differences with the Template mode (design mode (1)-Template mode (Template.
[Sample Code]
Component. h
[Html]
# Ifndef COMPONENT_H
# Define COMPONENT_H
Class Component
{
Public:
Component ();
Public:
Virtual void operation ();
};
# Endif // COMPONENT_H
# Ifndef COMPONENT_H
# Define COMPONENT_H
Class Component
{
Public:
Component ();
Public:
Virtual void operation ();
};
# Endif // COMPONENT_H
Component. cpp
[Html] view plaincopyprint? # Include <QDebug>
# Include "component. h"
Component: Component ()
{
QDebug () <"construct Component ";
}
Void Component: operation ()
{
QDebug () <"Base Function ";
}
# Include <QDebug>
# Include "component. h"
Component: Component ()
{
QDebug () <"construct Component ";
}
Void Component: operation ()
{
QDebug () <"Base Function ";
}
Decorator. h
[Html]
# Ifndef DECORATOR_H
# Define DECORATOR_H
# Include "component. h"
Class Decorator: public Component
{
Public:
Decorator (Component component );
Private:
Component component;
Public:
Void operation ();
};
# Endif // DECORATOR_H
# Ifndef DECORATOR_H
# Define DECORATOR_H
# Include "component. h"
Class Decorator: public Component
{
Public:
Decorator (Component component );
Private:
Component component;
Public:
Void operation ();
};
# Endif // DECORATOR_H
Decorator. cpp
[Html]
# Include <QDebug>
# Include "decorator. h"
Decorator: Decorator (Component component)
{
QDebug () <"construct Decorator ";
This-> component = component;
}
Void Decorator: operation ()
{
Component. operation ();
QDebug () <"Extend Function ";
}
# Include <QDebug>
# Include "decorator. h"
Decorator: Decorator (Component component)
{
QDebug () <"construct Decorator ";
This-> component = component;
}
Void Decorator: operation ()
{
Component. operation ();
QDebug () <"Extend Function ";
}
Main. cpp
[Html]
# Include "component. h"
# Include "decorator. h"
Int main (void)
{
Component component;
Component. operation ();
Decorator decorator (component );
Decorator. operation ();
Return 0;
}
# Include "component. h"
# Include "decorator. h"
Int main (void)
{
Component component;
Component. operation ();
Decorator decorator (component );
Decorator. operation ();
Return 0;
}
[Running result]
[Html]
Construct Component
Base Function
Construct Component
Construct Component
Construct Decorator
Base Function
Extend Function
Construct Component
Base Function
Construct Component
Construct Component
Construct Decorator
Base Function
Extend Function
Result Analysis]
With the help of the decorator, new functions are added to the Code without changing the original code.
[Instance profiling]
Design Mode (1)-Template this article introduces an improved Qt embedded input method. The code is rewritten using the decoration mode below. First look at the UML diagram:
Figure 2
(1) the original code is the QLineEdit class;
(2) The decorator is the QLineEditWithIM class and provides the installIM () method.
[Code list]
The following is only the modified part. For details about the code, refer to the document Qt Input Method Design (embedded) and design mode (1)-Template mode.
Qlineeditwithim. h
[Html]
# Ifndef QLINEEDITWITHIM_H
# Define QLINEEDITWITHIM_H
# Include <QLineEdit>
# Include "inputmethod. h"
Class QLineEditWithIM: public QLineEdit
{
Public:
QLineEditWithIM (QLineEdit * lineEdit );
~ QLineEditWithIM ();
Private:
QLineEdit * lineEdit;
InputMethod * im;
Public:
Void installIM ();
};
# Endif // QLINEEDITWITHIM_H
# Ifndef QLINEEDITWITHIM_H
# Define QLINEEDITWITHIM_H
# Include <QLineEdit>
# Include "inputmethod. h"
Class QLineEditWithIM: public QLineEdit
{
Public:
QLineEditWithIM (QLineEdit * lineEdit );
~ QLineEditWithIM ();
Private:
QLineEdit * lineEdit;
InputMethod * im;
Public:
Void installIM ();
};
# Endif // QLINEEDITWITHIM_H
Qlineeditwithim. cpp
[Html]
# Include "qlineeditwithim. h"
QLineEditWithIM: QLineEditWithIM (QLineEdit * lineEdit)
{
// # Ifdef Q_WS_QWS
Im = new InputMethod;
This-> lineEdit = lineEdit;
// # Endif
}
QLineEditWithIM ::~ QLineEditWithIM ()
{
Delete im;
}
Void QLineEditWithIM: installIM ()
{
// # Ifdef Q_WS_QWS
InstallEventFilter (im );
Connect (im-> keyboard, SIGNAL (setvalue (QString), this, SLOT (setText (QString )));
// # Endif
}
# Include "qlineeditwithim. h"
QLineEditWithIM: QLineEditWithIM (QLineEdit * lineEdit)
{
// # Ifdef Q_WS_QWS
Im = new InputMethod;
This-> lineEdit = lineEdit;
// # Endif
}
QLineEditWithIM ::~ QLineEditWithIM ()
{
Delete im;
}
Void QLineEditWithIM: installIM ()
{
// # Ifdef Q_WS_QWS
InstallEventFilter (im );
Connect (im-> keyboard, SIGNAL (setvalue (QString), this, SLOT (setText (QString )));
// # Endif
}
Login. h
[Html]
# Ifndef LOGIN_H
# Define LOGIN_H
# Include <QDialog>
# Include "qlineeditwithim. h"
Class QLabel;
Class QLineEdit;
Class QDialogButtonBox;
Class QLogin: public QDialog
{
Q_OBJECT
Public:
QLogin ();
~ QLogin ();
Public:
QLabel * managerLabel;
QLabel * passwdLabel;
QLineEditWithIM * managerEdit;
QLineEditWithIM * passwdEdit;
QPushButton * okButton;
QPushButton * cancelButton;
QDialogButtonBox * buttonBox;
Signals:
Void Authorize ();
Private slots:
Void login ();
Void cancel ();
};
# Endif // LOGIN_H
# Ifndef LOGIN_H
# Define LOGIN_H
# Include <QDialog>
# Include "qlineeditwithim. h"
Class QLabel;
Class QLineEdit;
Class QDialogButtonBox;
Class QLogin: public QDialog
{
Q_OBJECT
Public:
QLogin ();
~ QLogin ();
Public:
QLabel * managerLabel;
QLabel * passwdLabel;
QLineEditWithIM * managerEdit;
QLineEditWithIM * passwdEdit;
QPushButton * okButton;
QPushButton * cancelButton;
QDialogButtonBox * buttonBox;
Signals:
Void Authorize ();
Private slots:
Void login ();
Void cancel ();
};
# Endif // LOGIN_H
Login. cpp
[Html]
# Include <QtGui>
# Include "login. h"
QLogin: QLogin ()
{
ManagerLabel = new QLabel (tr ("& Manager :"));
QLineEdit * _ managerEdit = new QLineEdit;
ManagerEdit = new QLineEditWithIM (_ managerEdit );
ManagerEdit-> installIM ();
ManagerLabel-> setBuddy (managerEdit );
PasswdLabel = new QLabel (tr ("& Passwd :"));
QLineEdit * _ passwdEdit = new QLineEdit;
PasswdEdit = new QLineEditWithIM (_ passwdEdit );
PasswdEdit-> installIM ();
PasswdEdit-> setEchoMode (QLineEdit: Password );
PasswdLabel-> setBuddy (passwdEdit );
OkButton = new QPushButton (tr ("& Login "));
CancelButton = new QPushButton ("& Cancel ");
OkButton-> setDefault (true );
ButtonBox = new QDialogButtonBox;
ButtonBox-> addButton (okButton, QDialogButtonBox: ActionRole );
ButtonBox-> addButton (cancelButton, QDialogButtonBox: AcceptRole );
Connect (okButton, SIGNAL (clicked (), this, SLOT (login ()));
Connect (cancelButton, SIGNAL (clicked (), this, SLOT (cancel ()));
QHBoxLayout * topLayout = new QHBoxLayout;
TopLayout-> addWidget (managerLabel );
TopLayout-> addWidget (managerEdit );
QHBoxLayout * midLayout = new QHBoxLayout;
MidLayout-> addWidget (passwdLabel );
MidLayout-> addWidget (passwdEdit );
QVBoxLayout * mainLayout = new QVBoxLayout;
MainLayout-> addLayout (topLayout );
MainLayout-> addLayout (midLayout );
MainLayout-> addWidget (buttonBox );
MainLayout-> setMargin (20 );
SetLayout (mainLayout );
ManagerEdit-> setFocus ();
QIcon;
Icon. addFile (QString: fromUtf8 (":/new/main/picture/logo.png"), QSize (), QIcon: Normal, QIcon: Off );
Setmediawicon (icon );
SetWindowTitle ("Login ");
}
QLogin ::~ QLogin ()
{
QDebug () <"destruct login ";
Delete managerLabel;
Delete managerEdit;
Delete passwdLabel;
Delete passwdEdit;
Delete okButton;
Delete cancelButton;
}
/*
* Name: void login ()
* Type: slot
* Func: login when authorize
* In: Null
* Out: Null
*/
Void QLogin: login ()
{
QDebug () <managerEdit-> text ();
QDebug () <passwdEdit-> text ();
}
/*
* Name: void cancel ()
* Type: slot
* Func: cancel login
* In: Null
* Out: Null
*/
Void QLogin: cancel ()
{
ManagerEdit-> clear ();
PasswdEdit-> clear ();
Close ();
}
# Include <QtGui>
# Include "login. h"
QLogin: QLogin ()
{
ManagerLabel = new QLabel (tr ("& Manager :"));
QLineEdit * _ managerEdit = new QLineEdit;
ManagerEdit = new QLineEditWithIM (_ managerEdit );
ManagerEdit-> installIM ();
ManagerLabel-> setBuddy (managerEdit );
PasswdLabel = new QLabel (tr ("& Passwd :"));
QLineEdit * _ passwdEdit = new QLineEdit;
PasswdEdit = new QLineEditWithIM (_ passwdEdit );
PasswdEdit-> installIM ();
PasswdEdit-> setEchoMode (QLineEdit: Password );
PasswdLabel-> setBuddy (passwdEdit );
OkButton = new QPushButton (tr ("& Login "));
CancelButton = new QPushButton ("& Cancel ");
OkButton-> setDefault (true );
ButtonBox = new QDialogButtonBox;
ButtonBox-> addButton (okButton, QDialogButtonBox: ActionRole );
ButtonBox-> addButton (cancelButton, QDialogButtonBox: AcceptRole );
Connect (okButton, SIGNAL (clicked (), this, SLOT (login ()));
Connect (cancelButton, SIGNAL (clicked (), this, SLOT (cancel ()));
QHBoxLayout * topLayout = new QHBoxLayout;
TopLayout-> addWidget (managerLabel );
TopLayout-> addWidget (managerEdit );
QHBoxLayout * midLayout = new QHBoxLayout;
MidLayout-> addWidget (passwdLabel );
MidLayout-> addWidget (passwdEdit );
QVBoxLayout * mainLayout = new QVBoxLayout;
MainLayout-> addLayout (topLayout );
MainLayout-> addLayout (midLayout );
MainLayout-> addWidget (buttonBox );
MainLayout-> setMargin (20 );
SetLayout (mainLayout );
ManagerEdit-> setFocus ();
QIcon;
Icon. addFile (QString: fromUtf8 (":/new/main/picture/logo.png"), QSize (), QIcon: Normal, QIcon: Off );
Setmediawicon (icon );
SetWindowTitle ("Login ");
}
QLogin ::~ QLogin ()
{
QDebug () <"destruct login ";
Delete managerLabel;
Delete managerEdit;
Delete passwdLabel;
Delete passwdEdit;
Delete okButton;
Delete cancelButton;
}
/*
* Name: void login ()
* Type: slot
* Func: login when authorize
* In: Null
* Out: Null
*/
Void QLogin: login ()
{
QDebug () <managerEdit-> text ();
QDebug () <passwdEdit-> text ();
}
/*
* Name: void cancel ()
* Type: slot
* Func: cancel login
* In: Null
* Out: Null
*/
Void QLogin: cancel ()
{
ManagerEdit-> clear ();
PasswdEdit-> clear ();
Close ();
}
[Analysis]
(1) The key difference with the template mode is that QLineEditWithIM references the QLineEdit object;
(2) The template mode call code is
[Html]
QLineEditWithIM * managerEdit;
ManagerEdit = new QLineEditWithIM;
QLineEditWithIM * managerEdit;
ManagerEdit = new QLineEditWithIM;
The code for calling the decoration mode is
[Html]
QLineEdit * _ managerEdit = new QLineEdit;
ManagerEdit = new QLineEditWithIM (_ managerEdit );
ManagerEdit-> installIM ();
QLineEdit * _ managerEdit = new QLineEdit;
ManagerEdit = new QLineEditWithIM (_ managerEdit );
ManagerEdit-> installIM ();
Essentially, in the template mode, we do not use the reference object lineEdit provided by QLineEditWithIM. Using the template mode is also relatively simple. It can be seen that QLineEdit is considered as a template, and the template mode is more suitable.
Author: tandesir