#### Archived

This topic is now archived and is closed to further replies.

# Design Patterns

This topic is 5174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m curious as to what design patterns you have found useful to apply in your game engine. And what patterns should a game programmer look into and learn?

##### Share on other sites
abstract factory pattern
singleton pattern
template pattern

##### Share on other sites
*checks the contents page from GoF)

Singleton
Pluggable factory (and, thus, factory)
Template
Visitor
Composite
Memento
Observer
State/Strategy

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4
ry. .ibu cy. .abu ry. dy. "sy. .ubu py. .ebu ry. py. .ibu gy." fy. .ibu ny. .ebu

##### Share on other sites
client/server

Anything else is waste of time or poor programming skills.

Edit:
Also insert fancyname et al + pattern and scheme/ocaml/lisp and the size of your e-penis will turn exponential.

[edited by - Captian Goatse on October 11, 2003 1:05:14 PM]

##### Share on other sites
quote:
Original post by Captian Goatse
client/server

Anything else is waste of time or poor programming skills.

Thats a rather narrowminded and reactionary view.

##### Share on other sites
I fancy factory and interface a whole lot. Typically, interfaces will be expressed as pure virtual classes in header files; concrete implementations go into one source file per class, including the class declaration of the concrete class at the top of the .cpp file.

Others that are nice include asynchronous queuing patterns (FIFO, workers and callbacks), systems/services, visitors (great for traversing scene graphs!), model/view/controller, and generic property trees.

##### Share on other sites
Personally I use the Abstract Factory (very cool and easy and useful), the all beloved Singleton, I don''t know if "Interface" is actually a design pattern, but I use it too.
I haven''t gotten around yet on using Typelists with the Abstract Factory as described in "Modern C++ Design", it takes some time to grasp what is going on there. Template metaprogramming can make your head smoke

What''s a template pattern? Just using templates or what? I''d like to see some code that enlightens me about the Visitor pattern.

Baraclese

##### Share on other sites
quote:
Original post by Captian Goatse
client/server

That''s not a design pattern...

quote:

Anything else is waste of time or poor programming skills.

I think you''ve misunderstood what a design pattern is. How exactly does using a common solution to a frequent problem make you a poor programmer?

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4
ry. .ibu cy. .abu ry. dy. "sy. .ubu py. .ebu ry. py. .ibu gy." fy. .ibu ny. .ebu

##### Share on other sites
Captian Goatse you obviously either misinterpreted exactly what a design pattern is, or your a complete idiot.

Any object orientated programmer worth his salt knows and uses tried and tested design patterns.

http://www.amazon.com/exec/obidos/tg/detail/-/0201633612/qid=1065914435/sr=8-2/ref=sr_8_2/102-3614532-0849762?v=glance&s=books&n=507846

You could definitely learn from it.

##### Share on other sites
I have the book Design Patterns by the father of the singleton pattern. Anyhow, I''ve yet to see where design patterns actually help me.

I''ve not met a language that would force me to use singleton and if there is a language that is so incapable that I can''t brute force, I won''t use the language. As simple as that.

"Design patterns" are waste of time. It is cool if you get excited with them and they might be a nice hobby, but they are nothing practical.

Ok, I might have accidentally used some other design pattern, but common sense people!!

Common sense and pragmatic programming; no time wasted.

And please don''t call me an idiot if I don''t follow the modern trends.

##### Share on other sites
1. CG you are an idiot
2. why have you not been banned for that nick already?
3. design patterns; sure you don''t use them, but I''ll wager you''ve implemented them independantaly without even knowing it. thats the advantage; reading a book and discovering ''ahh, that would be an excellent solution to this problem!''

So tie one hand behind your back. Whatever.

##### Share on other sites
Yeah, I do completely agree with captain goatse! For instance, if a language prevents me from using goto''s and forces me to use that "while loop" thingie, then it''s just a poorly designed and useless language. I mean, "while loops" are all nice and fancy, and you can use them all the way you want - especially if the current industry trends are about using them - for fun, but don''t forget the real stuff at machine level gets done using goto!

And seriously, what does that sissy academical computer science research bring us?

New algorithms? Bah! Any half-decent programmer could code his way out of any situation! Why would it matter if the "acamedical" soution is "O(log n)" when a good coder can optimize his own brute-force version in assembler?

New ways of developing software? Who gives a damn about all that new stuff anyway? O''Caml, Lisp, Scheme, and all those new ideas and paradigms... like variables, functions, liked lists, stacks, queues, objects, polymorphism, curryfied functions, meta-programming, lambda calculus... are waste of time to use!

Of course, I might have used some of these accidentally, but I only code using common sense.

Captain Goatse, only one word can truly express my feelings toward you:

DUH!

[ end scarcastic flame ]

ToohrVyk

##### Share on other sites
I think the most important thing after learning the design patterns is to be able to think of your own design and work it to what best suits you and to flexibly implement design patterns, not to just hammer out what you saw in a book everytime.

##### Share on other sites
Captian Goatse: "I have the book Design Patterns by the father of the singleton pattern."

I'd like to know the name of that book

And the Singleton is:
"Ensure a class only has one instance, and provide a global point of access to it."

So if you don't need to ensure that there's only one instance of your class, and you don't need global access to it: DON'T USE IT.

It's as simple as that.

As you said, you probably implemented some patterns without knowing it. Well congratulations! You just reinvented the wheel!
How much time did you spend "inventing solutions" when they are already tested and presented in the nice book Design Patterns, Elements of Reusable Object-Oriented Software ?

To the OP, I've used:
Factory
Prototype
Singleton
State
Flyweight
Command
Observer

Baraclese: Template Method, as defined by GOF is:
"Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure"
Hope that shed some light into it

}-- Programmer/Gamer/Dreamer --{

[edited by - Seriema on October 12, 2003 9:16:11 AM]

##### Share on other sites
quote:
Original post by Seriema
Captian Goatse: "I have the book Design Patterns by the father of the singleton pattern."

I''d like to know the name of that book

And the Singleton is:
"Ensure a class only has one instance, and provide a global point of access to it."

So if you don''t need to ensure that there''s only one instance of your class, and you don''t need global access to it: DON''T USE IT.

It''s as simple as that.

As you said, you probably implemented some patterns without knowing it. Well congratulations! You just reinvented the wheel!
How much time did you spend "inventing solutions" when they are already tested and presented in the nice book Design Patterns, Elements of Reusable Object-Oriented Software ?

[edited by - Seriema on October 12, 2003 9:16:11 AM]

Seriously, do you even consider yourself as a programmer if you haven''t read Design Patterns by Gamma et al? i see that you are intentionally being half-assed dum-ass so I can turn thread into crapfest.

That book is mandatory like in every 101 programming class in even half decent college/universtity. Every post 95 book on design refers to that one.

I never invented any solutions. They come naturally. I''m sorry if you do not have enough common sense to see what is best and what is not. 90% of design patterns is common sense. It is just that some people take it too seriously. Programming is a creative process. You can do it according some exact pattern. Sorry.

What comes to singleton? It is most worthless creation of computer science ever. Now, if you''d have read the book you were "namedropping" carefully enough, you''d probably notice, that when you are doing everything "right" you should not have any need for global instance, ever. Period. I agree that single instance of a class is useful, but then again the same book gives you another form to do it, which in my opinion is much more elegant. You may consider rewising the book, again if you missed it.

Still my point holds valid, there has never been a single case, where it would have been must to employ a design pattern. It is up to the programmer isn''t it.

If you can make your way through any other way, then you should probably re-consider your career or hobby.

Design patterns are worthless waste of time.

##### Share on other sites
quote:
Original post by ToohrVyk BLAAAAAAAAAAHRRRRRGggggHH

Oh wait, you are trolling.
You still never proved me wrong.
Prove me one instance where one specific design pattern is a must. Spoiler: There isn't one.

I have one paradigm for you and it is the one I have found useful. Occam's razor.

It slices, it dices and it removes unnecesary superfluous crap from relatively simple entities. Oh yeah, it also works.

If you mention singleton and global in same sentence you should be shot and sodomised in no particular order. Global is global. Singleton with global access is just global in disquise. Basically even passing a variable to a function is a pattern. I'm not talking about that. I'm talking about people inventing strange concepts and giving them grand names.

Until you can prove me wrong please retain from trolling this thread.

Edit:
There is one "design pattern" that covers programming from the beginning to the end and that is XP. Other than that I have not seen anything else worth notion.

[edited by - Captian Goatse on October 12, 2003 9:57:32 AM]

##### Share on other sites
quote:
Original post by Captian Goatse
Seriously, do you even consider yourself as a programmer if you haven''t read Design Patterns by Gamma et al?

Yes. I consider myself a better programmer if I have names for the structures that arise in my code, though.

quote:
That book is mandatory like in every 101 programming class in even half decent college/universtity. Every post 95 book on design refers to that one.

Yes, because it''s an effective way to describe a particular solution by name, without having to explain all the details. I can tell someone to use a ''factory'' technique, and they know what it means, without my having to say ''a static member of the class which instantiates an object of that class.''

quote:

I never invented any solutions.

Oh, so all your code is cut-and-paste from the web, then?

quote:

They come naturally.

As they should.

quote:
I''m sorry if you do not have enough common sense to see what is best and what is not. 90% of design patterns is common sense.

Of course; as is knowing when each pattern is the right one to use. For example, is it best to derive from two base classes, or to composite? It completely depends on the situation.

quote:
It is just that some people take it too seriously.

I''m sorry, what the hell are you talking about? Assigning names to common solutions is ''taking it too seriously?'' Evidently I''d better stop calling ''matrices'' and ''vectors'' by their names, and start calling them ''set of numbers, conceptually arranged in an NxN grid'' or ''projection of a line along each axis of the space the line lies within.''

quote:
Programming is a creative process.

To some extent...

quote:
You can do it according some exact pattern. Sorry.

If you didn''t typo, then I agree; otherwise, you''re talking bullshit. Of *course* you can do it to an exact pattern. At the very least, all programs contain functions, and that''s a pattern. The ''function'' pattern. As are while loops, structures, if-else blocks...

quote:

What comes to singleton?

Buh?

quote:
It is most worthless creation of computer science ever.

Uh, no. The most worthless creation of computer science ever would have to be COBOL.

quote:
Now, if you''d have read the book you were "namedropping" carefully enough, you''d probably notice, that when you are doing everything "right" you should not have any need for global instance, ever. Period. I agree that single instance of a class is useful, but then again the same book gives you another form to do it, which in my opinion is much more elegant. You may consider rewising the book, again if you missed it.

Care to tell us which pattern you''re referring to? Because the only patterns I can see in the ''Related Patterns'' of that section are Factory, Builder, and Prototype, none of which do the same thing.

quote:

Still my point holds valid, there has never been a single case, where it would have been must to employ a design pattern. It is up to the programmer isn''t it.

It''s up to the programmer to pick a solution from the set that they know about, applying previous experiences or coming up with something new. It''s easier to say ''Prototype pattern'' than ''the way we did it 3 projects ago.''

quote:
If you can make your way through any other way, then you should probably re-consider your career or hobby.

Again, I say... buh?

quote:
Design patterns are worthless waste of time.

Write a commercial product which doesn''t exhibit any of them, then.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4
ry. .ibu cy. .abu ry. dy. "sy. .ubu py. .ebu ry. py. .ibu gy." fy. .ibu ny. .ebu

##### Share on other sites
quote:
Original post by Captian Goatse
Prove me one instance where one specific design pattern is a must. Spoiler: There isn't one.

Correct. However, there are plenty of situations where a specific design pattern is a desirable.

quote:

If you mention singleton and global in same sentence you should be shot and sodomised in no particular order. Global is global. Singleton with global access is just global in disquise.

Nope. Singleton imposes the limitation that you can only ever have one instance of the object in existence at a time. Globals don't do that. Global access to the object is a side-effect of the pattern. It's very simple to write a singleton which doesn't exhibit global access - it's a bit like a mutex, you try to create a singleton object and if there's another one in existence somewhere else, your creation fails. No guarantee you've got access to the other object.

quote:
Basically even passing a variable to a function is a pattern. I'm not talking about that. I'm talking about people inventing strange concepts and giving them grand names.

What's so strange about factory functions, composites, proxies, facades, or any of the rest of them? They're common solutions to common problems.

quote:

There is one "design pattern" that covers programming from the beginning to the end and that is XP. Other than that I have not seen anything else worth notion.

OK, now I *know* you don't know what design patterns are. XP is a development paradigm, not a design pattern. Go and read the first section of the 'Gang of Four' book, the one you claim is so worthless. You might be surprised.

Superpig
- saving pigs from untimely fates, and when he's not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4
ry. .ibu cy. .abu ry. dy. "sy. .ubu py. .ebu ry. py. .ibu gy." fy. .ibu ny. .ebu

[edited by - Superpig on October 12, 2003 10:10:51 AM]

##### Share on other sites
I never intended to prove you wrong, because everything you said that wasn''t true was only a personal opinion.

There is almost always more than one way of solving a particular problem. Deciding that design patterns are useless, only because there exists an alternate way of doing things, is a matter of personal opinion.

SO, if you can show me that design patterns (in general) either have an inherent flaw, or don''t allow solutions that would be possible without them, I''ll accept your point.

On the other hand, if you keep arguing that "What''s not neccessary, is useless", I''ll keep pointing you out to the fact that many of these things are not necessary either:
- functions
- pointers
- loops
- conditionals

ToohrVyk

##### Share on other sites
quote:
Original post by superpig

Singleton imposes the limitation that you can only ever have one instance of the object in existence at a time. Globals don''t do that. Global access to the object is a side-effect of the pattern.

However, in most cases I see these 1337 people using singleton wrong. People especially on these boards love to give singleton a global access. In this situation singleton has done nothing, but good.

quote:

It''s very simple to write a singleton which doesn''t exhibit global access - it''s a bit like a mutex, you try to create a singleton object and if there''s another one in existence somewhere else, your creation fails. No guarantee you''ve got access to the other object.

But why would you want to create a singleton that is not shared with all the objects of same type singleton within the object? In that case you''ve done wrong already. You should have no objects that have one common singleton. Also you could make simple check in the constructor to see whether or not it should be created. Static code would do here what it is meant to do. Thus singleton is not what you want here.

quote:

OK, now I *know* you don''t know what design patterns are. XP is a development paradigm, not a design pattern. Go and read the first section of the ''Gang of Four'' book, the one you claim is so worthless. You might be surprised.

I see. Don''t get arrogant with me. Thanks. I know perfectly well what design patterns are. That was an attempt to bring me down and personally offend me. I have not attempted to insult you prior to this: Your e-penis is smaller than mine.

Again, a supporter of flawed paradigm has demonstrated how common sense beats the hell out of academical retardation.

Academical research is often good, but these grand philosophical design patterns are a waste of effort and work. If it works, looks and is simple, it is good.

Also whoever said try to program without functions:
Functions are part of modern computer languages. Singletons are not. Singletons are artificial entities made to "ease" one''s life. However, oftentimes they are used wrongly and unjustly.

Besides, functions make things simple. That is what I have been saying all the time.

Goto on the otherhand does not make things simple, but starts vicious cycle.

##### Share on other sites
Well, lets see. I have used these recently:

Abstract Factory
Decorator
Composite
Chain of Command
Visitor

CG, I would really like to see how you make a GUI without using any patterns. I didn''t know them at the time, but I still ended up creating my own compsite classes.

##### Share on other sites
CG claims he''s uberleet programmer, yet have we seen anything, code or a game from him? I haven''t. Superpig has shown us the goods in the enginuity series so I conclude Superpig know something and CG is as brilliant as his nick.

##### Share on other sites
quote:
Original post by Captian Goatse
However, in most cases I see these 1337 people using singleton wrong.

That''s not the fault of the Singleton...

quote:
People especially on these boards love to give singleton a global access. In this situation singleton has done nothing, but good.

Uhm, I don''t quite get your last sentence there... but it''s a question of conceptual modelling. A given object, for example a ''manager,'' *should* only have one instance, and *should* be available globally. In such a situation, whatever you do will be a singleton; your implementation just might be somewhat unusual. Whether or not something is a singleton is a characteristic of the system, and depends on whether it fulfills the same specification as a singleton. It makes no difference to the outside user if you''re using a generalised reference-counting mechanism to ensure a single instance, or whatever... the behaviour of the system is that of a singleton.

quote:

But why would you want to create a singleton that is not shared with all the objects of same type singleton within the object?

I never said you''d want to, I just said it was possible. I have a feeling that in such a situation, it wouldn''t be called a Singleton anyway.

quote:
Also you could make simple check in the constructor to see whether or not it should be created.

In fact, you couldn''t (at least, not without using a second pattern like the Factory pattern or the State pattern). Constructors can''t return values, and can''t affect creation; you''d need a factory function to do it, which, incidentally, is exactly how many Singletons are implemented.

quote:

Don''t get arrogant with me.

This from the person who said "If you can make your way through any other way, then you should probably re-consider your career or hobby; Design patterns are worthless waste of time."

quote:
That was an attempt to bring me down and personally offend me.

No, it was an attempt to bring this discussion some semblance of reasonability. There''s no point debating design patterns with you if your definition of them is flawed.

quote:
Again, a supporter of flawed paradigm has demonstrated how common sense beats the hell out of academical retardation.

Academical research is often good, but these grand philosophical design patterns are a waste of effort and work. If it works, looks and is simple, it is good.

Where did you get the ''academic'' aspect of this from? I know you stated that the GoF book can be found on most university CS courses, but it''s an industry book - it''s what software engineers actually use.

Design Patterns aren''t meant to be ''grand'' or ''philosophical.'' They''re meant to be a pre-thought out set of solutions to problems you might encounter. What''s the problem with that?

quote:
Also whoever said try to program without functions:
Functions are part of modern computer languages. Singletons are not.

Actually, I''ve been told of at least one language where they *are* part of the language, but irritatingly the name escapes me.

quote:
Singletons are artificial entities made to "ease" one''s life. However, oftentimes they are used wrongly and unjustly.

Looking at where this thread has gone, is this a question of the sensibility of Singletons, or of design patterns as a whole? Singletons make a good example because most people have come across them and know how they work, but there''s a whole load more patterns than just the Singleton...

quote:
Besides, functions make things simple. That is what I have been saying all the time.

So do Singletons. Rather than typing ''gGlobalObjectInstance'' I type ''ObjectClass::GetInstance()'', and I also get the additional guarantee that there will never be more than one instance at a given time. Ignoring the small overhead incurred by calling the GetInstance() function, how is that worse than a global object?

quote:

Goto on the otherhand does not make things simple, but starts vicious cycle.

It *can* do. A drop-out from a routine in case of an error? Which is better, this:
if(FAILED(CallFunctionA())goto done;if(FAILED(CallFunctionB())goto done;if(FAILED(CallFunctionC())goto done;done:Cleanup();

or this:
do {if(FAILED(CallFunctionA())break;if(FAILED(CallFunctionB())break;if(FAILED(CallFunctionC())break;} while(0);Cleanup();

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4
ry. .ibu cy. .abu ry. dy. "sy. .ubu py. .ebu ry. py. .ibu gy." fy. .ibu ny. .ebu

##### Share on other sites
quote:
Original post by sagwagon
CG claims he''s uberleet programmer, yet have we seen anything, code or a game from him? I haven''t. Superpig has shown us the goods in the enginuity series so I conclude Superpig know something and CG is as brilliant as his nick.

No dipshit. Read. Comprehend. Post. I claim that with common sense there is no need for "design patterns" and they are waste of time.

##### Share on other sites
quote:
Original post by Captian Goatse
...

"it is a tale Told by an idiot, full of sound and fury, Signifying nothing."

• ### Forum Statistics

• Total Topics
628706
• Total Posts
2984309

• 23
• 10
• 9
• 13
• 13