首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎么设计一个这样的框架

2013-03-21 
如何设计一个这样的框架要写个程序暂且分为底层和上层吧底层有一个声明一个函数,里面调用其他类的操作。这

如何设计一个这样的框架
要写个程序
暂且分为底层和上层吧
底层有一个声明一个函数,里面调用其他类的操作。
这些类的操作在上层中实现。
类似这样



// 底层
classA{
public:
void fun()
{
// 功能C D E 的fun需要在架构的上层实现
c.fun();
d.fun();
e.fun();
}
private:
C c;
D d;
E e;
};

// 在上层实现 C D E 类
...


就是底层已经规定了调用的流程 上层实现流程中的每个动作
这个具体怎么实现?
[解决办法]
C,D,E分别定义虚类,在你的框架中实现,具体实现的地方需要继承你的虚类,并实现接口。
[解决办法]
编译期可以确定的话,使用模板,否则使用多态。
[解决办法]
引用:
编译期可以确定的话,使用模板,否则使用多态。
++
[解决办法]
// 底层
class Interface
{
    virtual void fun() = 0;
}
class    A{
    public:
        void fun()
        {
            // 功能C D E 的fun需要在架构的上层实现
            c->fun();
            d->fun();
            e->fun();
        }
    private:
        Interface *c;
        Interface *d;
        Interface *e;
};
 
// 在上层实现 C D E 类
class C : public Interface
{};
class D : public Interface
{};
class E : public Interface
{};
[解决办法]
// 底层
class Interface
{
public:
virtual void fun() = 0;
};
class A { 
public:
void fun()
{
// 功能C D E 的fun需要在架构的上层实现
c->fun();
d->fun();
e->fun();
}
void SetC(Interface *pc) {c = pc;}
void SetD(Interface *pd) {d = pd;}
void SetE(Interface *pe) {e = pe;}
private:
Interface *c;
Interface *d;
Interface *e;
};

// 在上层实现 C D E 类
class C : public Interface
{
void fun() {printf("this is c\n");}
};
class D : public Interface
{
void fun() {printf("this is d\n");}
};
class E : public Interface
{
void fun() {printf("this is e\n");}
}; 


int _tmain(int argc, _TCHAR* argv[])
{
A a;
C c;
D d;
E e;
a.SetC(&c);
a.SetD(&d);
a.SetE(&e);
a.fun();
}

输出:
this is c
this is d
this is e

热点排行