简单工厂模式:
对于比较简单的问题:对象种类不是树状结构关系,不强调必须符合“开闭原则”,使用简单工厂模式即可。
注意:简单工厂模式的对象创建方法是static的,因此,简单工厂模式也叫做静态工厂模式。
//抽象产品类
interface Ball{
public void play();
}
//具体产品类1
class FootBall implements Ball{
public void play(){
System.out.println("play football");
}
}
//具体产品类2
class BasketBall implements Ball{
public void play(){
System.out.println("play basketball");
}
}
//静态工厂类
class BallFactory{
static Ball createBall(String ballName){
if(ballName=="basketball"){
return new BasketBall();
}else if(ballName=="football"){
return new FootBall();
}
return null;
}
}
//测试类
public class StaticFactory{
public static void main(String[] args){
BallFactory factory=new BallFactory();
Ball footBall=factory.createBall("football");
footBall.play();
Ball basketBall=factory.createBall("basketball");
basketBall.play();
}
}
工厂方法模式:
工厂方法模式使得工厂类的创造对象的方法得以被重写,也就实现了工厂类的多态,符合“开闭原则”
package org.jyjiao;
abstract class BallFactory {
protected abstract Ball makeBall(); // Factory Method
}
class BasketballFact extends BallFactory {
public Ball makeBall() { // 子类实现Factory Method决定实例化哪一个类的
return new Basketball();
}
}
class FootballFact extends BallFactory {
public Ball makeBall() { // 子类实现Factory Method决定实例化哪一个类的
return new Football();
}
}
class Basketball extends Ball {
public void play() {
System.out.println("play the basketball");
}
}
class Football extends Ball {
public void play() {
System.out.println("play the football");
}
}
abstract class Ball {
protected abstract void play();
}
public class FactoryMethod {
public static void main(String[] args) {
BallFactory ballFactory = new BasketballFact();
Ball basketball = ballFactory.makeBall();
basketball.play();
ballFactory = new FootballFact();
Ball football = ballFactory.makeBall();
football.play();
}
}
抽象工厂模式:
抽象工厂模式应对树状对象族问题显得比较清晰(例如:汽车的产品族)
interface FootBall{
public void play();
}
class EnglishFootBall implements FootBall{
public void play(){
System.out.println("play EnglishFootBall");
}
}
class AmericanFootBall implements FootBall{
public void play(){
System.out.println("play AmericanFootBall");
}
}
interface BasketBall{
public void play();
}
class EnglishBasketBall implements BasketBall{
public void play(){
System.out.println("play EnglishBasketBall");
}
}
class AmericanBasketBall implements BasketBall{
public void play(){
System.out.println("play AmericanBasketBall");
}
}
interface Factory{
FootBall createFootBall();
BasketBall createBasketBall();
}
class EnglishBallFactory implements Factory{
public FootBall createFootBall(){
return new EnglishFootBall();
}
public BasketBall createBasketBall(){
return new EnglishBasketBall();
}
}
class AmericanBallFactory implements Factory{
public FootBall createFootBall(){
return new AmericanFootBall();
}
public BasketBall createBasketBall(){
return new AmericanBasketBall();
}
}
public class AbstractFactory{
public static void main(String[] args){
Factory americanFactory=new AmericanBallFactory();
FootBall americanFootBall=americanFactory.createFootBall();
americanFootBall.play();
BasketBall americanBasketBall=americanFactory.createBasketBall();
americanBasketBall.play();
Factory englishFactory=new EnglishBallFactory();
FootBall englishFootBall=englishFactory.createFootBall();
englishFootBall.play();
BasketBall englishBasketBall=englishFactory.createBasketBall();
englishBasketBall.play();
}
}
参考:
http://www.cnblogs.com/zhuxiongfeng/archive/2010/04/10/1708797.html
http://hi.baidu.com/kukei/blog/item/df4d97ddb4b2be355882dd01.html