The builder pattern for Java design patterns

Source: Internet
Author: User

This article continues to introduce the builder model of the 23 design pattern series. Definition: Builder mode: Separates the construction of a complex object from its representation so that the same build process can create different representations. Practical scope 1, when the algorithm to create complex objects should be independent of the object's components and the way they are assembled. 2, when the construction process must allow the constructed object to have different representations. Role in this design pattern, there are several roles: 1, Builder: Specify an abstract interface for each part that creates a product object. 2. ConcreteBuilder: Implements the builder interface to construct and assemble the individual parts of the product, defines and clarifies the representations it creates, and provides an interface for retrieving the product. 3. Director: Constructs an object that uses the Builder interface to guide the build process. 4. Product: Represents the complex object being constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process, which contains the classes that define the constituent parts, including the interfaces that assemble the parts into the final product. Role Builder: [Java]View PlainCopy print?
    1. Public interface Personbuilder {
    2. void Buildhead ();
    3. void Buildbody ();
    4. void Buildfoot ();
    5. Person Buildperson ();
    6. }
Role ConcreteBuilder: [Java]View PlainCopyprint?
  1. Public class Manbuilder implements Personbuilder {
  2. person person;
  3. Public Manbuilder () {
  4. person = New Mans ();
  5. }
  6. public void Buildbody () {
  7. Person.setbody ("building a man's body");
  8. }
  9. public void Buildfoot () {
  10. Person.setfoot ("building a man's foot");
  11. }
  12. public void Buildhead () {
  13. Person.sethead ("Building a man's head");
  14. }
  15. Public person Buildperson () {
  16. return person;
  17. }
  18. }
Role ConcreteBuilder: [Java]View PlainCopy print?
  1. Public class Womanbuilder implements Personbuilder {
  2. person person;
  3. Public Womanbuilder () {
  4. person = new Woman ();
  5. }
  6. public void Buildbody () {
  7. Person.setbody ("Building a Woman's body");
  8. }
  9. public void Buildfoot () {
  10. Person.setfoot ("Build a woman's foot");
  11. }
  12. public void Buildhead () {
  13. Person.sethead ("Build a woman's head");
  14. }
  15. Public person Buildperson () {
  16. return person;
  17. }
  18. }
Role Director: [Java]View PlainCopyprint?
    1. Public class Persondirector {
    2. Public person Constructperson (Personbuilder pb) {
    3. Pb.buildhead ();
    4. Pb.buildbody ();
    5. Pb.buildfoot ();
    6. return Pb.buildperson ();
    7. }
    8. }
Role Product: [Java]View PlainCopyprint?
  1. Public class Person {
  2. Private String head;
  3. private String body;
  4. private String foot;
  5. Public String GetHead () {
  6. return head;
  7. }
  8. public void Sethead (String head) {
  9. this.head = head;
  10. }
  11. Public String GetBody () {
  12. return body;
  13. }
  14. public void Setbody (String body) {
  15. this.body = body;
  16. }
  17. Public String Getfoot () {
  18. return foot;
  19. }
  20. public void Setfoot (String foot) {
  21. this.foot = foot;
  22. }
  23. }
  24. Public class Mans extends person {
  25. Public Mans () {
  26. System.out.println ("Start building men");
  27. }
  28. }
  29. Public class Woman extends person {
  30. Public Woman () {
  31. System.out.println ("Start building women");
  32. }
  33. }
Test: [Java]View PlainCopyprint?
  1. Public class test{
  2. public static void Main (string[] args) {
  3. Persondirector PD = new Persondirector ();
  4. Person Womanperson = Pd.constructperson (new Manbuilder ());
  5. Person Manperson = Pd.constructperson (new Womanbuilder ());
  6. }
  7. }
The builder model can evolve in many forms during use: if there is only one concrete object to be built, the abstract builder and director can be omitted, allowing ConcreteBuilder to play the role of mentor and builder, Even ConcreteBuilder can be put into product. In the "Effective Java" book, the second article mentions "when encountering multiple constructor parameters to consider the builder", in fact, the builder here is the builder mode, but the inside of the four characters are put into the specific product inside. The above example, if only a man, evolved as follows: [Java]View PlainCopyprint?
  1. Public class Man {
  2. Private String head;
  3. private String body;
  4. private String foot;
  5. Public String GetHead () {
  6. return head;
  7. }
  8. public void Sethead (String head) {
  9. this.head = head;
  10. }
  11. Public String GetBody () {
  12. return body;
  13. }
  14. public void Setbody (String body) {
  15. this.body = body;
  16. }
  17. Public String Getfoot () {
  18. return foot;
  19. }
  20. public void Setfoot (String foot) {
  21. this.foot = foot;
  22. }
  23. }
[Java]View PlainCopy print?
  1. Public class manbuilder{
  2. Mans Man;
  3. Public Manbuilder () {
  4. man = New Man ();
  5. }
  6. public void Buildbody () {
  7. Man.setbody ("building a man's body");
  8. }
  9. public void Buildfoot () {
  10. Man.setfoot ("building a man's foot");
  11. }
  12. public void Buildhead () {
  13. Man.sethead ("Building a man's head");
  14. }
  15. Public Mans Builderman () {
  16. Buildhead ();
  17. Buildbody ();
  18. Buildfoot ();
  19. return man;
  20. }
  21. }
Test: [Java]View PlainCopy print?
    1. Public class test{
    2. public static void Main (string[] args) {
    3. Manbuilder builder = new Manbuilder ();
    4. Mans man = Builder.builderman ();
    5. }
    6. }
More Design Patterns: 23 design Mode Series

jason0539

Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)

Sweep code Follow me public number

The builder pattern for Java design patterns

Related Article

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.