`
pouyang
  • 浏览: 313513 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Design Patterns 创建模式 之 Builder

阅读更多
Design Patterns  创建模式 之 Builder
创建者模式
简单地说,就好象我要一座房子住,可是我不知道怎么盖(简单的砌墙,层次较低),也不知道怎么样设计(建几个房间,几个门好看,层次较高),于是我需要找一帮民工,他们会砌墙,还得找个设计师,他知道怎么设计,我还要确保民工听设计师的领导,而设计师本身也不干活,光是下命令,这里砌一堵墙,这里砌一扇门,这样民工开始建设,最后,我可以向民工要房子了。在这个过程中,设计师是什么也没有,除了他在脑子里的设计和命令,所以要房子也是跟民工要,记住了!


1 定义工人接口 民工
/**
 * 工人接口,定义了各个工人所要进行的工所作。他们负责进行具体部件如窗户,地板的建造。 
 * 同时因为房子是民工建的,因此建设完成后由他把房子递交回房主 
 * @author poy
 */
public interface Builder {   
	  public  void makeWindow();   
	  public  void makeFloor();   
	  public  Room  getRoom();   
}   

/**
 * 民工。负责进行具体部件如窗户,地板的建造。
 * 同时因为房子是民工建的,因此建设完成后由他把房子递交回房主
 * @author poy
 */
public class Mingong  implements Builder {   
	  private  String window="";   
	  private  String floor="";   
	  public  void makeWindow() {   
	    window=new String("window");   
	  }   
	  public  void makeFloor(){   
	    floor=new String("floor");   
	  }   
	  // 回交房子给房主   
	  public  Room  getRoom() {   
	    if((!window.equals(""))&&(!floor.equals(""))) {   
	      System.out.println("room ready!");   
	      return new Room();   
	    }   
	    else return null;   
	  }   
}   


2 定义设计师
/**
 * 设计师。他知道房子应该怎么设计,但他不会自己去建造,而是指挥民工去建造。
 * @author poy
 */
public class Designer {
	// 指挥民工进行工作   
	  public void order(Builder  builder) {   
	    builder.makeWindow();   
	    builder.makeFloor();   
	  }   
}

3 房主
/**
 * 房主。房主的任务就是聘请一个民工,一个设计师,
 * 同时把民工给设计师指挥,督促设计师开展工作。最后从民工手上收房。     
 * @author poy
 *
 */
public class Client {     
  public static void main(String[] args) {     
     Builder mingong = new Mingong();     
     Designer  designer = new  Designer();     
     designer.order(mingong);     
     mingong.getRoom();     
  }     
}
 
分享到:
评论

相关推荐

    .NET Design Patterns [Kindle Edition]

    After reading this book, you will be able to convincingly leverage these design patterns (factory pattern, builder pattern, prototype pattern, adapter pattern, facade pattern, decorator pattern, ...

    Design.Patterns.Explained.Simply

    We've tried hard to avoid both of these categories with Design Patterns Explained Simply. This book is fast and simple way to get the idea behind each of the 29 popular design patterns. The book is ...

    《Java Design Patterns》高清完整英文PDF版

    Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....

    Head First Design Patterns 英文版 Head First设计模式

    《Head First Design Patterns》共有14章,每章都介绍了几个设计模式,完整地涵盖了四人组版本全部23个设计模式。前言先介绍《Head First Design Patterns》的用法;第1章到第11章陆续介绍的设计模式为Strategy、...

    Apress.Pro.Design.Patterns.in.Swift

    Pro Design Patterns in Swift shows you how to harness the power and flexibility of Swift to apply the most important and enduring design patterns to your applications, taking your development ...

    Beginning SOLID Principles and Design Patterns for ASP.NET Developers.pdf

    ■Chapter 4: Creational Patterns: Abstract Factory and Builder ■Chapter 5: Structural Patterns: Adapter, Bridge, Composite, and Decorator ■Chapter 6: Structural Patterns: Façade, Flyweight, and ...

    Design Patterns in Modern C++--2018

    Use creational patterns of builder, factories, prototype and singleton Implement structural patterns such as adapter, bridge, decorator, facade and more Work with the behavioral patterns such as ...

    Design Patterns Elements of Reusable Object-Oriented Software

    • How Design Patterns Solve Design Problems • How to Select a Design Pattern • How to Use a Design Pattern A Case Study: Designing a Document Editor • Design Problems • Document Structure ...

    Head First Design Patterns

    并非四人组的标准名词),第13章介绍如何进一步学习设计模式,如何发觉新的设计模式等主题,至于第14章则很快地浏览尚未介绍的设计模式,包括Bridge、Builder、Chain of Responsibility、 Flyweight、Interpreter、...

    Head First Design Patterns 高清英文版

    至于第14章则很快地浏览尚未介绍的设计模式,包括Bridge、Builder、Chain of Responsibility、Flyweight、Interpreter、Mediator、Memento、Prototype,Visitor。第1章还介绍了四个○○基本概念(抽象、封装、继承、...

    Android Design Patterns and Best Practice

    Chapter 1 , Design Patterns, introduces the development environment, and two common design patterns, the factory and abstract factory. ...support library and the builder design pattern.

    Packt.Go.Design.Patterns.2017

    Creational Patterns - Singleton, Builder, Factory, Prototype, and Abstract Factory Design Patterns Chapter 3. Structural Patterns - Composite, Adapter, and Bridge Design Patterns Chapter 4. ...

    Head First Design Patterns(英文,无水印,完整版)

    本书荣获2005年第十五届Jolt通用类图书震撼大奖。 本书英文xx版被《程序员》等机构评选为2006...至于第14章则很快地浏览尚未介绍的设计模式,包括Bridge、Builder、Chain of Responsibility、Flyweight、Interpreter、

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm”供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) 生成...

    design_patterns_in_typescript:TypeScript中的设计模式实现

    $ cd design_patterns_in_typescript $ tsc 根目录中有一个tsconfig.json文件,负责编译器选项。 设置后,默认目标现在为Ecmascript5。 任何其他选项都在这里。 默认情况下,输出为patterns.js文件。 要仅编译一...

    设计模式(Design.Patterns.CHN)

    3.2 Builder(生成器)—对象创建型 模式 63 3.3 Factory Method(工厂方法)— 对象创建型模式 70 3.4 Prototype(原型)—对象创建型 模式 87 3.5 Singleton(单件)—对象创建型 模式 84 3.6 创建型模式的讨论 89...

    Java.Design.Patterns.1537192353

    Java design patterns with the Simplest real world examples which are easy to understand & remember as well. Table of Contents PREFACE ABOUT DESIGN PATTERNS SINGLETON PATTERN FACTORY PATTERN ABSTRACT ...

    java餐饮管理系统源码6-design_patterns:设计模式

    建造者模式(Builder Pattern) 原型模式(Prototype Pattern) 结构型模式 适配器模式(Adapter Pattern) 桥接模式(Bridge Pattern) 过滤器模式(Filter、Criteria Pattern) 组合模式(Composite Pattern) ...

    [Java设计模式(第2版)(Design.Patterns.in.Java).John.Metsker

    《java设计模式(第2版)》通过一个完整的java项目对经典著作design patterns一书介绍的23种设计模式进行了深入分析与讲解,实践性强,却又不失对模式本质的探讨。本书创造性地将这些模式分为5大类别,以充分展现各个...

    jQuery.Design.Patterns.178588

    Learn about the observer pattern and the deferred observer pattern, two of the most popular design patterns that handle custom events Advance your jQuery skills by learning about patterns such as ...

Global site tag (gtag.js) - Google Analytics