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;
}