定义一个文件生成器。文件的类: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模式的特点。
<!-- -->
分享到:
相关推荐
设计模式_创建类_5种模式Java的实际例子_注释详细 包括 1、单例模式(Singleton) 2、工厂方法(Factory Method) 3、抽象工厂(Abstract Factory) 4、建造模式(Builder) 5、原型模式(Prototype)
概述 Builder模式:将一个...下面从一个Person例子进行分析重叠构造器模式、JavaBeans模式、Builder模式的使用。Person类有两个必要参数(id和name),有5个可选参数(age,sex,phone,address和desc) 重叠构造器模式
设计模式(超级详细) 内容简介 有感于设计模式在日常开发中的重要性,同时笔者也自觉对设计模式小有心得,故笔者 *写二十三种设计模式的简单例子、 并整理二十三种设计模式的理论局部,综合汇总成这份Java设计模式〔...
1.1什么是设计模式 2 1.2设计模式的作用 3 1.3GRASP模式的分类 4 1.4GoF设计模式的分类 4 1.5模式的学习阶段 6 第2章负责任地设计对象——GRASP 9 2.1InformationExpert(信息专家) 11 2.2Creator(创造者)...
1.1什么是设计模式 2 1.2设计模式的作用 3 1.3GRASP模式的分类 4 1.4GoF设计模式的分类 4 1.5模式的学习阶段 6 第2章负责任地设计对象——GRASP 9 2.1InformationExpert(信息专家) 11 2.2Creator(创造者)...
com.javapatterns.javaio 专题:设计模式在Java I/O中的应用 com.javapatterns.keygen 专题:序列键生成器与单例及多例模式 com.javapatterns.liskov 里氏代换原则 com.javapatterns.lod 迪米特法则 ...
这种设计模式属于创建模式,因为该模式提供了创建对象的最佳方法之一。 Builder类逐步构建最终对象。 该构建器独立于其他对象。 执行 我们考虑了一家快餐店的商业案例,其中典型的一餐可能是汉堡和冷饮。 汉堡可以...
至于第14章则很快地浏览尚未介绍的设计模式,包括Bridge、Builder、Chain of Responsibility、Flyweight、Interpreter、Mediator、Memento、Prototype、Visitor。第1章还介绍了四个OO基本概念(抽象、封装、继承、...
设计模式之 Template(模板方法) 实际上向你介绍了为什么要使用 Java 抽象类,该模式原理简单,使用很普遍. 设计模式之 Strategy(策略) 不同算法各自封装,用户端可随意挑选需要的算法. 设计模式之 Chain of ...
在《Head First Design Patterns》文中是这么介绍生成器模式的,“封装一个产品的构造过程,并允许按步骤构造”,感觉说得不是很清楚。而在网上查找相关资料是这么解释的,建造者...这种类型的设计模式属于创建型模式。
《研磨设计模式》在内容上深入、技术上实用、和实际开发结合程度很高,书中大部分的示例程序都是从实际项目中简化而来,因此很多例子都可以直接拿到实际项目中使用。如果你想要深入透彻地理解和掌握设计模式,并...
《研磨设计模式》在内容上深入、技术上实用、和实际开发结合程度很高,书中大部分的示例程序都是从实际项目中简化而来,因此很多例子都可以直接拿到实际项目中使用。如果你想要深入透彻地理解和掌握设计模式,并...
《研磨设计模式》在内容上深入、技术上实用、和实际开发结合程度很高,书中大部分的示例程序都是从实际项目中简化而来,因此很多例子都可以直接拿到实际项目中使用。如果你想要深入透彻地理解和掌握设计模式,并...
至于第14章则很快地浏览尚未介绍的设计模式,包括Bridge、Builder、Chain of Responsibility、Flyweight、Interpreter、Mediator、Memento、Prototype,Visitor。第1章还介绍了四个○○基本概念(抽象、封装、继承、...
至于第14章则很快地浏览尚未介绍的设计模式,包括Bridge、Builder、Chainof Responsibility、Flyweight、Interpreter、Mediator、Memento、Prototype、Visitor。第1章还介绍了四个OO基本概念(抽象、封装、继承、...
在个人自学阶段的23种设计模式代码的全部实现,全部使用Java编写,其中还包括各个设计模式在源码中的使用,每种设计模式都举了一个简单的小例子来进行实现,并加以注释 包名解释 一、DesignPattern 1.1 创建型模式 ...
4.5 在其他设计模式中的体现...........50 4.6 一个重构做法的讨论...................54 第5 章 专题:Java 语言的接口........59 5.1 什么是接口...................................59 5.2 为什么使用接口..........
《研磨设计模式》书中的例子 --第二章:简单工厂 本质:选择实现 --第三章:外观模式(Facade) 本质:封装交互,简化调用 --第四章:适配器模式(Adapter) 本质:转换匹配,复用功能 --第五章:单例模式(Singleton) ...
有效的java se 书中的例子 建造者模式 第 2 条:当面临许多构造函数参数时考虑构建器 伸缩构造函数模式有效,但是当参数很多时很难编写客户端代码,而且更难阅读 Builder 模式模拟命名的可选参数 Class.newInstance...