码迷,mamicode.com
首页 > 编程语言 > 详细

工厂方法模式之JAVA实现

时间:2014-10-10 20:03:24      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   java   sp   div   log   ef   

 

//Product

interface Human {
    public void talk();
}

class American implements Human {
    @Override
    public void talk() {
        System.out.println("American speak English.");
    }
}

class Chinese implements Human {
    @Override
    public void talk() {
        System.out.println("Chinese speak Chinese.");
    }
}

class Korean implements Human {
    @Override
    public void talk() {
        System.out.println("Korean speak Korean.");
    }
}

 

//Factory

abstract class HumanFactory {
    abstract Human create();
}

class ChineseFactory extends HumanFactory {
    public Human create() {
        return new Chinese();
    }
}

class AmericanFactory extends HumanFactory {
    public Human create() {
        return new American();
    }
}

class KoreanFactory extends HumanFactory {
    public Human create() {
        return new Korean();
    }
}

 

//Test

public class FactoryMethodPattern {
    /**
     * @param args
     */
    public static void main(String[] args) {
        HumanFactory factory = new ChineseFactory();
        Human human = factory.create();
        human.talk();
    }
}

 

工厂方法模式之JAVA实现

标签:style   blog   color   ar   java   sp   div   log   ef   

原文地址:http://www.cnblogs.com/jingmoxukong/p/4016173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!