工厂模式

简单工厂模式:

 对于比较简单的问题:对象种类不是树状结构关系,不强调必须符合“开闭原则”,使用简单工厂模式即可。

注意:简单工厂模式的对象创建方法是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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值