Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我使用抽象类作为接口。它可以关联到哪个设计模式?_C++_Design Patterns - Fatal编程技术网

C++ 我使用抽象类作为接口。它可以关联到哪个设计模式?

C++ 我使用抽象类作为接口。它可以关联到哪个设计模式?,c++,design-patterns,C++,Design Patterns,我使用抽象基类作为接口,使用派生类作为实现。请参见下面的代码。。它是否与C++中的任何标准设计模式相关联? Class people { public: virtual void setname(string name)=0; virtual void SetAge(int Age)=0; //etc consists of only pure virtual functions like above }; Class Students: pu

我使用抽象基类作为接口,使用派生类作为实现。请参见下面的代码。。它是否与C++中的任何标准设计模式相关联?
Class people
{
    public:
       virtual void setname(string name)=0;
       virtual void SetAge(int Age)=0;
       //etc consists of only pure virtual functions like above
};

Class Students: public people
{
    void setname(string name) 
    {
        //implementation of the function
    }
    void SetAge(int Age) { //implementation }
}
我在上面定义了许多类,在Buildclass的构造函数中创建的对象如下:

Buildclass::Buildclass()
{
    people *Obj = (people*) new Students();
    interface *obj1 = (interface*) new implementation();
}
我已经为上面提供了getinstance函数,以便在另一层中使用

void BuildClass::getPeopleinstance()
{
   return Obj;
}
void BuildClass::getAnotherinstance()
{
   return Obj1;
}

上述代码是否可以与任何设计模式相关联?请告诉我好吗?我不知道

您似乎在使用工厂模式,但这并不重要。专注于编写好的代码,而不是设计模式。

对任何一个,它甚至不是一个有效的C++。学习第一个编程、数据结构和算法。当你了解基本知识时,OOP就会出现。类应该围绕算法和数据进行设计,而不是相反。@Rajesh:试着编译它。编译器会让你们知道的。你们为什么关心你们的代码可能遵循什么样的设计模式?只要你的代码是好的,这没关系,模式是一个指导原则,而不是一个检查表。@Rajesh是新来的,所以他需要rep给他一个机会。只是对他的问题投反对票对他没有帮助,他可能不想再这样做了。