Design Mode C ++ study note 10 (Builder Mode)

Source: Internet
Author: User

The builder mode separates the construction of a complex object from its representation, so that different representations can be created during the same construction process. For a piece of obscure text, the method to create different representations is to pass in the created parameters during the creation process. For details, see the code.

10. 1. Explanation

Main (), customer

CCarModel, Product Model

CBenzModel, Mercedes-Benz Model

CBMWModel, BMW model

ICarBuilder, builder Interface

CBenzBuilder, Mercedes-Benz Builder

CBMWBuilder, BMW Builder

CDirector, Director

Note: CCarModel implements the template method. Builder is responsible for starting to build the product. The order of building a product is determined by Director or main.

Note: The Builder mode is very similar to the abstract factory. The builder pays more attention to the logic order of product construction, while the abstract factory pays more attention to producing products of different models. The abstract factory does not care about the order.

Check the Code:

// Builder. cpp
# Include "stdafx. h"
# Include "CarModel. h"
# Include "BenzModel. h"
# Include "BMWModel. h"
# Include "BenzBuilder. h"
# Include "BMWBuilder. h"
# Include "Director. h"
# Include <vector>
# Include <iostream>
Using std: vector;
Using std: string;
Using std: cout;
Using std: endl;

Void DoBenzRun () // when no use mode is available, you need to pass the step one by one into the model.
{
Cout <"---------- generate the Mercedes-Benz Model ----------" <endl;
CBenzModel * pBenz = new CBenzModel ();
Vector <string> seq;
Seq. push_back ("engine boom"); // The engine is first started when the customer requests run.
Seq. push_back ("start"); // start up
Seq. push_back ("stop"); // stop after a period of operation.

PBenz-> SetSequence (& seq );
PBenz-> Run ();
Delete pBenz;
}

Void DoBuilder () // The usage mode is generated by benzBuilder and bmwBuilder, and the same creation sequence is used.
{
Cout <"---------- generate model ---------- In the same order" <endl;
Vector <string> seq;
Seq. push_back ("engine boom ");
Seq. push_back ("start ");
Seq. push_back ("stop ");

CBenzBuilder benzBuilder;
BenzBuilder. SetSequence (& seq );
CBenzModel * pBenz = dynamic_cast <CBenzModel *> (benzBuilder. GetCarModel ());
PBenz-> Run ();

CBMWBuilder bmwBuilder;
BmwBuilder. SetSequence (& seq );
CBMWModel * pBmw = dynamic_cast <CBMWModel *> (bmwBuilder. GetCarModel ());
PBenz-> Run ();
}
Void DoDirector () // use the instructor to encapsulate the creation logic and enclose the creation sequence in the instructor class.
{
Cout <"---------- generate models in batches ----------" <endl;
CDirector dire;

// A-type Mercedes-Benz
For (int I = 0; I <2; I ++)
Director. GetABenzModel ()-> Run ();

// B-type Mercedes-Benz vehicles
For (int I = 0; I <2; I ++)
Director. GetBBenzModel ()-> Run ();

// W c-type BMW
For (int I = 0; I <2; I ++)
Director. GetCBMWModel ()-> Run ();
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
DoBenzRun ();

DoBuilder ();

DoDirector ();

_ CrtSetDbgFlag (_ CRTDBG_LEAK_CHECK_DF | _ CRTDBG_ALLOC_MEM_DF );
_ CrtDumpMemoryLeaks ();
Return 0;
}

// CarModel. h

# Pragma once
# Include <vector>
# Include <iostream>
Using std: vector;
Using std: string;
Class CCarModel
{
Public:
CCarModel (void );
Virtual ~ CCarModel (void );
Void Run ();
Void SetSequence (vector <string> * pSeq );
Protected:
Virtual void Start () = 0;
Virtual void Stop () = 0;
Virtual void Alarm () = 0;
Virtual void EngineBoom () = 0;
Private:
Vector <string> * m_pSequence;
};

// CarModel. cpp

# Include "StdAfx. h"
# Include "CarModel. h"
# Include <vector>
# Include <iostream>
Using std: vector;
Using std: string;
CCarModel: CCarModel (void)
{
}
CCarModel ::~ CCarModel (void)
{
}
Void CCarModel: SetSequence (vector <string> * sequq)
{
M_pSequence = pSeq;
}
Void CCarModel: Run ()
{
Vector <string >:: const_iterator it = m_pSequence-> begin ();
For (; it <m_pSequence-> end (); ++ it)
{
String actionName = * it;
If (actionName. compare ("start") = 0)
{
Start ();
}
Else if (actionName. compare ("stop") = 0)
{
Stop ();
}
Else if (actionName. compare ("alarm") = 0)
{
Alarm ();
}
Else if (actionName. compare ("engine boom") = 0)
{
EngineBoom ();
}
}
}

// BenzModel. h

# Pragma once
# Include "carmodel. h"
Class CBenzModel:
Public CCarModel
{
Public:
CBenzModel (void );
~ CBenzModel (void );
Protected:
Void Start ();
Void Stop ();
Void Alarm ();
Void EngineBoom ();
};

// BenzModel. cpp

# Include "StdAfx. h"
# Include "BenzModel. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CBenzModel: CBenzModel (void)
{
}
CBenzModel ::~ CBenzModel (void)
{
}
Void CBenzModel: Start ()
{
Cout <"Mercedes-Benz launch..." <endl;
}
Void CBenzModel: Stop ()
{
Cout <"Mercedes parking..." <endl;
}
Void CBenzModel: Alarm ()
{
Cout <"Benz whistle" <endl;
}
Void CBenzModel: EngineBoom ()
{
Cout <"the sound of the Mercedes-Benz engine is like this..." <endl;
}

// BMWModel. h

# Pragma once
# Include "carmodel. h"
Class CBMWModel:
Public CCarModel
{
Public:
CBMWModel (void );
~ CBMWModel (void );
Protected:
Void Start ();
Void Stop ();
Void Alarm ();
Void EngineBoom ();
};

// BMWModel. cpp

# Include "StdAfx. h"
# Include "BMWModel. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CBMWModel: CBMWModel (void)
{
}
CBMWModel ::~ CBMWModel (void)
{
}
Void CBMWModel: Start ()
{
Cout <"BMW launch..." <endl;
}
Void CBMWModel: Stop ()
{
Cout <"BMW parking..." <endl;
}
Void CBMWModel: Alarm ()
{
Cout <"BMW whistle" <endl;
}
Void CBMWModel: EngineBoom ()
{
Cout <"the BMW engine sounds like this..." <endl;
}

// ICarBuilder. h

# Pragma once
# Include "CarModel. h"
# Include <iostream>
# Include <vector>
Using std: string;
Using std: vector;
Class ICarBuilder
{
Public:
ICarBuilder (void)
{
}
Virtual ~ ICarBuilder (void)
{
}
Virtual void SetSequence (vector <string> * pseq) = 0;
Virtual CCarModel * GetCarModel () = 0;
};
// BenzBuilder. h

# Pragma once
# Include "icarbuilder. h"
# Include "CarModel. h"
# Include <iostream>
# Include <vector>
Using std: string;
Using std: vector;
Class CBenzBuilder:
Public ICarBuilder
{
Public:
CBenzBuilder (void );
~ CBenzBuilder (void );
Void SetSequence (vector <string> * pSeq );
CCarModel * GetCarModel ();
Private:
CCarModel * m_pBenz;
};
// BenzBuilder. cpp

# Include "StdAfx. h"
# Include "BenzBuilder. h"
# Include "BenzModel. h"
CBenzBuilder: CBenzBuilder (void)
{
M_pBenz = new CBenzModel ();
}
CBenzBuilder ::~ CBenzBuilder (void)
{
Delete m_pBenz;
}
Void CBenzBuilder: SetSequence (vector <string> * sequq)
{
M_pBenz-> SetSequence (semi Q );
}
CCarModel * CBenzBuilder: GetCarModel ()
{
Return m_pBenz;
}

// BMWBuilder. h

# Pragma once
# Include "icarbuilder. h"
# Include "CarModel. h"
# Include <iostream>
# Include <vector>
Using std: string;
Using std: vector;
Class CBMWBuilder:
Public ICarBuilder
{
Public:
CBMWBuilder (void );
~ CBMWBuilder (void );
Void SetSequence (vector <string> * pSeq );
CCarModel * GetCarModel ();
Private:
CCarModel * m_pBMW;
};

// BMWBuilder. cpp

# Include "StdAfx. h"
# Include "BMWBuilder. h"
# Include "BMWModel. h"
CBMWBuilder: CBMWBuilder (void)
{
M_pBMW = new CBMWModel ();
}
CBMWBuilder ::~ CBMWBuilder (void)
{
Delete m_pBMW;
}
Void CBMWBuilder: SetSequence (vector <string> * sequq)
{
M_pBMW-> SetSequence (pSeq );
}
CCarModel * CBMWBuilder: GetCarModel ()
{
Return m_pBMW;
}

// Director. h

# Pragma once
# Include "BenzModel. h"
# Include "BMWModel. h"
# Include "BenzBuilder. h"
# Include "BMWBuilder. h"
# Include <vector>
Using std: vector;
Class CDirector
{
Public:
CDirector (void );
~ CDirector (void );
CBenzModel * GetABenzModel ();
CBenzModel * GetBBenzModel ();
CBMWModel * GetCBMWModel ();
CBMWModel * GetDBMWModel ();
Private:
Vector <string> * m_pSeqence;
CBenzBuilder * m_pBenzBuilder;
CBMWBuilder * m_pBMWBuilder;
};

// Director. cpp

# Include "StdAfx. h"
# Include "Director. h"
CDirector: CDirector (void)
{
M_pBenzBuilder = new CBenzBuilder ();
M_pBMWBuilder = new CBMWBuilder ();
M_1_qence = new vector <string> ();
}
CDirector ::~ CDirector (void)
{
Delete m_pBenzBuilder;
Delete m_pBMWBuilder;
Delete m_pSeqence;
}
CBenzModel * CDirector: GetABenzModel ()
{
M_1_qence-> clear ();
M_1_qence-> push_back ("start ");
M_1_qence-> push_back ("stop ");
M_pBenzBuilder-> SetSequence (m_pSeqence );
Return dynamic_cast <CBenzModel *> (m_pBenzBuilder-> GetCarModel ());
}
CBenzModel * CDirector: GetBBenzModel ()
{
M_1_qence-> clear ();
M_define qence-> push_back ("engine boom ");
M_1_qence-> push_back ("start ");
M_1_qence-> push_back ("stop ");
M_pBenzBuilder-> SetSequence (m_pSeqence );
Return dynamic_cast <CBenzModel *> (m_pBenzBuilder-> GetCarModel ());
}
CBMWModel * CDirector: GetCBMWModel ()
{
M_1_qence-> clear ();
M_1_qence-> push_back ("alarm ");
M_1_qence-> push_back ("start ");
M_1_qence-> push_back ("stop ");
M_pBMWBuilder-> SetSequence (m_pSeqence );
Return static_cast <CBMWModel *> (m_pBMWBuilder-> GetCarModel ());
}
CBMWModel * CDirector: GetDBMWModel ()
{
M_1_qence-> clear ();
M_1_qence-> push_back ("start ");
M_pBenzBuilder-> SetSequence (m_pSeqence );
Return dynamic_cast <CBMWModel *> (m_pBMWBuilder-> GetCarModel ());
}

The builder mode is a creation mode. It mainly focuses on the Creation sequence. The production products are slightly different in different order.

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.