Builder
Design Pattern:
IBuilder.java
public interface IBuilder {
public void buildPreface();
public void buildReportTitle();
public void buildHeader();
public void buildContent();
public void buildFooter();
public Report getReport();
}
Mybuilder.java
public class MyBuilder implements IBuilder{
private Report report;
public MyBuilder(){
report = new Report();
}
@Override
public Report getReport(){
return report;
}
@Override
public void buildContent() {
report.setContent("my 1st report............");
}
@Override
public void buildFooter() {
report.setFooter("copyright softwareArchitecture&DesignWithArun.com : 2014");
}
@Override
public void buildHeader() {
report.setHeader(new String[]{"header_1", "header_2"});
}
@Override
public void buildPreface() {
report.setPreface("Report using Builder by : "+System.getProperty("user.info"));
}
@Override
public void buildReportTitle()
{
report.setReportTitle("My Report");
}
}
ReportDirector.java
public class ReportDirector {
private IBuilder myBuilder;
public ReportDirector(){
myBuilder = new MyBuilder();
}
public void buildReport(){
// report
building logic/steps
myBuilder.buildReportTitle();
myBuilder.buildHeader();
myBuilder.buildPreface();
myBuilder.buildContent();
myBuilder.buildFooter();
}
public Report getReport(){
return this.myBuilder.getReport();
}
}
Report.java
public class Report {
private String reportTitle;
private String header[];
private String preface;
private String content;
private String footer;
public String getReportTitle() {
return reportTitle;
}
public void
setReportTitle(String reportTitle) {
this.reportTitle =
reportTitle;
}
public String[] getHeader() {
return header;
}
public void setHeader(String[]
header) {
this.header = header;
}
public String getPreface() {
return preface;
}
public void setPreface(String
preface) {
this.preface = preface;
}
public String getContent() {
return content;
}
public void setContent(String
content) {
this.content = content;
}
public String getFooter() {
return footer;
}
public void setFooter(String
footer) {
this.footer = footer;
}
}
Client.java
public class Client {
public static void main(String[] args) {
ReportDirector reportDirector = new ReportDirector();
reportDirector.buildReport();
Report report =
reportDirector.getReport();
//use report
object as per business
System.out.println(report.getContent());
}
}
No comments:
Post a Comment