Design Pattern in Java 101 – Builder Pattern (Creational Pattern)
By admin on Dec 31, 2007 in Creational, Java, Programming
Builder pattern is used to construct a complex object from simple objects step by step.
E.g.,
I have a Message class
public class Message { private String sender; private String recipient; private String header; private String body; public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getRecipient() { return recipient; } public void setRecipient(String recipient) { this.recipient = recipient; } public String getHeader() { return header; } public void setHeader(String header) { this.header = header; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String toString() { return "Sender: " + sender + "\n" + "Recipient: " + recipient + "\n" + "Header: " + header + "\n" + "Body: " + body ; } }
Then create a MessageBuilder class
public abstract class MessageBuilder { protected Message message; public Message getMessage() { return message; } public void setMessage(Message message) { this.message = message; } public void createMessage() { message = new Message(); } public void constructMessage(){ message = new Message(); } public void constructSender(String sender){ message.setSender(sender); } public void constructRecipient(String recipient){ message.setRecipient(recipient); } public abstract void constructMessageHeader(); public abstract void constructMessageBody(String body); }
I can build different mesages, e.g. SMSMessageBuilder and MMSMessageBuilder
public class SMSMessageBuilder extends MessageBuilder { public void constructMessageHeader() { message.setHeader("SMS header"); } public void constructMessageBody(String body) { message.setBody(body); } }
public class MMSMessageBuilder extends MessageBuilder { public void constructMessageHeader() { message.setHeader("MMS header"); } public void constructMessageBody(String body) { message.setBody(body); } }
ContentProvider class creates the required message using the relevant builder
public class ContentProvider { private MessageBuilder messageBuilder; public MessageBuilder getMessageBuilder() { return messageBuilder; } public void setMessageBuilder(MessageBuilder messageBuilder) { this.messageBuilder = messageBuilder; } public Message getMessage() { return messageBuilder.getMessage(); } public Message createMessage( MessageBuilder messageBuilder, String sender, String recipient, String message) { messageBuilder.constructMessage(); messageBuilder.constructSender(sender); messageBuilder.constructRecipient(recipient); messageBuilder.constructMessageHeader(); messageBuilder.constructMessageBody(message); return messageBuilder.getMessage(); } }
To test it
public class TestPattern { public static void main(String[] args){ ContentProvider contentProvider = new ContentProvider(); MessageBuilder smsMessageBuilder = new SMSMessageBuilder(); MessageBuilder mmsMessageBuilder = new MMSMessageBuilder(); Message sms = contentProvider.createMessage( smsMessageBuilder, "1234", "5678", "SMS message"); Message mms = contentProvider.createMessage( mmsMessageBuilder, "1234", "5678", "MMS message"); System.out.println(sms.toString()); System.out.println(mms.toString()); } }
The output
Sender: 1234 Recipient: 5678 Header: SMS header Body: SMS message Sender: 1234 Recipient: 5678 Header: MMS header Body: MMS message
Rhett Stewart | Apr 18, 2008 | Reply
montmorilonite abasedness sanballat absciss penalizable demos oversystematic conqueror
1/1
http://webdesignersuk.tripod.com/
Http://maneetti.net/ | May 3, 2013 | Reply
I have been browsing on-line greater than 3 hours as
of late, but I never found any fascinating article like yours.
It is beautiful value sufficient for me. In my
view, if all web owners and bloggers made just right content material as you probably did, the web will be
much more useful than ever before.
Http://Noidea.free.fr/ | May 12, 2013 | Reply
I am curious to find out what blog system you have been using?
I’m experiencing some small security issues with my latest blog and I’d like to find something
more risk-free. Do you have any suggestions?
Janelle | May 15, 2013 | Reply
How long has insurance cover been with us and where on earth does it
originate from. A home is more than a structure made up of brick
and concrete for some people as they invest a lot of emotions in to it, therefore it needs
to be protected. Through using these particular measures you might
find your cheap home insurance quotes just at your fingertips.
Lashawn | May 15, 2013 | Reply
Superb post however I was wondering if you could write a litte more on this topic?
I’d be very grateful if you could elaborate a little bit more. Bless you!
Jenifer | May 16, 2013 | Reply
I think the admin of this site is actually working hard for his site, as here every data
is quality based information.
building insurance quotes | May 16, 2013 | Reply
But if you use the internet, you should be able to find cheap homeowner
insurance policies in no time. No matter what
route you choose, the final point is to get a bunch of homeowners’ insurance quotes and what the policies cover, then do a side-by-side comparison, weed out the ones you don’t
like. Employee motivation is the classic response on
this matter.
Del | May 29, 2013 | Reply
Visit websites of all of the chosen companies and scrutinize the offered plans very carefully.
The Consolidated Omnibus Budget Reconciliation
Act (COBRA) of 1986 does not confer any new benefits. the government also revised upward the numbers
for August and September, showing 110,000 fewer jobs
lost,.
Home Insurance | Jun 5, 2013 | Reply
To protect your assets, you should ask to your insurance agent for additional coverage.
Home insurance company you choose to apply with a client by performing a credit check will check
your credit score. First determine the extent of the
injury and if medical help is necessary.
home insurance price comparison sites uk | Jun 8, 2013 | Reply
I’ve been surfing online more than 4 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. Personally, if all site owners and bloggers made good content as you did, the internet will be much more useful than ever before.
one day car insurance quote | Jun 16, 2013 | Reply
certainly like your web site however you have to take a look at the
spelling on quite a few of your posts. Several of them are rife with spelling problems and I find it very troublesome to inform the reality nevertheless I’ll definitely come again again.
Hannah | Jun 17, 2013 | Reply
Do you have a spam issue on this site; I also am a blogger, and I was wanting to know your situation; many of us have created some
nice procedures and we are looking to trade
solutions with others, please shoot me an e-mail if interested.
cheap one day car insurance uk | Jun 17, 2013 | Reply
Good answer back in return of this difficulty with solid arguments and explaining everything
on the topic of that.
best payday loans | Jun 22, 2013 | Reply
I read this piece of writing completely about the resemblance of most recent and previous technologies,
it’s remarkable article.
http://www.cellsea.com/user/home/CR510ed35c14f81 | Jul 4, 2013 | Reply
I visited several web sites but the audio quality for audio songs present
at this site is in fact excellent.
Keisha | Jul 5, 2013 | Reply
Hi to every , as I am truly keen of reading this web site’s post to be updated regularly. It includes fastidious stuff.
compare home insurance quotes | Jul 7, 2013 | Reply
Hmm it appears like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your
blog. I as well am an aspiring blog blogger but I’m still new to the whole thing. Do you have any tips and hints for newbie blog writers? I’d definitely appreciate it.
Compare Home Insurance companies | Jul 8, 2013 | Reply
Hey! Someone in my Myspace group shared this website with us so I came to take a look.
I’m definitely loving the information. I’m book-marking and
will be tweeting this to my followers! Great blog and amazing
design and style.