WEB开发网
开发学院WEB开发Jsp 抽象工厂类 阅读

抽象工厂类

 2008-01-05 08:48:54 来源:WEB开发网   
核心提示://package com.cc; import java.util.*; import java.lang.Class; interface book{ void sayBookName(); void sayBookPRice(); } class ChineseBook implements book //中文书

  //package com.cc;
import java.util.*;
import java.lang.Class;

interface book{
void sayBookName();
void sayBookPRice();
}

class ChineseBook implements book //中文书
{
private String name;
private int price;
public ChineseBook(){
//this.ChineseBook("chinese",10);
name = "ChineseBook";
price = 10;
}

public ChineseBook(String name,int price){
this.name = name;
this.price = price;
}

public void sayBookName(){
System.out.println("i am a chinese book "+name);
}

public void sayBookPrice(){
System.out.println("my price is "+price);
}
//继续书店工厂
//
private static class Factory extends BookFactory //内部类的使用
{
protected book create(){
return new ChineseBook();
}
}
static { //生成1个实例后,就要把这个实例加到工厂中
System.out.println("loading ChineseBook.class");
BookFactory.addFactory("ChineseBook",new Factory()); //为什么要把工厂
//放到list中呢?便于加载一次以后可直接调用
}
}

class EnglishBook implements book //英文书
{
private String name;
private int price;
public EnglishBook(){
//this.EnglishBook("english",10);
name = "EnglisthBook";
price = 10;
}

public EnglishBook(String name,int price){
this.name = name;
this.price = price;
}

public void sayBookName(){
System.out.println("i am a english book "+name);
}

public void sayBookPrice(){
System.out.println("my price is "+price);
}

//继续书店工厂
//
private static class Factory extends BookFactory
{
protected book create(){
return new EnglishBook();
}
}
static { //生成1个实例后,就要把这个实例加到工厂中
System.out.println("loading EnglishBook.class");
BookFactory.addFactory("EnglishBook",new Factory()); //为什么要把工厂
//放到list中呢?便于加载一次以后可直接调用
}
}

abstract class BookFactory
{
protected abstract book create(); //这个方法要在具体类中实现
private static Map factories = new HashMap(); //工厂map
public static void addFactory(String id,BookFactory f)
{
//加入工厂,将书和创建书的工厂加入到map中,为什么要加入创建书的工厂呢?
//因为创建书的工厂有个create()方法,他要具体返回一个实例
System.out.println(factories.put(id,f));
}
public static final book createBook(String id){//创建书实例,书的名称作为参数,传出的是被创建好的Book的实例
if (!factories.containsKey(id))//当工厂中不包含这个类时要装载
{
try{
// Load dynamically 动态装载
Class.forName(id); //怎么加载的?只要直接执行
//这个类,类就会调用static里的语句
}catch(ClassNotFoundException e){
throw new RuntimeException("error"+id);
}
// See if it was put in 看看它是否被装载
if(!factories.containsKey(id))throw new RuntimeException("Bad shape creation: " + id);
}
return ((BookFactory)factories.get(id)).create();
}
}

public class BookFactory2
{
String bookList[] = {"EnglishBook", "ChineseBook","EnglishBook","ChineseBook","ChineseBook","EnglishBook"};
List books = new ArrayList();

public void test(){
for (int i=0;i<bookList.length;i++)
{
books.add(BookFactory.createBook(bookList));
System.out.println("already create");
}
Iterator it = books.iterator();
while(it.hasNext()){
book s = (book)it.next();
s.sayBookName();
}
}
public static void main(String args[]) {
new BookFactory2().test();
}
}

Tags:抽象 工厂

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接