The builder pattern for Java design patterns

Source: Internet
Author: User
Tags getdate

Introduction to Builders ' models

The builder pattern separates the construction of a complex object from its representation, allowing the same build process to create different representations. In other words, the build pattern is the object's creation pattern. The construction mode separates the internal representation of a product from the production process of the product, allowing a construction process to produce objects with different internal representations.

Design method of builder model

The builder pattern typically has four structural bodies, each of which is director,builder,concretebuilder,product. Each body plays a different role, and the following are their respective roles.

Role of Director

Constructs an object that uses the builder abstract interface, even if a builder object is used to construct a Director object.

The role of builder

Specifies the abstract interface for each part that creates a product object, which defines some common parts.

The role of ConcreteBuilder

Implement the public interface of builder to construct each component of the product;

Define and clarify the representations it creates;

Provides an interface for obtaining a product, such as a Get method;

The role of product

Represents a complex object that is constructed, contains classes that define parts, and assembles those parts into the final product.

Example code for Builder modeExample Introduction

Below will be a message push feature to disguise the builder mode of the application, first in two kinds of messages, one is on-line reminders, the other is offline reminders. The content of the message is time: * * * someone is online (or offline)! First, we create a new message class, because the message body is roughly the same, so we first define a public abstract message, and then let two specific messages inherit this abstract class, which can implement partial code sharing.

Abstract message


Package com.builder;/** * Notifies abstract class */import Java.util.date;public abstract classes abstractnotification {private String Subjec T;private Date date;protected String Content;public string Getsubject () {return subject;} public void Setsubject (String subject) {this.subject = subject;} Public Date getDate () {return date;} public void SetDate (date date) {this.date = date;} Public String getcontent () {return content;} public void Send () {StringBuilder StringBuilder = new StringBuilder (); Stringbuilder.append ("Time:"); Stringbuilder.append (This.getdate ()); Stringbuilder.append ("T"); Stringbuilder.append (This.getsubject ()); Stringbuilder.append (This.getcontent ()); System.out.println (Stringbuilder.tostring ());}}
Online Reminder Message


Package Com.builder;public class Onlinenotification extends Abstractnotification {public onlinenotification () { This.content= "already Online";}}




Offline Reminder Message


Package Com.builder;public class Offlinenotification extends Abstractnotification {public offlinenotification () { This.content= "Already offline";}}



We then create a builder interface that defines a common method for creating names and dates, and defines a method for getting notifications.


Builder interface


Package Com.builder;import Java.util.date;public Interface Builder {public abstract void Buildsubject (String subject); public abstract void Builddate (date date);p ublic abstract abstractnotification getnotification (); }



We then created two concrete builder objects to implement the builder interface.


Two classes of ConcreteBuilder


Package Com.builder;import Java.util.date;public class Onlinenotificationbuilder implements builder {private Abstractnotification abstractnotification;public Onlinenotificationbuilder () {abstractnotification=new Onlinenotification ();} @Overridepublic void Buildsubject (String subject) {abstractnotification.setsubject (subject);} @Overridepublic void Builddate (date date) {abstractnotification.setdate (date);} @Overridepublic abstractnotification getnotification () {return abstractnotification;}}



Package Com.builder;import Java.util.date;public class Offlinenotificationbuilder implements builder {private Abstractnotification abstractnotification;public Offlinenotificationbuilder () {abstractnotification=new Offlinenotification ();} @Overridepublic void Buildsubject (String subject) {abstractnotification.setsubject (subject);} @Overridepublic void Builddate (date date) {abstractnotification.setdate (date);} @Overridepublic abstractnotification getnotification () {return abstractnotification;} }




Director class
Package Com.builder;import Java.util.date;public class Director {Private builder Builder;public director (builder Builder) {This.builder = builder;} public void construct (String subject,date date) {builder.buildsubject (subject); Builder.builddate (date); Abstractnotification notification = builder.getnotification (); Notification.send ();}}



Test code


Package Com.builder;import Java.util.date;public class Test {public static void main (string[] args) {builder Onlinenotifi Cationbuilder = new Onlinenotificationbuilder ();D Irector director = new Director (Onlinenotificationbuilder); Director.construct ("Zhang San", New Date ());}}



Test results



Time: Thu 16:07:58 CST 2015-sheet three already on-line





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.