简体   繁体   English

你不能没有哪种GoF模式?

[英]Which of the GoF patterns could you not live without?

如果将来您只能使用“ 四人帮”一书中定义的单一规范模式,您会选择哪一个以及出于什么原因?

Not a design pattern, but to me this principle was definitely the most influential on my programming style of the whole book: 不是一种设计模式,但对我而言,这个原则绝对是我整本书编程风格中最具影响力的:

Favor object composition over class inheritance. 在类继承上支持对象组合。

Combined with the other principle "Program to an interface, not an implementation", most design patterns follow naturally. 结合其他原则“程序到接口,而不是实现”,大多数设计模式自然而然地遵循。

Choosing one pattern as the most important is in my opinion impossible. 在我看来,选择一种最重要的模式是不可能的。 They all have their purpose and complement each other. 他们都有自己的目的,相得益彰。 Its like asking if a hammer or a screwdriver is more useful. 就像询问锤子或螺丝刀是否更有用一样。 It depends on the problem, sometimes you have a nail and sometimes a screw. 这取决于问题,有时你有一个钉子,有时你的螺丝。

Abstract Factory Pattern or Template Method Pattern... but mostly Abstract Factory Pattern. 抽象工厂模式或模板方法模式......但主要是抽象工厂模式。 They have just been extremely useful on a lot of situations. 它们在很多情况下都非常有用。

I specially liked the way Abstract Factory allows you to prepare for situations where you know your program may change in the future by following the same rules in a different way. 我特别喜欢抽象工厂允许您通过以不同方式遵循相同规则来准备您将来可能会改变您的程序的情况。

Is mostly like the Abstract Class allows you to define specs "inside" the code, restrain the code to follow that specs while the factory allows you to "choose" among those specs. 大多数情况下,抽象类允许您在代码“内部”定义规范,限制代码遵循该规范,而工厂允许您在这些规范中“选择”。 Simply beautiful. 简单的美丽。

观察者模式

I like the state pattern. 我喜欢国家模式。 It's good for modeling, well, state. 对于建模,好吧,状态都有好处。 For a game project I'm writing, I use state hierarchies to keep track of game state. 对于我正在编写的游戏项目,我使用状态层次结构来跟踪游戏状态。 It's simple but very flexible. 它很简单但非常灵活。

I'd say the Decorator pattern . 我会说装饰者模式 Mainly because it's used so heavily in frameworks already. 主要是因为它已经在框架中大量使用。

For example, if you've ever written code like this: 例如,如果您编写过这样的代码:

FileStream file = new FileStream("file.txt");
GZipStream compression = new GZipStream(file);
TextReader reader = new TextReader(compression);
reader.ReadAll();

Then you've used the Decorator pattern. 然后你使用了Decorator模式。

If I had to choose one, I'd go with the MCEscher picture on the front cover, to put on the wall. 如果我必须选择一个,我会选择封面上的MCEscher图片,放在墙上。 ;) ;)

This is a difficult question because, I have found that when I was learning patterns, I was implementing a lot of systems in a very simillar manner to many of the design patterns (particulary the more fundamental ones such as the creational patterns, or state, and strategy and template). 这是一个棘手的问题,因为我发现当我学习模式时,我正在以非常类似的方式为许多设计模式实现许多系统(特别是更基本的模式,如创建模式或状态,和战略和模板)。

Now that I know about the GoF, I cant seperate out in my mind what patterns I wouldnt have discovered naturaly, and to me that is what makes a good pattern. 现在我知道了GoF,我无法在脑海中分离出我自然不会发现的模式,对我而言,这就是形成良好模式的模式。 A good pattern is one that is so natural that you write code using the pattern because its a good solution. 一个好的模式很自然,你可以使用模式编写代码,因为它是一个很好的解决方案。

Patterns just give us a way to talk about our strategies for composing solutions in a common dialect. 模式只是让我们谈谈我们用普通方言编写解决方案的策略。 I know I didn't answer your question so I applogize. 我知道我没有回答你的问题所以我applogize。

I like the Visitor pattern. 我喜欢访客模式。 More than once, I had a design problem and couldn't really solve it, until I finally found that the Visitor pattern was the answer. 不止一次,我遇到了设计问题并无法解决问题,直到我终于发现访客模式才是答案。

You can add any functionality to a class without having to change it! 您可以向类添加任何功能,而无需更改它! Just put an accept() function in there. 只需在其中放入一个accept()函数。

Listener or Observer pattern. 监听器或观察者模式。

Can't imagine my life polling for stuff to check if it happened. 无法想象我的生活中会检查是否发生了事情。

Others: 其他:

  1. Adaptor (this is very useful if you don't want to touch the code that works but you don't have the time to understand how, or you just don't know) 适配器 (如果您不想触摸有效的代码但是您没有时间了解如何,或者您只是不知道),这非常有用
  2. State
  3. Strategy 战略

I would have to say the Singleton Pattern. 我不得不说Singleton模式。 How many times you need that one instance of a class that you use over and over again! 你需要多少次一次又一次地使用一个类的实例! It may be considered an Anti-Pattern but it's one that I use frequently. 它可能被认为是反模式,但它是我经常使用的模式。

Again the Abstract Factory Pattern is also one that I use all the time and the up front development has saved me hours of re-factoring later on when we need to implement a new set of requirements. 抽象工厂模式也是我一直使用的模式 ,并且当我们需要实现一组新的需求时,前期开发为我节省了数小时的重新分解。

The Behavioral Patterns : 行为模式

  • State when my business logic is distribued; 陈述我的业务逻辑分配时的状态;
  • Strategy when the context change the algorithm; 上下文改变算法时的策略 ;
  • Iterator when I read a collection of objects; 读取对象集合时的迭代器 ;

One I can't live without is Iterator. 我不能没有的是迭代者。 I use it daily. 我每天都用它。 I can hardly imagine life without it :-) My second most used is Decorator/Wrapper. 我很难想象没有它的生活:-)我最常用的是Decorator / Wrapper。

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

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