·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » Java的建造设计模式

Java的建造设计模式

类别: JAVA教程  评论数:0 总得分:0
分为指导者,抽象建造者,具体建造者
有一个例子

import java.util.*;
import junit.framework.*;

//不同的媒体形式:

class Media extends ArrayList {}
class Book extends Media {}
class Magazine extends Media {}
class WebSite extends Media {}

// 不同媒体的组成元素:

class MediaItem {
 private String s;
 public MediaItem(String s) { this.s = s; }
 public String toString() { return s; }
}

class Chapter extends MediaItem {
 public Chapter(String s) { super(s); }
}

class Article extends MediaItem {
 public Article(String s) { super(s); }
}

class WebItem extends MediaItem {
 public WebItem(String s) { super(s); }
}

// 抽象建造者角色,它规范了所有媒体建造的步骤:

class MediaBuilder {
 public void buildBase() {}
 public void addMediaItem(MediaItem item) {}
 public Media getFinishedMedia() { return null; }
}

//具体建造者角色

class BookBuilder extends MediaBuilder {
 private Book b;
 public void buildBase() {
  System.out.println("Building book framework");
  b = new Book();
 }

 public void addMediaItem(MediaItem chapter) {
  System.out.println("Adding chapter " + chapter);
  b.add(chapter);
 }
 public Media getFinishedMedia() { return b; }
}

class MagazineBuilder extends MediaBuilder {
 private Magazine m;
 public void buildBase() {
  System.out.println("Building magazine framework");
  m = new Magazine();
 }

 public void addMediaItem(MediaItem article) {
  System.out.println("Adding article " + article);
  m.add(article);
 }
 public Media getFinishedMedia() { return m; }
}

class WebSiteBuilder extends MediaBuilder {
 private WebSite w;
 public void buildBase() {
  System.out.println("Building web site framework");
  w = new WebSite();
 }

 public void addMediaItem(MediaItem webItem) {
  System.out.println("Adding web item " + webItem);
  w.add(webItem);
 }

 public Media getFinishedMedia() { return w; }
}

//指导者角色,也叫上下文
class MediaDirector {
 private MediaBuilder mb;
 public MediaDirector(MediaBuilder mb) {
  this.mb = mb; //具有策略模式相似特征的
 }

 public Media produceMedia(List input) {
  mb.buildBase();
  for(Iterator it = input.iterator(); it.hasNext();)
   mb.addMediaItem((MediaItem)it.next());
   return mb.getFinishedMedia();
 }
};

//测试程序――客户程序角色

public class BuildMedia extends TestCase {
 private List input = Arrays.asList(new MediaItem[] {
  new MediaItem("item1"), new MediaItem("item2"),
  new MediaItem("item3"), new MediaItem("item4"),
 });

 public void testBook() {
  MediaDirector buildBook = new MediaDirector(new BookBuilder());
  Media book = buildBook.produceMedia(input);
  String result = "book: " + book;
  System.out.println(result);
  assertEquals(result, "book: [item1, item2, item3, item4]");
 }

 public void testMagazine() {
  MediaDirector buildMagazine = new MediaDirector(new

MagazineBuilder());
  Media magazine = buildMagazine.produceMedia(input);
  String result = "magazine: " + magazine;
  System.out.println(result);
  assertEquals(result, "magazine: [item1, item2, item3, item4]");
 }

 public void testWebSite(){
  MediaDirector buildWebSite = new MediaDirector(new

WebSiteBuilder());
  Media webSite = buildWebSite.produceMedia(input);
  String result = "web site: " + webSite;
  System.out.println(result);
  assertEquals(result, "web site: [item1, item2, item3, item4]");
 }

 public static void main(String[] args) {
  junit.textui.TestRunner.run(BuildMedia.class);
 }

}





-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1