문제

I was reading about builder patters from the http://en.wikibooks.org/wiki/C%2B%2B_Programming/Code/Design_Patterns#Creational_Patterns link and the code below seems to use idea similar to pimpl idiom by having a pointer to pizzaBuilder in the Class Cook as the private member. Is there any overlap between pimp idioms and how it used in designer patterns?

class Cook
{
        public:
                void setPizzaBuilder(PizzaBuilder* pb)
                {
                        m_pizzaBuilder = pb;
                }
                Pizza* getPizza()
                {
                        return m_pizzaBuilder->getPizza();
                }
                void constructPizza()
                {
                        m_pizzaBuilder->createNewPizzaProduct();
                        m_pizzaBuilder->buildDough();
                        m_pizzaBuilder->buildSauce();
                        m_pizzaBuilder->buildTopping();
                }
        private:
                PizzaBuilder* m_pizzaBuilder;
};
도움이 되었습니까?

해결책

No. While language features used (pointers, private fields) may be similar, intended result is completely different.

PIMPL fairly transparent to the user of the class - and that's the point. You don't see CookPimpl used in interface of the Cook - it's hidden in .cpp file, caller can't even do anything meaningful to it.

Builder class on the other hand is self-sufficient, and could be used directly, in Cook class or in PizzaRestaurant.

What is more, PIMPL idiom is C++-specific (adding or removing private member leads to recompilation everywhere, since definitions are copy-pasted), while design patterns strive to be language agnostic.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top