谁来描述一下工厂模式?最好有简单的列字

解决方案 »

  1.   

    http://blog.csdn.net/chg2008/category/218713.aspx
    看看这个,对你有帮助
      

  2.   

    package cn.com.chengang.spring;public interface Human {       void eat();       void walk();} package cn.com.chengang.spring;public class Chinese implements Human {    /* (非 Javadoc)     * @see cn.com.chengang.spring.Human#eat()     */    public void eat() {        System.out.println("中国人对吃很有一套");    }     /* (非 Javadoc)     * @see cn.com.chengang.spring.Human#walk()     */    public void walk() {        System.out.println("中国人行如飞");    }} package cn.com.chengang.spring;public class American implements Human {    /* (非 Javadoc)     * @see cn.com.chengang.spring.Human#eat()     */    public void eat() {        System.out.println("美国人主要以面包为主");    }     /* (非 Javadoc)     * @see cn.com.chengang.spring.Human#walk()     */    public void walk() {        System.out.println("美国人以车代步,有四肢退化的趋势");    }}
    package cn.com.chengang.spring;public class Factory {    public final static String CHINESE = "Chinese";    public final static String AMERICAN = "American";     public Human getHuman(String ethnic) {        if (ethnic.equals(CHINESE))            return new Chinese();        else if (ethnic.equals(AMERICAN))            return new American();        else            throw new IllegalArgumentException("参数(人种)错误");    }} 下面是一个测试的程序,使用工厂方法来得到了不同的“人种对象”,并执行相应的方法。package cn.com.chengang.spring;public class ClientTest {    public static void main(String[] args) {        Human human = null;        human = new Factory().getHuman(Factory.CHINESE);        human.eat();        human.walk();        human = new Factory().getHuman(Factory.AMERICAN);        human.eat();        human.walk();    }}