OO设计、表设计

发布于 2024-10-18 22:59:08 字数 1740 浏览 0 评论 0原文

用木材或钢材设计桌子。我有以下设计,哪个更好,为什么?或者有更好的建议设计吗?

设计1:

class Meterial{
public:
  void virtual info()=0;
};

class Wood:public Meterial{
public:
  void info();
};

void Wood::info(){
  cout<<"wood"<< " ";
}

class Steel:public Meterial{
public:
  void info();
};

void Steel::info(){
  cout<<"steel"<< " ";
}




class Furniture{
  void virtual info()=0;
};

class Table:public Furniture{
private:
  Meterial * _meterial;
public:
  Table(Meterial * m);
  void info();
};

Table::Table(Meterial * m){
  _meterial= m;
}


void Table::info(){
  _meterial->info();
  cout<< " table " << endl;
}

int main(){
  Table * wood_table=new Table(new Wood());
  Table * steel_table=new Table(new Steel());
  wood_table->info();
  steel_table->info();
}

设计2:

class Meterial{
public:
  virtual void info()=0;
};

class Wood:public Meterial{
public:
  void info();
};

void Wood::info(){
  cout<<" wood " << " ";
}

class Steel:public Meterial{
public:
  void info();
};

void Steel::info(){
  cout<<" Steel " << " ";
}



class Furniture{
public:
  virtual void info()=0;
};

class Table:public Furniture{
public:
  void info();
};

void Table::info(){
  cout<<" table "<< endl;
}

class WoodTable:public Wood,public Table{
public:
  void info();
};

void WoodTable::info(){
  Wood::info();
  Table::info();
}


class SteelTable:public Steel,public Table{
public:
  void info();
};

void SteelTable::info(){
  Steel::info();
  Table::info();
}



int main(){
  WoodTable *woodTable = new WoodTable();
  SteelTable *steelTable = new SteelTable();

  woodTable->info();
  steelTable->info();
}

谢谢!

Design table with wood or steel meterial. I have following designed, which is better and why? Or any better suggestion design?

design 1 :

class Meterial{
public:
  void virtual info()=0;
};

class Wood:public Meterial{
public:
  void info();
};

void Wood::info(){
  cout<<"wood"<< " ";
}

class Steel:public Meterial{
public:
  void info();
};

void Steel::info(){
  cout<<"steel"<< " ";
}




class Furniture{
  void virtual info()=0;
};

class Table:public Furniture{
private:
  Meterial * _meterial;
public:
  Table(Meterial * m);
  void info();
};

Table::Table(Meterial * m){
  _meterial= m;
}


void Table::info(){
  _meterial->info();
  cout<< " table " << endl;
}

int main(){
  Table * wood_table=new Table(new Wood());
  Table * steel_table=new Table(new Steel());
  wood_table->info();
  steel_table->info();
}

design 2:

class Meterial{
public:
  virtual void info()=0;
};

class Wood:public Meterial{
public:
  void info();
};

void Wood::info(){
  cout<<" wood " << " ";
}

class Steel:public Meterial{
public:
  void info();
};

void Steel::info(){
  cout<<" Steel " << " ";
}



class Furniture{
public:
  virtual void info()=0;
};

class Table:public Furniture{
public:
  void info();
};

void Table::info(){
  cout<<" table "<< endl;
}

class WoodTable:public Wood,public Table{
public:
  void info();
};

void WoodTable::info(){
  Wood::info();
  Table::info();
}


class SteelTable:public Steel,public Table{
public:
  void info();
};

void SteelTable::info(){
  Steel::info();
  Table::info();
}



int main(){
  WoodTable *woodTable = new WoodTable();
  SteelTable *steelTable = new SteelTable();

  woodTable->info();
  steelTable->info();
}

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北凤男飞 2024-10-25 22:59:08

仅当您有非常具体的理由,或者所有其他选项都不好也不坏时,才应该使用继承。切勿继承以重用。

材料是否具有可以动态变化的行为?表格的材质可以在运行时改变吗?如果其中任何一个的答案是肯定的,那么您可能需要运行时多态性和继承。那么您的第一个设计就是朝着正确方向迈出的良好一步。然而,你的第二个设计是有风险的,而且几乎没有带来什么好处(请原谅这个双关语)。多重继承?为什么?您应该有一个非常具体、充分的理由来使用多重继承。

另一种设计替代方案是使用编译时多态性。

struct wood { };
struct steel { };

template<class M>
struct table { };

// ...
table<wood> wood_table;

You should only use inheritance when you have a very specific reason to, or when all other options are not better nor worse. Never inherit for reuse.

Does material have any behaviour that can change dynamically? Can the table's material change at runtime? If the answer is yes to either of these then you'll likely need runtime polymorphism and inheritance. Your first design is then a good step in the right direction. Your second design, however, is risky and brings little to the table (and pardon the pun). Multiple inheritance? Why? You should have a very specific, solid reason to use multiple inheritance.

Another design alternative is to use compile-time polymorphism.

struct wood { };
struct steel { };

template<class M>
struct table { };

// ...
table<wood> wood_table;
爱给你人给你 2024-10-25 22:59:08

想想“是”和“有”之间的区别。桌子具有材质属性,但桌子本身并不是材质。我会选择第一个。

Think of the difference between "is a" and "has a". A table has a material property, a table itself isn't a material. I would go with the first one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文