Sign in to follow this  

creational patterns

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

ive just started reading the book "Design Patterns Elements of reusable OO Software"... and finished the "Creational Patterns" section... i think ive understood the concept and im fairly sure i can implement them... abstract factory: let the factory decide how the objects are created object factory: let the objects decide how they are created prototypes: seems to be the same as abstract factory? however im a bit unsure in what case one is better than the other... they all do basicly the same stuff... but i cant get my head around an example where one would be better or more appropriate than another... im doin mostly game/graphics programming so if you could come up with an example from that itd be easier for me... altough any example would be helpful any help?

Share this post


Link to post
Share on other sites
Any factory decides how the objects are created; prototypes are objects kept around to be copied when a similar object is desired.

Apart from rereading the chapter you haven't understood in "Design Patterns", which incidentally include a kind of game as a running example, Wikipedia http://en.wikipedia.org/wiki/Creational_pattern and the C2 Wiki http://c2.com/cgi/wiki?CategoryCreationalPatterns provide good starting points to find decent examples and explanations.

These creational patterns are rarely opposed to one another; objects of the same type can be created in different situations, some creational patterns, like Prototype, can be used to implement others.




Share this post


Link to post
Share on other sites
You'll also find 'Modern C++' - off the top of my head I think the author was Andrei Alexandrescu - to be a good book to learn some good fairly modern methods for implementing patterns in C++.

Good luck!

Share this post


Link to post
Share on other sites
Head First Design Patterns is an excellent book to learn the design patterns from. This book has diagrams, images, interesting examples, side notes, and many more. It's exciting to read and far from boring; check it out at your local Borders. Beware, the examples are in Java, but the writers use as little Java-specific code as possible to keep the ideas applicable across multiple languages. If you want C++, the code looks the same.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dragon_Strike
ive just started reading the book "Design Patterns Elements of reusable OO Software"... and finished the "Creational Patterns" section... i think ive understood the concept and im fairly sure i can implement them...

Creational patterns all are of the form "What is the process by which information is turned into objects". The zeroth creational pattern is a constructor, whereby the user picks the class to create, and provides it with the exact set of creational information it expects. So the complete set of information is (a) the exact class, and (b) the parameters, and the process is one of passing (b) to (a)

Viewed from this perspective, a factory pattern is one where (a) is not provided. Instead, (b) is passed to a factory object, which itself decides what the most appropriate (a) is.

A prototype is a special kind of (b): simply an example object of type (a). When the secretary needs to print up a bunch of payroll forms, she doesn't type them up from a list of requirements from the employee handbook. She just takes a payroll form and sticks it in the copy machine. The one she stuck in is the prototype.

Abstract factory: An object with a lot of factories in it. This is a boring pattern.

Share this post


Link to post
Share on other sites
Quote:
Original post by nobodynews
I'm reading through Modern C++ Design, by Alexandrescu and it does contain a lot of interesting information. Though he probably could have left out the singleton stuff.

The singleton stuff was primarily meant a case study in policy-based parameterization. Don't read it for singletons; read it for the use of templates.

Share this post


Link to post
Share on other sites
Quote:
Original post by nobodynews
I'm reading through Modern C++ Design, by Alexandrescu and it does contain a lot of interesting information. Though he probably could have left out the singleton stuff.


Agreed. I'm reading through that chapter now. I'm glad to have read it, but I'm still not convinced that the singleton pattern is any more useful than I previously thought. Then again, the book is a few years old now, and most everyone was more accepting of the pattern back then.

The worse part, IMO is that he made the small-object allocator from chapter 4 a singleton, and I don't believe that it necessarily requires it or that its even a good idea. The downsides are *slightly* lessened by the fact that the template parameterization creates different small-object allocator singletons for different policies -- eg, a small-object allocator<SingleThreaded> is a different object than a small-object allocator<MultiThreaded> -- but this is still unnecessarily restrictive. For example, what if one wants to have multiple multi-threaded allocators assigned to independently-threaded portions of the application? You can get around this by having, for example, MultiThreadedAI and MultiThreadedGUI policies, but they're probably just duplicates with different names, which doesn't strike me as a better solution than the alternative of simply having the small-object allocator as an object, rather than a singleton.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dragon_Strike
Quote:
Original post by Sneftel
Abstract factory: An object with a lot of factories in it. This is a boring pattern.


why is it a boring pattern?


Heh. Just finished that chapter this morning in my book. One of the shorter chapters.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this