工厂方法模式
//工厂方法模式:现实例子
//CopyRight(C)CAnca Software Office. 2006
//Created by CAnca.
import java.util.*;
public class Real_World_Example{
public static void main(String[] args){
Product[] products = new Product[2];
products[0] = new concreteProduct1();
products[1] = new concreteProduct2();
for(int i = 0 ; i < products.length ; i++){
products[i].show();
}
}
}
abstract class Product{
protected ArrayList al;
public Product(){
al = new ArrayList();
setFood();
}
abstract void setFood();
public void show(){
System.out.println("食物有:");
for(int i = 0 ; i < al.size() ; i++){
System.out.println((i+1) + "、" + al.get(i));
}
System.out.println();
}
}
class concreteProduct1 extends Product{
public void setFood(){
this.al.add("Apple");
this.al.add("Banana");
this.al.add("Pear");
}
}
class concreteProduct2 extends Product{
public void setFood(){
this.al.add("苹果");
this.al.add("香蕉");
this.al.add("莉子");
}
}