C++ example of implementation composite and visitor pattern together - c++

Now I'm developing a specialized vector editor. Can you tell me where I can see a good example (only C++ language ) of source (not standard book example) implementation composite and visitor pattern together.

Composite + visitor isn't such a popular pair, in most cases you can see composite + iterator ... so you will have to try add them by your own hands, but it shouldn't be hard. In both patterns you have inteface implemented by concrete classes, so you have to fill your composite with visitor ideas
http://sourcemaking.com/design_patterns/composite/cpp/2
http://sourcemaking.com/design_patterns/composite/cpp/1
http://sourcemaking.com/design_patterns/visitor
gl :)

A quick google search gave me these implementation examples of Composite Pattern:
C++ example for Composite Design Pattern
Composite design pattern
Composite design pattern - multiple container classes
Composite Pattern Made as C++ Component

This example source code is based upon the file system example found in the book Pattern Hatching: Design Patterns Applied, by John Vlissides, which uses Composite and Visitor. (The code includes external iterator that is not in the book).
Free slides detailing the example from the book are at Designing with Patterns.

Related

Is there a combination of state and template design patterns?

I was wondering, if there is a way to combine state and template design patterns with each other?
So that you are able to change the possible states by property. For example a testing algorithm with multiple test states and different characteristics of them based on the guidelines of each country.
Does a pattern exists for that purpose?
Does that make sense in any way?
Is it easy to combine them?

C++ Design Pattern library?

What is the most common C++ Design Pattern libraries?
I've read about Loki library in Alexandrescu's book, but looks like it somewhat dead now. Is there something similar out there?
“Design patterns are bug reports against your programming language” --
Peter Norvig
To answer the question why there are not many C++ design pattern libraries, it is useful to know what design patterns were meant to solve in the first place. The classic GoF book states in the preface
Design patterns describe simple and elegant solutions to specific
problems in object-oriented software design.
The 90s style of object-oriented programming relied heavily on using abstract classes as interfaces, with concrete implementation classes deriving from these interfaces. The GoF patterns describe creational, structural and behavioral relationships between objects of different class types. They key element was: encapsulate and parameterize whatever will frequently change. Many of the GoF patterns can also be reformulated using templates, but then the flexibility is constrained to compile-time rather than run-time.
Object-oriented programming makes it very easy to add different concrete implementations of an interface. What OOP has a hard time with is adding new functionality to existing interfaces. The Visitor pattern is the prime example: it is essentially a work-around that relies on an extra level of indirection to allow new algorithms to work on existing data structures.
This is the exact opposite of functional programming: with functional programming it is very easy to add new functions for existing data, but it is much harder to add new data types to which such functions apply. The difficulty in getting extensibility in both functions and types is called the expression problem.
OOP style polymorphism is heavily based on internal polymorphism: the dynamic function dispatch is based on the object's type. Modern C++ also uses external polymorphism where techniques such as type erasure allow run-time flexibility with a static interface. The new std::shared_ptr and boost::any or adobe::poly classes are prime example of these techniques.
A recent ACCU presentation by Tobias Darm showed many examples of transforming the old internally polymporhic GoF patterns to this new style externally polymorphic patterns. The rough idea is to replace abstract classes with a function argument that can take std::function as a parameter. The std::function then controls the polymorphic flexibility from the outside. Many of the GoF patterns can be greatly enhanced in terms of boilerplate this way.
TL;DR: The classic GoF patterns were tailored to solve OOP shortcomings. But OOP is no longer the dominant C++ style. A combination of generic programming (the Standard Library, Boost) and OOP can solve many problems more elegantly, making classic design patterns no longer the go-to solution.
The original definition of a design pattern was a reusable approach to a reoccurring problem that could not be conveniently encapsulated in a library. Thus, the moment you can encapsulate a pattern in a library, it ceases to be a pattern, in my opinion. This has, for instance, largely happened with iterators in C++, as the standard C++ library has a comprehensive framework for implementing iterators now.
I’ve never tried to use Loki, but reading Alexandrescu’s book, I was not persuaded that a library based approach really had much to offer for many patterns.
May seems tautology, but the most common is ... the standard library itself!
It is not -strictly speaking- a "pattern library", but a folder for a number of tools addressing common pattern implementation.
Note that your question is not answerable, being a pattern just a conceptual definition commonly used in a variety of problems. Libraries don't provide patterns, they (can) use patterns (like anybody else can) to provide implementation of specific problem solutions.
Patterns are at an higher abstraction layer than coding.
In an effort to improve code maintainability, re-usability and readability, some researchers (such as GoF, Booch) started examining best practices. They have noticed that there are some patterns adopted by experienced developers to address specific design problems.
As you can see, experience created design patterns. So using design patterns is coding like a specialist. And there is no silver bullet for this.
It is true that some straightforward design patterns such as decorators find support from specific languages. But that's the limit. Domain specific frameworks also guide you to use their interfaces to complete the design pattern decided by the authors of those.
Libraries will only help you understand how design patterns used in that library will facilitate your implementation. It won't even give you a choice to change the design.

An idea about componentization in C++

I have been trying to understand componentization(contrasting to the OOP concepts and also called component oriented programming), in relation to C++.
I have researched regarding this on internet but there were very little structured information available. The windows COM object seems pretty componentized. I have found http://c2.com/cgi/wiki?ComponentDefinition useful.
What could be the best and simple C++ code example, to illustrate the componentization concept?
I have a few high level ideas,like:
I have an English word. A word is made up of several symbols or
characters. Now, each character can be of several types like
alphabetic, numeric, punctuation, whitespace, etc. So, each
alphabet,number,etc. represent the fundamental components, based on
which, a word will be formed and will come into existence.
The word becomes an aggregate component(of symbols), based on which
a sentence will be formed and so on.
The protons, neutrons and electrons are individual agrregate components which form an atom.
Then, how is composite design pattern different from the componentization concept?
Please guide me.
Thanks.
'Composite' as you mentioned is a design pattern. A design pattern is a problem-solution pair applicable during the design of a piece of software.
If I understand your interpretation of the term 'componentization' correctly, it is an architectural princicple which is followed in a higher abstraction level than the design to define the structure of the SW.
(If you are interested in precisely what I mean by architecture, please refer this paper which tries to define the terms design/architecture formally.)
If you get slightly more deep, 'Composite' helps to treat the container and the contents with the same interface. e.g, if you apply 'composite' pattern in your example, you could define an interface 'particle' and then can treat atom/electron/proton/neutron as particles and at the same time the container/content relationship is also maintained. This is a very specific problem-solution pair which can arise only in certain situations.
However, 'Componentization' can be applicable in more broader situations and you are not bothered if there is any container-content relationship in the first place. Even if there is such a relationship between the components, you don't care to treat them with the same interface.

C++ class design patterns in open source projects

I have recently started learning design patterns. I have understood the basics of few patterns. Now I want to get familiar with some real code examples where these patterns are being utilized. Can somebody here please point out to some open source projects where design patterns are being used ? I would like to see how design patterns are being used in real world code.
Following are some of the links. JUnit Cookbook provides brilliant example of quite a few patterns - really worth checking.
JUnit Cook's Tour
Vince Huton's DP
Design PAtterns
Hope that helps.
Well, this is tooting my own horn a bit, but an Open Source project I'm working on makes extensive use of the factory method pattern. I implement the guts of the pattern itself here as a set of templates, define a factory here, register a subclass for that factory here, and use the factory to instantiate objects here.
IMHO, ACE (Adaptive Communication Environment) can be a good example. It also uses c++ templates with design patterns, which is also pragmatical. This framework takes basis of Pattern-Oriented Software Architecture: Patterns for Concurrent and Networked Objects book, which is also a good reference for design patterns, besides GoF
In the book by GOF which you are probably reading, for each Design Pattern they provide a REAL code example, I mean not the sample code, but they actually say in which projects each DP has been used. Those projects are mostly OpenSource. HTH

Famous design patterns that a C++ programmer should know [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What C++ idioms should C++ programmers use?
After reading books like C++ Primer, Effective C++ and TC++PL I want to learn some important design patterns.
So, what are the famous design patterns that every C++ programmer should know?
C++-specific ones: RAII and PIMPL.
The obvious answer is the Gang-Of-Four patterns from the famous book. These are the same patterns that get listed all over the place.
http://en.wikipedia.org/wiki/Design_Patterns
Beyond that, have a look around Martin Fowlers web site...
http://martinfowler.com/
There's a fair bit on there - the "famous" one is probably "dependency injection". Most others are pretty domain specific, though.
"Mixin layers" can be interesting for C++. A template class takes its own base as a template parameter, so that the template can be used to add the same functionality to many different classes, or as a composition method so that various features can be easily included/excluded for a library. The curiously recurring template trick is sometimes used as well (the original base is the final fully-composed class) so that the various mixin layers can do some degree of "reflection", so that intermediate methods can be defined in terms of fully-composed member types etc. Of course it can be a bit prone to unresolvable cyclic dependencies, if you're not careful.
http://portal.acm.org/citation.cfm?id=505148
Note - "the original base" doesn't mean the original base class that's inhereted from as that would cause an illegal inheritance cycle - it's just a template parameter used to refer to, to access the types/constants/etc in the final result and perhaps for metaprogramming reflection techniques.
I honestly don't know at this point if I was confused when I wrote "base", or just chose a confusing word.
Read the Design Patterns: Elements of Reusable Object-Oriented Software.
In no particular order, the Gang of Four patterns I see and use most, are probably the following:
Composite
Template Method
Abstract Factory
Singleton (much hated, but everywhere)
Visitor
Builder
Proxy
I suggest reading Head First Design Patterns. It's a fun read, and you'll learn about a lot of the common design patterns.
The think pattern. It's a silver bullet.