本文介绍了选择创作模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好朋友, i正在研究c ++中的设计模式,我读过非常有趣的创作模式文章Design Patterns 1 of 3 - Creational Design Patterns通过 Kanasz Robert这是一篇非常好的文章.. 我自己在酒店登记系统项目的初始阶段需要更详细的了解这个领域。 任何身体都可以建议我在哪个区域我需要更多地关注创作模式。抽象工厂,生成器,工厂方法,原型和单件。 提前谢谢。 我尝试了什么: 我正在抽象工厂Hello friends,i am working on Design patterns in c++ , i have read very interesting article on creation patterns "Design Patterns 1 of 3 - Creational Design Patterns" byKanasz Robert it is very good article..my self in intial phase of project on hotel checkin system need more detailed understanding on this area.can any body suggest me on which area i have to focus more in creational patterns.i.e; Abstract factory, Builder, Factory Methods, Prototype and singleton.Thank you in advance.What I have tried:I am working on abstract factory intially推荐答案对于编写模式书籍的人来说,生活中令人遗憾的是,好的C ++通常不需要创作模式。搜索从上面进行参数化,你会看到最好的C ++ [1]使用的模式。根据需要自动创建对象,并确保只有创建对象的代码片段知道其具体类是什么 - 其他所有内容都使用对接口的引用。 另外需要考虑的是,在模式运动开始时提倡的一些模式(特别是单身)实际上是反模式,因为它们是引入内存泄漏,竞争条件和谁的好方法知道如果你在多线程代码中使用它们还有什么。其他模式如果过度应用(例如构建器)可能会使您的代码创建对象不可读。如果您需要根据从各种不同范围收集的信息创建对象,则Factory和Abstract工厂非常有用。然而,大多数程序不需要设计的复杂性,并且过度使用这些模式可能会使您的代码看起来像90年代早期最糟糕的COM-mania过度,您花费3/4的时间编写样板来支持模式奖励很少。 所以最好的办法是找出你的程序需要的实体,创建代表它们的类,看看是否有任何好处。这种时尚不是在堆栈上敲打它并使用对接口的引用。如果是这种情况那么看一本关于模式的书,看看是否适合。 [1]至少使用OO编写的C ++样式,C ++中的函数式编程看起来有点不同。It's a sad fact of life for people writing patterns books that good C++ doesn't need creational patterns that often. Do a search for "parametrise from above" and you'll see the pattern that most good C++ [1] uses. Create objects automatically as you need them and make sure that only the piece of code that creates the object knows what its concrete class is - everything else uses a reference to an interface.Another thing to consider is that some patterns advocated at the beginning of the patterns movement (especially singleton) are actually anti-patterns because they're a great way of introducing memory leaks, race conditions and who knows what else if you use them in multi-threaded code. Other patterns if over applied (e.g. builder) can make your code to create objects unreadable. Factory and Abstract factory can be very useful if you need to create objects based on information gathered from a variety of different scopes. However most programs don't need that complexity of design and overuse of these patterns can make your code look like the worst excesses of COM-mania from the early 90s where you spend 3/4 of your time writing boiler plate to support the pattern for very little reward.So the best thing to do is identify the entities your program's going to need, make classes to represent them and see if any benefit from being created in a fashion that's not "bung it on the stack and use a reference to an interface." If that's the case then look at a book on patterns and see if anything fits.[1] At least C++ written using an OO style, functional programming in C++ looks a bit different.想一想:如果其中一个模式是一个完美的模式,那么你不觉得所有其他创作模式都会被遗忘,并且永远不会出现在现代出版物和百科全书中? 简单的实际推理表明:没有银弹。您应该从项目的体系结构开始,从项目的目标开始关注。这应该会引导你做出很好的选择。此外,谁告诉你在整个项目中你必须使用一个单一的创作设计模式?是什么让你觉得你无法结合这些方法,或者只是为不同的类,层或子系统使用不同的模式? 你没有与我们分享足够的信息为你做出选择。你是需要做出选择的人。 但即使这不是最重要的事情。更重要的是,您必须记住使用一种或另一种设计模式不能成为您的目标之一。决不。它们的发布是为了帮助您决定产品,而不是您的产品应该扮演那些模式的推动者的角色。 Patters,如果有的话,总是接下来。我强烈建议阅读我过去的答案,忘记设计模式回答相关主题:请建议我一些基本的C ++项目与设计模式 [ ^ ]。 如果你读它,你会发现忘记不是我真正的意思(这是一种比喻)。我只是试图解释如何理性地采用这些设计模式。 永远记住,使用这些设计模式不是您的义务。 -SAJust think of that: if one of those patterns would be a "perfect" one to be used everywhere, don't you think that all other creational patterns would be long forgotten and would never appear in modern publications and encyclopedias?That simple practical reasoning suggests: there is no a silver bullet. You should start with the architecture of your project, starting your focus from the goals of your project. That should lead you to a good selection. Besides, who told you that you have to use one single creational design pattern throughout the whole project? What makes you thinking that you cannot combine the approaches, or simply use different patters for different classes, layers or sub-systems?You did not share enough information with us to make a choice for you. You are the one who needs to make the choice.But even that is not the most important thing. More importantly, you have to remember that using one or another design pattern cannot be one of your goals. Never. They are published to help your with the decisions on your product, not that your product should play the role of promoter of those patters. Patters, if any, always come next. I strongly advise to read my past answer, "forget design patterns" answer on a related topic: Please suggest me some basic C++ project with design patters[^].If you read it, you will see that "forget" is not what I really mean (this is a figure of speech). I just try to explain, among other things, how to rationally take those design patterns. Always remember that using those design patterns is not your obligation.—SA 这篇关于选择创作模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 18:20