Resolving design patterns with code and UML diagrams-The Creator Pattern

Source: Internet
Author: User

Today, I learned the builder model, one of the more complex models. However, she thought like the abstract factory's producer model.

 

But there is an additional director identity than the producer, which directs the builder to produce who...

 

Therefore, this mode is quite interesting. Directly use the UML diagram.

 

 

We produce two types of products. The two types of products are determined by the manufacturer, and the products only decide what they are. Therefore, at this time, the producer has the final say.

 

But there is also a more advanced conductor, namely the director class. Schedule production.

 

Okay, go to the code. Let's take a look.

 

 

[Cpp]
// Builder. cpp: defines the entry point of the console application.
//
//************************************** **********************************/
/* @ Filename Builder. cpp
@ Author wallwind
@ Createtime 2012/10/23 23:35
@ Function builder Mode
@ Email wochenglin@qq.com
*/
/*************************************** *********************************/
 
# Include "stdafx. h"
# Include <iostream>
# Include <vector>
# Include <string>
 
Using namespace std;
 
Class Product
{
Public:
Product ()
{
M_part = new vector <string>;
}
Virtual ~ Product ()
{
If (m_part)
{
Delete m_part;
}
}
 
Void addPart (string part)
{
M_part-> push_back (part );
}
 
Void showPro ()
{
Vector <string >:: iterator it = m_part-> begin ();
For (; it! = M_part-> end (); it ++)
Cout <* it <endl;
}
Private:
Vector <string> * m_part;
 
};
 
Class Builder
{
Public:
Builder (){}
Virtual ~ Builder (){}
 
Virtual void BuildPartA (){}
Virtual void BuildPartB (){}
 
Virtual Product * getProduct () const
{
Return NULL;
}

 
};
 
 
Class ConcreteBuider1: public Builder
{
Public:
ConcreteBuider1 (){}
Virtual ~ ConcreteBuider1 (){}
Virtual void BuildPartA ()
{
M_product-> addPart ("ConcreteBuider1: BuildPartA ");
}
 
Virtual void BuildPartB ()
{
M_product-> addPart ("ConcreteBuider1: BuildPartB ");
}
 
Virtual Product * getProduct () const
{
Return m_product;
}
 
Private:
Product * m_product;
};
 
 
 
Class ConcreteBuider2: public Builder
{
Public:
ConcreteBuider2 (){}
Virtual ~ ConcreteBuider2 (){}
Virtual void BuildPartA ()
{
M_product-> addPart ("ConcreteBuider2: BuildPartA ");
}
 
Virtual void BuildPartB ()
{
M_product-> addPart ("ConcreteBuider2: BuildPartB ");
}
 
Virtual Product * getProduct () const
{
Return m_product;
}
 
Private:
Product * m_product;
};
 
Class Director
{
Public:
Director (){}
~ Director (){}
 
Void CreateProduct (Builder * build)
{
Build-> BuildPartA ();
Build-> BuildPartB ();
}
 
};
Int _ tmain (int argc, _ TCHAR * argv [])
{
 
Builder * build1 = new ConcreteBuider1 ();
Director dire;
 
Director. CreateProduct (build1 );
 
Product * product1 = build1-> getProduct ();
Product1-> showPro ();
 
Builder * build2 = new ConcreteBuider2 ();
Director. CreateProduct (build2 );
 
Product * product2 = build2-> getProduct ();
Product2-> showPro ();

Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.