Design Mode (7)-Build Mode)

Source: Internet
Author: User

[Description] The construction mode defines the complex building Object Design for processing other objects.

[UML diagram]

 

Figure 1

1. The BuildClient class provides the setBuilder () method to set a builder;

2. The Builder class provides a set of method interfaces, including BuildA (), BuildB (), and BuildC ();

3. The BuildClient class provides the build () method to call a group of methods of the Builder object.

 

[Sample Code]

Builder. h

[Html]
# Ifndef BUILDER_H
# Define BUILDER_H
 
Class Builder
{
Public:
Builder ();
 
Public:
Virtual void BuildA ();
Virtual void BuildB ();
Virtual void BuildC ();
 
};
 
# Endif // BUILDER_H

# Ifndef BUILDER_H
# Define BUILDER_H

Class Builder
{
Public:
Builder ();

Public:
Virtual void BuildA ();
Virtual void BuildB ();
Virtual void BuildC ();

};

# Endif // BUILDER_H
Builder. cpp

[Html]
# Include <QDebug>
# Include "builder. h"
 
Builder: Builder ()
{
QDebug () <"construct Builder ";
}
 
Void Builder: BuildA ()
{
 
}
 
Void Builder: BuildB ()
{
 
}
 
Void Builder: BuildC ()
{
 
}

# Include <QDebug>
# Include "builder. h"

Builder: Builder ()
{
QDebug () <"construct Builder ";
}

Void Builder: BuildA ()
{

}

Void Builder: BuildB ()
{

}

Void Builder: BuildC ()
{

}
Xmlbuilder. h

[Html]
# Ifndef XMLBUILDER_H
# Define XMLBUILDER_H
 
# Include "builder. h"
 
Class XMLBuilder: public Builder
{
Public:
XMLBuilder ();
 
Public:
Void BuildA ();
Void BuildB ();
Void BuildC ();
};
 
# Endif // XMLBUILDER_H

# Ifndef XMLBUILDER_H
# Define XMLBUILDER_H

# Include "builder. h"

Class XMLBuilder: public Builder
{
Public:
XMLBuilder ();

Public:
Void BuildA ();
Void BuildB ();
Void BuildC ();
};

# Endif // XMLBUILDER_H

Xmlbuilder. cpp

[Html]
# Include <QDebug>
# Include "xmlbuilder. h"
 
XMLBuilder: XMLBuilder ()
{
QDebug () <"construct XMLBuilder ";
}
 
Void XMLBuilder: BuildA ()
{
QDebug () <"XMLBuilder BuildA ";
}
 
Void XMLBuilder: BuildB ()
{
QDebug () <"XMLBuilder BuildB ";
}
 
Void XMLBuilder: BuildC ()
{
QDebug () <"XMLBuilder BuildC ";
}

# Include <QDebug>
# Include "xmlbuilder. h"

XMLBuilder: XMLBuilder ()
{
QDebug () <"construct XMLBuilder ";
}

Void XMLBuilder: BuildA ()
{
QDebug () <"XMLBuilder BuildA ";
}

Void XMLBuilder: BuildB ()
{
QDebug () <"XMLBuilder BuildB ";
}

Void XMLBuilder: BuildC ()
{
QDebug () <"XMLBuilder BuildC ";
}

Textbuilder. h

[Html]
# Ifndef TEXTBUILDER_H
# Define TEXTBUILDER_H
 
# Include "builder. h"
 
Class TextBuilder: public Builder
{
Public:
TextBuilder ();
 
Public:
Void BuildA ();
Void BuildB ();
Void BuildC ();
};
 
# Endif // TEXTBUILDER_H

# Ifndef TEXTBUILDER_H
# Define TEXTBUILDER_H

# Include "builder. h"

Class TextBuilder: public Builder
{
Public:
TextBuilder ();

Public:
Void BuildA ();
Void BuildB ();
Void BuildC ();
};

# Endif // TEXTBUILDER_H

Textbuilder. cpp

[Html]
# Include <QDebug>
# Include "textbuilder. h"
 
TextBuilder: TextBuilder ()
{
QDebug () <"construct TextBuilder ";
}
 
Void TextBuilder: BuildA ()
{
QDebug () <"TextBuilder BuildA ";
}
 
Void TextBuilder: BuildB ()
{
QDebug () <"TextBuilder BuildB ";
}
 
Void TextBuilder: BuildC ()
{
QDebug () <"TextBuilder BuildC ";
}

# Include <QDebug>
# Include "textbuilder. h"

TextBuilder: TextBuilder ()
{
QDebug () <"construct TextBuilder ";
}

Void TextBuilder: BuildA ()
{
QDebug () <"TextBuilder BuildA ";
}

Void TextBuilder: BuildB ()
{
QDebug () <"TextBuilder BuildB ";
}

Void TextBuilder: BuildC ()
{
QDebug () <"TextBuilder BuildC ";
}
 

Buildclient. h

[Html]
# Ifndef BUILDCLIENT_H
# Define BUILDCLIENT_H
 
# Include "builder. h"
 
Class BuildClient
{
Public:
BuildClient ();
 
Private:
Builder * builder;
 
Public:
Void setBuilder (Builder * builder );
Void build ();
};
 
# Endif // BUILDCLIENT_H

# Ifndef BUILDCLIENT_H
# Define BUILDCLIENT_H

# Include "builder. h"

Class BuildClient
{
Public:
BuildClient ();

Private:
Builder * builder;

Public:
Void setBuilder (Builder * builder );
Void build ();
};

# Endif // BUILDCLIENT_H

Buildclient. cpp

[Html]
# Include <QDebug>
# Include "buildclient. h"
 
BuildClient: BuildClient ()
{
}
 
Void BuildClient: setBuilder (Builder * builder)
{
This-> builder = builder;
}
 
Void BuildClient: build ()
{
Builder-> BuildA ();
Builder-> BuildB ();
Builder-> BuildC ();
}

# Include <QDebug>
# Include "buildclient. h"

BuildClient: BuildClient ()
{
}

Void BuildClient: setBuilder (Builder * builder)
{
This-> builder = builder;
}

Void BuildClient: build ()
{
Builder-> BuildA ();
Builder-> BuildB ();
Builder-> BuildC ();
}
Main. cpp

[Html]
# Include <QDebug>
# Include "buildclient. h"
# Include "xmlbuilder. h"
# Include "textbuilder. h"
 
Int main (int argc, char ** argv)
{
BuildClient client;
Client. setBuilder (new XMLBuilder );
Client. build ();
Return 0;
}

# Include <QDebug>
# Include "buildclient. h"
# Include "xmlbuilder. h"
# Include "textbuilder. h"

Int main (int argc, char ** argv)
{
BuildClient client;
Client. setBuilder (new XMLBuilder );
Client. build ();
Return 0;
}

[Running result]

[Html]
Construct Builder
Construct XMLBuilder
XMLBuilder BuildA
XMLBuilder BuildB
XMLBuilder BuildC

Construct Builder
Construct XMLBuilder
XMLBuilder BuildA
XMLBuilder BuildB
XMLBuilder BuildC

[Comparison] construction mode and Strategy Mode

Design Pattern (3)-Example code UML diagram in Strategy, as shown in Figure 2:

 

Figure 2

Compared with the UML diagram of the Policy mode, it is not difficult to find that the policy mode is very similar to the construction mode. What is the difference between them?

Difference: the Policy mode provides a method, while the construction mode provides a set of methods.

 

You may have different opinions on the differences between the construction mode and the Strategy Mode. You are welcome to give your valuable comments and learn together!

 

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.