Design Pattern: Builder mode

Source: Internet
Author: User

definition: separates the construction of a complex object from its representation so that the same build process can create different representations.

Type: Create class Mode

Class Diagram:

Four elements

    • Product class: generally is a more complex object, that is, the process of creating objects is more complex, generally there will be more code volume. In this class diagram, the product class is a concrete class, not an abstract class. In practical programming, a product class can consist of an abstract class with its different implementations, or it can consist of multiple abstract classes and their implementations.
    • Abstract Builder: The purpose of introducing abstract builders is to bring the concrete process of construction into its subclasses. This makes it easier to expand. There are generally at least two abstract methods, one for building products and one for returning products.
    • Builder: Implement all the non-implemented methods of an abstract class, typically two tasks: build a product, and return to a well-formed product.
    • Director class: responsible for invoking the appropriate builder to build the product, the Director class is generally not related to the product class, and direct interaction with the Director class is the Builder class. Generally speaking, the Director class is used to encapsulate the variable parts of the program.

Code implementation

[Java]View Plaincopy
  1. Class Product {
  2. private String name;
  3. private String type;
  4. public void Showproduct () {
  5. System.out.println ("name:" +name);
  6. SYSTEM.OUT.PRINTLN ("model:" +type);
  7. }
  8. public void SetName (String name) {
  9. this.name = name;
  10. }
  11. public void SetType (String type) {
  12. this.type = type;
  13. }
  14. }
  15. Abstract class Builder {
  16. public abstract void Setpart (String arg1, string arg2);
  17. public Abstract Product getproduct ();
  18. }
  19. Class ConcreteBuilder extends Builder {
  20. Private Product Product = new product ();
  21. Public Product getproduct () {
  22. return product;
  23. }
  24. public void Setpart (String arg1, String arg2) {
  25. Product.setname (ARG1);
  26. Product.settype (ARG2);
  27. }
  28. }
  29. Public class Director {
  30. Private Builder builder = new ConcreteBuilder ();
  31. Public Product getaproduct () {
  32. Builder.setpart ("BMW car","X7");
  33. return builder.getproduct ();
  34. }
  35. Public Product getbproduct () {
  36. Builder.setpart ("Audi car","Q5");
  37. return builder.getproduct ();
  38. }
  39. }
  40. Public class Client {
  41. public static void Main (string[] args) {
  42. Director Director = new Director ();
  43. Product Product1 = Director.getaproduct ();
  44. Product1.showproduct ();
  45. Product product2 = Director.getbproduct ();
  46. Product2.showproduct ();
  47. }
  48. }

The advantages of the builder model

First, the builder pattern is well encapsulated. Using builder mode can effectively encapsulate changes, in the case of using builder mode, the general product class and the builder class are more stable, so encapsulating the main business logic in the Director class can achieve better stability overall.

Second, the builder pattern is easy to scale. If there is a new requirement, it can be done by implementing a new builder class, basically without modifying the code that has been tested before, so there is no risk to the original functionality.

The difference between builder mode and factory model

As we can see, the builder pattern is very similar to the factory model, and in general, the builder model is only a "director class" role that is more than the factory model. In the class diagram of the builder pattern, if the director class is seen as the client of the final call, then the rest of the diagram can be seen as a simple factory pattern.

The builder pattern is typically used to create more complex objects than the factory model, because the object creation process is more complex, so the object creation process is independent of the new class-The Director class. In other words, the factory pattern is to encapsulate the entire creation of objects in the factory class, which is provided by the factory class to the client, and the builder's model typically only provides for the construction of individual components in the product class, and the concrete construction process is delivered to the Director class. The Director class is responsible for assembling individual components into products according to specific rules, and then delivering the assembled product to the client.

Summarize

The builder pattern is similar to the factory model, they are both builder models and the applicable scenarios are similar. In general, if the construction of the product is complex , then use the factory model, if the product is more complex to build, then use the builder mode.

Design Pattern: Builder mode

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.