Java的建造设计模式

2008-02-23 10:14:35来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


分为指导者,抽象建造者,具体建造者
有一个例子

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);
 }

}

上一篇: FIFO与LRU 算法实现(java)
下一篇: 读者写者问题之写者优先(java)

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Spring--对业务层的UnitTest

下一篇:FIFO与LRU 算法实现(java)