Java设计模式-----Factory Method模式
2008-06-06 11:17
405 查看
源自:http://www.blogjava.net/flustar/archive/2007/11/29/factoryMethod.html
Factory Method模式:
定义一个用于创建对象的接口,让子类决定实例化哪一个类。FactoryMethod使一个类的实例化延迟到其子类。
例子:
Factory Method模式:
定义一个用于创建对象的接口,让子类决定实例化哪一个类。FactoryMethod使一个类的实例化延迟到其子类。
例子:
public abstract class Ball { protected abstract void play(); } public class Basketball extends Ball { protected void play() { System.out.println("play the basketball"); } } public class Football extends Ball { protected void play() { System.out.println("play the football"); } } public abstract class BallFactory { protected abstract Ball makeBall(); } public class BasketballFact extends BallFactory { protected Ball makeBall() { return new Basketball(); } } public class FootballFact extends BallFactory { protected Ball makeBall() { return new Football(); } } public class Client { 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(); } }
相关文章推荐
- java 设计模式之工厂模式Factory Method
- Java 设计模式——工场模式(Factory method)
- java设计模式之一工厂模式(Factory Method)
- java设计模式之一工厂模式(Factory Method)
- java设计模式之一工厂模式(Factory Method)
- Java设计模式-----Factory Method模式
- java设计模式(一)--工厂模式(Factory Method)
- 在Java中应用设计模式--Factory Method
- java设计模式(2):工厂方法模式(Factory Method)
- Java设计模式---工厂方法模式(Factory-Method)
- Java设计模式-工厂方法模式(Factory Method)
- 设计模式之一---工厂方法模式【Factory Method模式】 (java版)
- Java经典设计模式-创建型模式-工厂方法模式(Factory Method)
- 在Java中应用设计模式 - Factory Method
- java设计模式之一工厂模式(Factory Method)
- java设计模式之一工厂模式(Factory Method)
- java设计模式之一工厂模式(Factory Method)
- 在 Java 中应用设计模式 - Factory Method
- java设计模式学习 ----- 工厂方法模式(Factory Method)
- java23种常用设计模式之工厂方法模式(Factory Method)