Builder模式

定义一个文件生成器。文件的类:MyFile 分为三个部分,也就是说有三个属性:FileHead,FileContent,FileEnd。
FileHead定义如下:

package create.builder1;
public class FileHead {
 private String value;

 public String getValue() {
  return value;
 }

 public void setValue(String value) {
  this.value = value;
 }
}

FileContent定义如下:

package create.builder1;
public class FileContent {
 private String value;

 public String getValue() {
  return value;
 }

 public void setValue(String value) {
  this.value = value;
 }
}

FileEnd 定义如下:

package create.builder1;
public class FileEnd {
 private String value;

 public String getValue() {
  return value;
 }

 public void setValue(String value) {
  this.value = value;
 }
}

现在定义MyFile:
package create.builder1;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class MyFile {
 private FileHead fhead;

 private FileContent fcontent;

 private FileEnd fend;
  
 public FileContent getFcontent() {
  return fcontent;
 }

 public void setFcontent(FileContent fcontent) {
  this.fcontent = fcontent;
 }

 public FileEnd getFend() {
  return fend;
 }

 public void setFend(FileEnd fend) {
  this.fend = fend;
 }

 public FileHead getFhead() {
  return fhead;
 }

 public void setFhead(FileHead fhead) {
  this.fhead = fhead;
 }
 public void write(String path) throws IOException{
  File f=new File(path);
  FileWriter fw=new FileWriter(f);
  PrintWriter out=new PrintWriter(fw);
  out.println(fhead.getValue());
  out.println(fcontent.getValue());
  out.println(fend.getValue());
  out.close();  
 }
}

定义Builder,我给她取名字为FileBuilder:
package create.builder1;

public interface FileBuilder {
  void buildHead();
  void buildContent();
  void buildEnd();
  MyFile getResult();
}
该定义Director了,名字为:FileDirector:
package create.builder1;

public class FileDirector {
    private FileBuilder filebuilder;
    public FileDirector(FileBuilder filebuilder){
     this.filebuilder=filebuilder;
    }
    public void construct(){
     filebuilder.buildHead();
     filebuilder.buildContent();
     filebuilder.buildEnd();
    }
}

现在到了定义具体Builder的时候了,定义两个,一个是生成txt文件的,一个生成xml文件
TxtBuilder:
package create.builder1;

public class TxtBuilder implements FileBuilder{
 private FileHead fh=new FileHead();
 private FileContent fc=new FileContent();;
 private FileEnd fe=new FileEnd();

 public void buildHead() {
  // TODO Auto-generated method stub
  fh.setValue(“This is text file”);
 }

 public void buildContent() {
  // TODO Auto-generated method stub
  fc.setValue(“this is my content”);
 }

 public void buildEnd() {
  // TODO Auto-generated method stub
  fe.setValue(“this is the end of the file”);
 }

 public MyFile getResult() {
  // TODO Auto-generated method stub
  MyFile my=new MyFile();
  my.setFcontent(fc);
  my.setFhead(fh);
  my.setFend(fe);
  return my;
 }

}

XmlBuilder

package create.builder1;

public class XmlBuilder implements FileBuilder {
    private FileHead fh=new FileHead();
    private FileContent fc=new FileContent();
    private FileEnd fe=new FileEnd();
   
 public void buildHead() {
  // TODO Auto-generated method stub
  fh.setValue(“<?xml version=\”1.0\” encoding=\”GB2312\”?><content>”); 
 }

 public void buildContent() {
  // TODO Auto-generated method stub
  fc.setValue(“<test>asdasd</test>”);
 }

 public void buildEnd() {
  // TODO Auto-generated method stub
       fe.setValue(“</content>”);  
 }

 public MyFile getResult() {
  // TODO Auto-generated method stub
  MyFile my=new MyFile();
  my.setFcontent(fc);
  my.setFhead(fh);
  my.setFend(fe);
  return my;
 }

}
Demo如下:
package create.builder1;

import java.io.IOException;

public class Demo {

 public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
        FileBuilder fb=new TxtBuilder();
        FileDirector fd=new FileDirector(fb);
       
        fd.construct();
        MyFile my=fb.getResult();
        my.write(“D:/test.txt”);
       
        FileBuilder fbxml=new XmlBuilder();
        FileDirector fdxml=new FileDirector(fbxml);
        fdxml.construct();
    
   MyFile myxml=fbxml.getResult();
        myxml.write(“D:/test1.xml”);
 }

}
在Demo里可以看到Builder模式的特点。
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s