以下程序是否实施工厂设计 [英] Whether the following program implement the factory Design

查看:72
本文介绍了以下程序是否实施工厂设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个以下程序是否实现了工厂设计。如果不是什么

是我必须更改的内容才能实现以下目标

程序设计为工厂设计模式。


#include< iostream>


使用命名空间std;


class Quad

{

public:

void Area();

void Desc();

};


级广场:公共四元组

{

公众:

Square(){};


void Area(int x)

{

cout<<"" square of square is ="<< x * x<<< endl; < br $>
}


void Desc()

{

cout<<"这个派生类从Base Class Quad开始的广场<<< endl;

}

};


类矩形:public Quad

{

public:

Rectangle(){};


void Area(int x,int y)

{

cout<<">矩形区域="<< x * y<< endl;

}


void Desc()

{

cout<<"这个来自Base Class Quad&Qu的派生类矩形ot;<< endl;

}


};


class Creator

{

public:

Quad * Creator :: Create(int id)

{

if (id == 2)

返回新Square;

else

返回新的矩形;

}

};


int main(int argc,char * argv [])

{

Creator mcreator;

if(argc< = 2)

{

Square * square = reinterpret_cast< Square *>(mcreator.Create(2 ));

square-> Area(3);

square-> Desc();

}

else

{

矩形*矩形=

reinterpret_cast< Rectangle *>(mcreator.Create(1));

rectangle-> Area(3,4);

rectangle-> Desc();

}


返回0;

}

解决方案

sunnyaécrit:
< blockquote class =post_quotes>
这个以下程序是否实现了工厂设计。如果不是什么

是我必须改变的事情,以便使这个以下

程序设计为工厂设计模式。


#include< iostream>


>使用命名空间std;

类Quad
{
公开:
void Area( );
void Desc();
};

班级广场:公共四方
{
公开:
Square(){} ;

void Area(int x)
{
cout<<"" square of square is ="<< x * x<< endl;
}

void Desc()
{
cout<<"这个派生类Square来自Base Class Quad"<< endl;
}
};

类Rectangle:public Quad
公共:
Rectangle(){};

void Area(int x,int y)
{
cout<<">矩形区域="<< x * y<< endl;
}
void Desc()
{
cout<<""这个来自Base Class Quad的衍生类Rectangle"<<< endl;
}
{
公开:
Quad * Creator :: Create(int id)
{
if( id == 2)
返回新广场;
其他
返回新的矩形;
}
};

int main(int argc,char * argv [])
{
创作者mcreator;
if(argc< = 2)
{
Square * square = reinterpret_cast< Square *>(mcreator.Create(2));
square-> ;区域(3);
square-> Desc();
}

{
矩形*矩形=
reinterpret_cast<矩形*> ;(mcreator.Create(1));
rectangle-> Area(3,4);
rectangle-> Desc();
}

返回0;
}




你在考虑

- " Abstract"工厂设计
http://en.wikipedia.org/wiki/Abstract_factory_pattern



- 工厂方法设计
http://en.wikipedia.org/wiki/Factory_method_pattern

这似乎更有可能来自你的代码

1.制作Quad :: Area和Quad :: Desc虚拟

2.如果你想要阻止在工厂外创建Square和Retangle

,使他们的构造函数受到保护并声明

Creator作为朋友类

3.考虑使用enum作为Creator :: Create和

的参数也许会让它变得静止


Michael




Michael DOUBEZ写道:


sunnyaécrit:


这是否这样做以下程序实现工厂设计。如果不是什么

是我必须改变的事情,以便使这个以下

程序设计为工厂设计模式。 />

#include&l吨;&的iostream GT;


使用命名空间std;


class Quad

{

public:

void Area();

void Desc();

};


级广场:公共四元组

{

公众:

Square(){};


void Area(int x)

{

cout<<"" square of square is ="<< x * x<<< endl; < br $>
}


void Desc()

{

cout<<"这个派生类从Base Class Quad开始的广场<<< endl;

}

};


类矩形:public Quad

{

public:

Rectangle(){};


void Area(int x,int y)

{

cout<<">矩形区域="<< x * y<< endl;

}


void Desc()

{

cout<<"这个来自Base Class Quad&Qu的派生类矩形ot;<< endl;

}


};


class Creator

{

public:

Quad * Creator :: Create(int id)

{

if (id == 2)

返回新Square;

else

返回新的矩形;

}

};


int main(int argc,char * argv [])

{

Creator mcreator;

if(argc< = 2)

{

Square * square = reinterpret_cast< Square *>(mcreator.Create(2 ));

square-> Area(3);

square-> Desc();

}

else

{

矩形*矩形=

reinterpret_cast< Rectangle *>(mcreator.Create(1));

rectangle-> Area(3,4);

rectangle-> Desc();

}


返回0;

}




你在想

- "摘要"工厂设计
http://en.wikipedia.org/wiki/Abstract_factory_pattern



- 工厂方法设计
http://en.wikipedia.org/wiki/Factory_method_pattern

这似乎更有可能来自你的代码

1.制作Quad :: Area和Quad :: Desc virutal



一旦你做了这个虚拟你可以remvoe reintrepret cast。然后这个

desing将是工厂模式,你的工厂方法是Creator。


2.如果你想阻止在工厂外创建Square和Retangle

,请使他们的构造函数受到保护并声明

作为朋友类的创作者

3.考虑使用enum作为Creator的参数::创建和

或许使其静止


Michael




sunny写道:


以下程序是否实现了工厂设计。如果不是什么

是我必须更改的内容才能实现以下目标

程序设计为工厂设计模式。


#include< iostream>


使用命名空间std;


class Quad

{

public:

void Area();

void Desc();

};


级广场:公共四元组

{

公众:

Square(){};


void Area(int x)

{

cout<<"" square of square is ="<< x * x<<< endl; < br $>
}


void Desc()

{

cout<<"这个派生类从Base Class Quad开始的广场<<< endl;

}

};


类矩形:public Quad

{

public:

Rectangle(){};


void Area(int x,int y)

{

cout<<">矩形区域="<< x * y<< endl;

}


void Desc()

{

cout<<" This Derived c lass Rectangle from Base Class Quad"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

{

public:

Quad * Creator :: Create(int id)

{

if(id == 2)

返回新Square;

else

返回新矩形;

}

};


int main(int argc,char * argv [])

{

Creator mcreator;

if(argc< = 2)

{

Square * square = reinterpret_cast< Square *>( mcreator.Create(2));



在这种情况下不要使用reinterpret_cast。如果您对速度有所了解,请使用dynamic_cast或

static_cast。可能更好用

boost :: polymorphic_downcast。


我不确定你是否使用了reinterpret_cast这个

特定情况会产生未定义的行为,但对问题中的

对象进行微小更改(例如引入MI)肯定会。

工厂的全部意义是隐藏这种情况详细信息。


square-> Area(3);

square-> Desc( );

}

其他

{

矩形*矩形=

reinterpret_cast< ; Rectangle *>(mcreator.Create(1));

rectangle-> Area(3,4);

rectangle-> Desc(); < br $>
}



工厂创建的对象不需要像这样生成。如果

你从对象的创建中逐渐减少,那么工厂是无意义的b
。是的,创建者是一个工厂,但它正在创建的对象

不是多态的,所以没有意义。这就是你得到的其他答案背后的理由




Does this following program implement the factory design.if not what
are things that i have to change in order to make this following
program to be designed to factory design pattern.

#include<iostream>

using namespace std;

class Quad
{
public:
void Area();
void Desc();
};

class Square : public Quad
{
public:
Square() {};

void Area(int x)
{
cout<<"Area of square is = "<<x*x<<endl;
}

void Desc()
{
cout<<"This Derived class Square from Base Class Quad"<<endl;
}
};

class Rectangle : public Quad
{
public:
Rectangle() {};

void Area(int x, int y)
{
cout<<"Area of Rectangle is = "<<x*y<<endl;
}

void Desc()
{
cout<<"This Derived class Rectangle from Base Class Quad"<<endl;
}

};

class Creator
{
public:
Quad* Creator::Create(int id)
{
if (id==2)
return new Square;
else
return new Rectangle;
}
};

int main(int argc, char* argv[])
{
Creator mcreator;
if (argc<=2)
{
Square *square = reinterpret_cast<Square*>(mcreator.Create(2));
square->Area(3);
square->Desc();
}
else
{
Rectangle *rectangle =
reinterpret_cast<Rectangle*>(mcreator.Create(1));
rectangle->Area(3,4);
rectangle->Desc();
}

return 0;
}

解决方案

sunny a écrit :

Does this following program implement the factory design.if not what
are things that i have to change in order to make this following
program to be designed to factory design pattern.

#include<iostream>

>using namespace std;

class Quad
{
public:
void Area();
void Desc();
};

class Square : public Quad
{
public:
Square() {};

void Area(int x)
{
cout<<"Area of square is = "<<x*x<<endl;
}

void Desc()
{
cout<<"This Derived class Square from Base Class Quad"<<endl;
}
};

class Rectangle : public Quad
{
public:
Rectangle() {};

void Area(int x, int y)
{
cout<<"Area of Rectangle is = "<<x*y<<endl;
}

void Desc()
{
cout<<"This Derived class Rectangle from Base Class Quad"<<endl;
}

};

class Creator
{
public:
Quad* Creator::Create(int id)
{
if (id==2)
return new Square;
else
return new Rectangle;
}
};

int main(int argc, char* argv[])
{
Creator mcreator;
if (argc<=2)
{
Square *square = reinterpret_cast<Square*>(mcreator.Create(2));
square->Area(3);
square->Desc();
}
else
{
Rectangle *rectangle =
reinterpret_cast<Rectangle*>(mcreator.Create(1) );
rectangle->Area(3,4);
rectangle->Desc();
}

return 0;
}



Are you thinking of
- "Abstract" Factory design
http://en.wikipedia.org/wiki/Abstract_factory_pattern
or
- Factory "method" design
http://en.wikipedia.org/wiki/Factory_method_pattern
Which seems more likely from your code
1. Make Quad::Area and Quad::Desc virutal
2. If you want to prevent creation of Square and Retangle
outside the factory, make their constructor protected and declare
Creator as a friend class
3. Consider using enum as parameter of Creator::Create and
perhaps make it static

Michael



Michael DOUBEZ wrote:

sunny a écrit :

Does this following program implement the factory design.if not what
are things that i have to change in order to make this following
program to be designed to factory design pattern.

#include<iostream>

using namespace std;

class Quad
{
public:
void Area();
void Desc();
};

class Square : public Quad
{
public:
Square() {};

void Area(int x)
{
cout<<"Area of square is = "<<x*x<<endl;
}

void Desc()
{
cout<<"This Derived class Square from Base Class Quad"<<endl;
}
};

class Rectangle : public Quad
{
public:
Rectangle() {};

void Area(int x, int y)
{
cout<<"Area of Rectangle is = "<<x*y<<endl;
}

void Desc()
{
cout<<"This Derived class Rectangle from Base Class Quad"<<endl;
}

};

class Creator
{
public:
Quad* Creator::Create(int id)
{
if (id==2)
return new Square;
else
return new Rectangle;
}
};

int main(int argc, char* argv[])
{
Creator mcreator;
if (argc<=2)
{
Square *square = reinterpret_cast<Square*>(mcreator.Create(2));
square->Area(3);
square->Desc();
}
else
{
Rectangle *rectangle =
reinterpret_cast<Rectangle*>(mcreator.Create(1));
rectangle->Area(3,4);
rectangle->Desc();
}

return 0;
}


Are you thinking of
- "Abstract" Factory design
http://en.wikipedia.org/wiki/Abstract_factory_pattern
or
- Factory "method" design
http://en.wikipedia.org/wiki/Factory_method_pattern
Which seems more likely from your code
1. Make Quad::Area and Quad::Desc virutal

once you make this virtual you can remvoe reintrepret cast . then this
desing will be factory pattern and your factory method is Creator .

2. If you want to prevent creation of Square and Retangle
outside the factory, make their constructor protected and declare
Creator as a friend class
3. Consider using enum as parameter of Creator::Create and
perhaps make it static

Michael



sunny wrote:

Does this following program implement the factory design.if not what
are things that i have to change in order to make this following
program to be designed to factory design pattern.

#include<iostream>

using namespace std;

class Quad
{
public:
void Area();
void Desc();
};

class Square : public Quad
{
public:
Square() {};

void Area(int x)
{
cout<<"Area of square is = "<<x*x<<endl;
}

void Desc()
{
cout<<"This Derived class Square from Base Class Quad"<<endl;
}
};

class Rectangle : public Quad
{
public:
Rectangle() {};

void Area(int x, int y)
{
cout<<"Area of Rectangle is = "<<x*y<<endl;
}

void Desc()
{
cout<<"This Derived class Rectangle from Base Class Quad"<<endl;
}

};

class Creator
{
public:
Quad* Creator::Create(int id)
{
if (id==2)
return new Square;
else
return new Rectangle;
}
};

int main(int argc, char* argv[])
{
Creator mcreator;
if (argc<=2)
{
Square *square = reinterpret_cast<Square*>(mcreator.Create(2));

Don''t use reinterpret_cast in cases like this. Use dynamic_cast or
static_cast if you are conserned about speed. Possibly better use
boost::polymorphic_downcast.

I don''t know for certain if your use of reinterpret_cast in this
particular case creates undefined behavior but minor changes to the
objects in questions (introducing MI for instance) definately will.
The whole point of a factory is to hide this kind of detail.

square->Area(3);
square->Desc();
}
else
{
Rectangle *rectangle =
reinterpret_cast<Rectangle*>(mcreator.Create(1));
rectangle->Area(3,4);
rectangle->Desc();
}

Objects created by a factory shouldn''t need to be cast like this. If
you are casting down from creation of the object then the factory is
pointless. Yes, creator is a factory but the objects it is creating
are not polymorphic so it makes no sense. That is the reasoning behind
the other answers you got.


这篇关于以下程序是否实施工厂设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆