Subject: [基础] Symbian程序中的观察者模式
Lee
Administrator
Rank: 9Rank: 9Rank: 9



UID 2
Digest Posts 0
Credits 18567
Posts 133
Money 189
Reading Access 200
Registered 13-3-2007
Status Offline
Post at 13-3-2007 11:43  Profile | Blog | P.M. 
Symbian程序中的观察者模式

在这个站点上有很多关于Symbian程序外表的文章,很少有关于架构设计的观点。在以下的文中中,我介绍一下在Symbian应用程序中的观察者模式,我会用一些有意义的类名来代表整个Symbian程序的设计流程。在这个应用程序中,将告诉你在一个Symbian程序中如何使用 Engine和前台,UI接口,和用户直接的交互。在文章中,我已经给了一种解决的方案。在接下来的文章种,我将介绍另外一种方法,通过内嵌类。让我们开始应用程序的引擎类和它的观察者,我们创建了一个观察者接口 MObserver,定义如下:

class MObserver
{
public:
        virtual void CallbackFunction1() = 0;
};

接口,它有一个纯虚函数,它必须被一个创建类实现。在我们的例子中,Engine(引擎类)将实现它:

class Engine : public MObserver
{
public:        Engine(CAppUi& aAppUi);
        void CallbackFunction1();//From MObserver
        void DoSomething();
        Subject&  GetSubject();
private:        Subject*  iSubject;
        CAppUi&  iAppUi;
};

从名字可以清晰的看出,Engine类有一个Subject类,他是实际上在后台工作的类;CAppUi 类是核心与视图的用户接口。引擎创建一个Subject类,通常在Symbian程序中,CAppUi 类是通过veiw类表现数据到用户接口的(UI)。

Subject类申明如下:

class Subject
{
public:
        Subject(MObserver& aObserver);
        void DoSomething();
private:
        MObserver& iObserver;
};

很明显,Subject类链接了它的观察者iObserver。Symbian程序在一般情况下,在后台通过一些异步函数,Subject类将继承至CActive类而工作。这里有一个 DoSomething()将调用一些异步函数,这个函数也将调用MObserver中的CallbackFunction1()。CallbackFunction1 函数将更新AppUi 应用程序或引擎和UI中相关的表现形式。因此让我们来看看CAppUi类的申明:

class CAppUi
{
public:
        CAppUi();
        Engine& GetEngine();
        void PrintToUI(char* msg);
        virtual ~CAppUi();
private:
        Engine* iEngine;
};

AppUi类的作用是,创建应用程序的引擎。记住,应用程序和Engine已经循环的引用,整个的流程如下:

AppUi将创建引擎;
Engine创建Subject;
AppUi通过一些菜单命令调用Engine中的DoSometing函数;
Engine将委派一些任务通过Subject类的DoSometing函数;
Subject的DoSometing函数将通知通过CallbackFunction1创建Observer;
Subject的DoSometing函数看上去如下:

void Subject::DoSomething()
{        //Call the Asynchronous function
        //Now Notify the Observer regarding this.
        iObserver.CallbackFunction1();
}

Observer中的这个回调函数将打印AppUi中消息通过函数PrintToUi。这个函数将呈现从Engine得到的消息到Veiw中。

希望这篇文章能讲清楚一些Symbian入门者对于如何使用UI的疑惑。

【感谢网友koffdsa的另一篇精彩译文:译文地址




有其他问题请加入Symbian开发群参与讨论:群 ①:623041已满,群②:36865776已满 请加群③:76404484
Top
Lee
Administrator
Rank: 9Rank: 9Rank: 9



UID 2
Digest Posts 0
Credits 18567
Posts 133
Money 189
Reading Access 200
Registered 13-3-2007
Status Offline
Post at 13-3-2007 11:46  Profile | Blog | P.M. 
Observer Pattern in Symbian application

As in this site there are many articles about some or the other implementation of certain aspects of a Symbian application, but there is not many on how a Symbian application is formed from a designing point of view. In the following article I have tried to throw some lights on how the Observer pattern (refer GoF book on Design Pattern) is usually implemented in a normal Symbian application. I have tried to simulate the whole design using meaningful names of the classes so that one can visualize the normal flow of a Symbian application. In this application I have tried to model how an engine and the front end UI interacts to present a responding UI to the user of a certain application. In this article I have given one way of solution. In the next article I will present the other way of doing it through nested classes. Lets start with the Engine class of an application and its Observer. Lets say we have an Observer interface called MObserver which has been defined as follows:

class MObserver
{
public:
        virtual void CallbackFunction1() = 0;
};

As this is an interface having a pure virtual function, this has to be implemented by a concrete class. In our case the Engine class will implement it as follows.

class Engine : public MObserver
{
public:        Engine(CAppUi& aAppUi);
        void CallbackFunction1();//From MObserver
        void DoSomething();
        Subject&  GetSubject();
private:        Subject*  iSubject;
        CAppUi&  iAppUi;
};

As it is becoming clear from the naming of thse classes, the Engine class has reference to the Subject class which will actually do the work in the background and the CAppUi class which is actually the Front End class of the application. It’s the engine whhich actually create its subject class. In normal Symbian application this CAppUi class is responsible for rendering data in the UI through the View class.

Now lets talk about the Subject class which has been declared as follows:

class Subject
{
public:
        Subject(MObserver& aObserver);
        void DoSomething();
private:
        MObserver& iObserver;
};

As it is obvious that the subject is linked with its observer. In normal scenario of a Symbian application, this Subject class will be derived from CActive class to do the work in the background through some Asynchronous function. Here the function called DoSomething() will call some of the Asynchronous functions. This function will also call the CallbackFunction1() of the MObserver. Hence it is basically working as the function Notify as mentioned in the GoF book. This CallbackFunction1 will actually update the AppUi regarding the present state of application/engine and the UI will look responsive. So lets look how the CAppUi class has been declared.

class CAppUi
{
public:
        CAppUi();
        Engine& GetEngine();
        void PrintToUI(char* msg);
        virtual ~CAppUi();
private:
        Engine* iEngine;
};

The responsibility of the AppUi class is to create the engine of the application. Please remember that the Application and the Engine has cyclic reference to each other. So the flow whole flow goes like this:

AppUi will create the Engine.
The engine will create the Subject.
The AppUi will call the DoSomething function of the Engine through some of its Menu command.
The engine will delegate this task to the DoSomething function of the Subject.
The DoSomething function of the Subject will notify the concrete observer (which is the engine) through the CallbackFunction1.
The DoSomething function of the Subject will look something like this:

void Subject::DoSomething()
{        //Call the Asynchronous function
        //Now Notify the Observer regarding this.
        iObserver.CallbackFunction1();
}

And this Callback function of the Observer will actually print the message in the AppUi through the function PrintToUi. In actual scenario this function will actually render the message gotten from Engine in the View.

I hope this article will clarify some of the doubts of a newbee Symbian programmer about how the Ui actually responds when there is some background processing going on.

原文地址




有其他问题请加入Symbian开发群参与讨论:群 ①:623041已满,群②:36865776已满 请加群③:76404484
Top
koffdsa
Newbie
Rank: 1



UID 144
Digest Posts 0
Credits 2
Posts 3
Money 14
Reading Access 10
Registered 5-6-2007
Status Offline
Post at 5-6-2007 14:02  Profile | P.M. 
。。。。我刚刚上传完我翻译的,发现发错了地方,结果到这一看发现已经有了,惭愧啊:Q :Q :Q :Q
Top
Lee
Administrator
Rank: 9Rank: 9Rank: 9



UID 2
Digest Posts 0
Credits 18567
Posts 133
Money 189
Reading Access 200
Registered 13-3-2007
Status Offline
Post at 5-6-2007 16:39  Profile | Blog | P.M. 
译文已经收编,可以方便其他开发者参考。




有其他问题请加入Symbian开发群参与讨论:群 ①:623041已满,群②:36865776已满 请加群③:76404484
Top
 


All times are GMT+8, the time now is 9-9-2010 20:15

CopyRight © Symbianx.cn 2007 Powered By Discuz! 5
Clear Cookies - Contact Us - Symbian OS系统[S60,UIQ]开发中文翻译论坛 - Archiver

本站原文版权归原文作者所有,本站译文版权归本站所有,如需转载请注明原文和译文出处,否则追究法律责任