c++ - Minimalist framework -
i've read somewhere inversion of control (ioc) (kind of) principle of framework.
is correct (taking advantage of that) designed framework x because ioc pattern employed?
may ask minimal framework example (c++ code please)?
or instead, can try more specific, providing code (a complete qt project) , asking if there framework there.
the class frameworkforx (below, @ end) might called framework?
framework.pro
qt += core gui greaterthan(qt_major_version, 4): qt += widgets target = framework template = app sources += main.cpp\ widget.cpp \ frameworkforx.cpp \ solvemyproblem.cpp headers += widget.h \ frameworkforx.h \ solvemyproblem.h
main.cpp
#include "widget.h" #include <qapplication> int main(int argc, char *argv[]) { qapplication a(argc, argv); widget w; w.show(); return a.exec(); }
widget.h
#ifndef widget_h #define widget_h #include <qwidget> class qlineedit; class qlabel; class solvemyproblem; class widget : public qwidget { q_object public: widget(qwidget *parent = 0); ~widget(); private: qlineedit *lineedit; qlabel *label; solvemyproblem *solvemyproblem; public slots: void doit(); void onresult(int result); }; #endif // widget_h
widget.cpp
#include "widget.h" #include "qvboxlayout" #include "qpushbutton" #include "qlineedit" #include "qlabel" #include "solvemyproblem.h" widget::widget(qwidget *parent) : qwidget(parent), solvemyproblem(new solvemyproblem) { qvboxlayout *vlayout = new qvboxlayout(this); lineedit = new qlineedit; vlayout->addwidget(lineedit); qpushbutton *bt = new qpushbutton("do it!"); connect(bt, &qpushbutton::clicked, this, &widget::doit); vlayout->addwidget(bt, 1, qt::alignright); label = new qlabel("the result shown here."); connect(solvemyproblem, &solvemyproblem::result, this, &widget::onresult); vlayout->addwidget(label); resize(400, 100); setwindowtitle("is (frameworkforx) framework?"); } widget::~widget() { } void widget::doit() { qstring text = lineedit->text(); solvemyproblem->dosomething(text); } void widget::onresult(int result) { qstring text = qstring::number(result); label->settext(text); }
solvemyproblem.h
#ifndef solvemyproblem_h #define solvemyproblem_h #include "frameworkforx.h" class solvemyproblem : public frameworkforx { public: solvemyproblem(); int dospecificjob(const qstring &text); }; #endif // solvemyproblem_h
solvemyproblem.cpp
#include "solvemyproblem.h" solvemyproblem::solvemyproblem() { } int solvemyproblem::dospecificjob(const qstring &text) { int = text.size(); return i; }
frameworkforx.h
#ifndef frameworkforx_h #define frameworkforx_h #include "qobject" class qstring; class frameworkforx : public qobject { q_object public: frameworkforx(); void dosomething(const qstring &text); protected: virtual int dospecificjob(const qstring &text) = 0; signals: void result(int i); }; #endif // frameworkforx_h
frameworkforx.cpp
#include "frameworkforx.h" frameworkforx::frameworkforx() { } void frameworkforx::dosomething(const qstring &text) { int value = dospecificjob(text); emit result(value); }
your example close framework. , it's close ioc. in opinion, not exact pattern because of part:
widget::widget(qwidget*) : ... solvemyproblem(new solvemyproblem)
the widget
still has control on strategy. constructs it. therefore, there no inversion. proper ioc implementation take strategy e.g. parameter. then, makes sense require base class or interface:
widget::widget(qwidget*, frameworkforx* mysolver) : ... solvemyproblem(mysolver) //of course, declaration of solvemyproblem has adapted
so far ioc part. let's come framework part. possible definition of framework following:
a framework software component extension points , variation points.
a software component anything. class, library, whole set of libraries... ioc offers variation point. can vary functionality of class (in case of widget
) supplying custom implementation. can plug implementation framework build functional software component (whereas framework kind of skeleton).
so: yes. in opinion, every ioc implementation can seen framework. in concrete example, framework consist of widget
, frameworkforx
(where frameworkforx
interface). solvemyproblem
custom implementation plugged framework.
Comments
Post a Comment