本文介绍了以下程序是否实施工厂设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这个以下程序是否实现了工厂设计。如果不是什么 是我必须更改的内容才能实现以下目标 程序设计为工厂设计模式。 #include< iostream> 解决方案 你在考虑 - " 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 你在想 - "摘要"工厂设计 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。 在这种情况下不要使用reinterpret_cast。如果您对速度有所了解,请使用dynamic_cast或 static_cast。可能更好用 boost :: polymorphic_downcast。 我不确定你是否使用了reinterpret_cast这个 特定情况会产生未定义的行为,但对问题中的 对象进行微小更改(例如引入MI)肯定会。 工厂的全部意义是隐藏这种情况详细信息。 工厂创建的对象不需要像这样生成。如果 你从对象的创建中逐渐减少,那么工厂是无意义的b 。是的,创建者是一个工厂,但它正在创建的对象 不是多态的,所以没有意义。这就是你得到的其他答案背后的理由 Does this following program implement the factory design.if not whatare things that i have to change in order to make this followingprogram to be designed to factory design pattern. #include<iostream> 解决方案 Are you thinking of- "Abstract" Factory design http://en.wikipedia.org/wiki/Abstract_factory_patternor- Factory "method" design http://en.wikipedia.org/wiki/Factory_method_patternWhich seems more likely from your code1. Make Quad::Area and Quad::Desc virutal2. If you want to prevent creation of Square and Retangleoutside the factory, make their constructor protected and declareCreator as a friend class3. Consider using enum as parameter of Creator::Create andperhaps make it static Michael Are you thinking of - "Abstract" Factory design http://en.wikipedia.org/wiki/Abstract_factory_patternor - 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 virutalonce you make this virtual you can remvoe reintrepret cast . then thisdesing will be factory pattern and your factory method is Creator . Don''t use reinterpret_cast in cases like this. Use dynamic_cast orstatic_cast if you are conserned about speed. Possibly better useboost::polymorphic_downcast. I don''t know for certain if your use of reinterpret_cast in thisparticular case creates undefined behavior but minor changes to theobjects in questions (introducing MI for instance) definately will.The whole point of a factory is to hide this kind of detail. Objects created by a factory shouldn''t need to be cast like this. Ifyou are casting down from creation of the object then the factory ispointless. Yes, creator is a factory but the objects it is creatingare not polymorphic so it makes no sense. That is the reasoning behindthe other answers you got. 这篇关于以下程序是否实施工厂设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-21 10:03