简体   繁体   中英

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.

If I had to choose one, I'd go with the MCEscher picture on the front cover, to put on the wall. ;)

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 pattern. 模式的模式。 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.

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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