简单工厂和工厂模式的合并对比代码

public class Factory2 {

	/**
	 * @param args  工厂模式
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		//工厂模式
		Ifactory ifactory =new people1factory();
		leifeng student=ifactory.creatLeifeng();
		student.wash();
		student.buy();
		
		System.out.println("***************");
		
		//简单工厂模式
		leifeng student1=simplyfactory.creLeifeng("people1");
		student1.wash();
		student1.buy();

	}

}

//******************************************************************************

class leifeng{//雷锋类
	
	public void wash() {}
	public void buy() {}
}

class people1 extends leifeng{
	public void wash() {
	System.out.println("洗1");
	}
	public void buy() {
		System.out.println("买1");
	}
	
}//人员1
class people2 extends leifeng{
	public void wash() {
	System.out.println("洗2");
	}
	public void buy() {
		System.out.println("买2");
	}
	
}//人员2

//******************************************************************************
//工厂模式
interface Ifactory{//接口工厂
	
	leifeng creatLeifeng();
}

//1工厂实现了工厂接口,生产人员1
class people1factory implements Ifactory{

	@Override
	public leifeng creatLeifeng() {
		// TODO Auto-generated method stub
		return new people1();
	}
}

//2工厂实现了工厂接口,生产人员2
class people2factory implements Ifactory{

	@Override
	public leifeng creatLeifeng() {
		// TODO Auto-generated method stub
		return new people2();
	}
}

//***********************************************************************************
//简单工厂模式
class simplyfactory{
	
	public static leifeng creLeifeng(String type) {
		leifeng resuLeifeng=null;
		switch (type) {
		case "people1":
			resuLeifeng=new people1();
			break;
		case "people2":
			resuLeifeng=new people2();
			break;
		}
		return resuLeifeng;
	}
}

//*************************************************************************************
//抽象工厂,大约就是增加类似于leifeng的接口,一系列的相关的接口,和工厂模式基本类似
interface leifeng2{
	
}

  

posted @ 2014-04-28 17:01  蓦然回首的包子  阅读(235)  评论(0)    收藏  举报