简体   繁体   English

Gof设计模式:在什么情况下的适用性

[英]Gof Design patterns: applicability in what scenarios

I am trying to understand design patterns and I have started with GoF design patterns and I found this question in SO 我试图了解设计模式,因此我开始使用GoF设计模式, 因此在SO中发现了这个问题

I also understand there there are other categories of design patterns like: J2EE design patterns, Patterns of Enterprise Application architecture. 我还了解还有其他类型的设计模式,例如:J2EE设计模式,企业应用程序体系结构模式。

My doubt is: 我的疑问是:

Does GoF design pattern be applied to "standalone" kind of applications? GoF设计模式是否适用于“独立”类型的应用程序? I have difficulty in understanding in how can GoF design patterns be applied to, say remote look up in an distributed application. 我很难理解如何将GoF设计模式应用于分布式应用程序中的远程查找。

Can anyone please help me understand what is the real situations / kind of applications where GoF design patterns can be applied. 任何人都可以帮助我了解可以应用GoF设计模式的实际情况/类型的应用程序。

The GoF patterns can be applied to any system design, If you are looking for concrete examples, then you are thinking about them completely wrong(no offense intended at all). GoF模式可以应用于任何系统设计,如果您正在寻找具体的示例,那么您正在考虑它们是完全错误的(根本没有冒犯的意图)。 It is common at first to think that way. 一开始以这种方式思考是很常见的。

I could say for example, starting with what is arguably the simplest and most common, a Poker Game that only ever has one deck might be a candidate for the Singleton Pattern . 我可以说,例如,从最简单和最常见的情况开始,只有一个套牌的扑克游戏可能是Singleton Pattern的候选人。 The Singleton ensures that there is only ever one instantiation of a class. Singleton确保只有一个类的实例。 There are a thousand different scenarios where this may be the case. 在千种不同的情况下,可能会是这种情况。

The same Poker Game might likely use the Visitor Pattern to evaluate each hand at the table. 相同的扑克游戏可能会使用“ 访客模式”来评估桌上的每手牌。 The Visitor is used to Iterate over collections of objects and perform just about any kind of operation on an individual object or composite. 访客用于遍历对象集合,并对单个对象或复合对象执行几乎任何类型的操作。

At the same time you might say that a Hand is a Composite made up of individual cards which by themselves could be considered a leaf object of that Composite type Hand; 同时,您可能会说一手牌是由多个独立牌组成的合成牌,它们本身可以被视为该合成牌型牌的叶子对象; in which case the Composite Pattern in conjunction with the Visitor would be used (they often are used together) with the Iterator Pattern . 在这种情况下,将与访问者结合使用组合模式 (通常将它们与迭代器模式一起使用)。

The Observer Pattern may be working at the same time, as each player would be registered with the dealer who is essentially driving the game. 观察者模式可能同时工作,因为每个玩家都将向本质上是在驾驶游戏的发牌人注册。 If the game has AI players, the Strategy Pattern could be used to swap out algorithms used to drive the AI players decisions based on the ever changing dynamics of the game. 如果游戏中有AI玩家,则策略模式可用于交换基于不断变化的游戏动态来驱动AI玩家决策的算法。 Actually, as I re-read that, I should have mentioned the State Pattern here as well, State Machines are typically used for AI systems. 实际上,当我重新阅读时,我也应该在这里提到状态模式 ,状态机通常用于AI系统。

The Decorator Pattern could be used to add unique behaviors to the individual AI players, or to change the way the graphics are drawn, or diversity of background noises or both and more, at run-time. 装饰器模式可用于在运行时为单个AI播放器添加独特的行为,或更改图形的绘制方式或背景噪声的多样性,或两者兼而有之。 I probably shouldn't forget to mention FlyWeight in that context as well. 在这种情况下,我可能不应该忘记提及FlyWeight FlyWeight Decorators are really cool. FlyWeight装饰器真的很棒。

Patterns, not just GoF, are a philosophy of design, with the intent to solve Anti-Patterns that are typical pitfalls of any design process. 模式(不仅仅是GoF)是一种设计理念,旨在解决反模式,这是任何设计过程中的典型陷阱。 You can be creative with them to the limit of your imagination. 您可以在他们的想象力范围内发挥创造力。

Patterns are just the opposite of concrete, in that they promote the cardinal principles of OOD, like programming to Interfaces rather than implementation, or promoting lower level dependencies to the control of higher level abstractions, or the Open Closed Principle (Open for Extension; Closed for Modification) like the Pandora's box analogy. 模式与具体情况恰恰相反,它们促进了OOD的基本原理,例如对接口进行编程而不是对实现进行编程,或者将较低级别的依赖项促进对高层抽象的控制,或者开放封闭原则(开放扩展;封闭进行修改),例如Pandora的盒子类比。

While it is best, in my opinion, to leave Pattern implementation as loosely coupled as the code it helps us write, there is solid convention to be considered with their use. 在我看来,最好让Pattern实现与它有助于我们编写的代码保持松散耦合,但在使用它们时要考虑一个可靠的约定。 Each Pattern was developed with a typical motivation and intent. 每种模式都有典型的动机和意图。 They are also conveniently categorized to reflect this, as there was an original Anti-Pattern(and still is) that motivated the design of each one, as well as typical combinations of certain patterns that work quite naturally together. 它们也可以方便地归类,以反映这一点,因为有一个原始的“反模式”(现在仍然是)激发了每个人的设计,并且某些模式的典型组合非常自然地协同工作。

The way in which one approaches the concept, or better, the philosophy of OOD, will have a direct influence on that individuals ability to implement Patterns effectively. 一种方法来处理OOD的概念或更好的哲学的方式,将直接影响个人有效实施模式的能力。 Incidentally, it does have a requisite AHH HAA! 顺便说一句,它确实具有必需的AHH HAA! moment, which is the only real contingency tied to their effective, and creative use. 时刻,这是与其有效和创造性使用相关的唯一实际意外情况。

有很多示例可以应用GoF设计模式,开始时您可以检查以下站点: 设计模式案例源码制作用Java实现的设计模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM