java中的设计模式(三)--抽象工厂模式 反射机制

阅读本模式之前,请先熟悉简单工厂模式和工厂方法模式

1.定义人员类的接口

package com.dao;

public interface People {
	public void cry();

	public void eat();

	public void jump();
	
	public void sex();
}

 

2.定义三个抽象类,实现人员类接口

package com.dao;

public abstract class AbstractYelloPeople implements People{

	public void cry() {
		// TODO Auto-generated method stub
		System.out.println("黄种人会哭");
	}

	public void eat() {
		// TODO Auto-generated method stub
		
	}

	public void jump() {
		// TODO Auto-generated method stub
		
	}

	
}


 

package com.dao;

public abstract class AbstractWhitePeople implements People{

	public void cry() {
		// TODO Auto-generated method stub
		System.out.println("白种人会哭");
	}

	public void eat() {
		// TODO Auto-generated method stub
		
	}

	public void jump() {
		// TODO Auto-generated method stub
		
	}

	
}


 

package com.dao;

public abstract class AbstractBlackPeople implements People{

	public void cry() {
		// TODO Auto-generated method stub
		System.out.println("黑种人会哭");
	}

	public void eat() {
		// TODO Auto-generated method stub
		
	}

	public void jump() {
		// TODO Auto-generated method stub
		
	}

	
}

 

3.人员类的具体产品对象

package com.dao.impl;

import com.dao.AbstractBlackPeople;

public class BlackFamalePeople extends AbstractBlackPeople {

	public void sex() {
		System.out.println("该黑种人性别为女");
	}

}


 

package com.dao.impl;

import com.dao.AbstractBlackPeople;

public class BlackMalePeople extends AbstractBlackPeople {

	public void sex() {
		System.out.println("该黑种人性别为男");
	}

}


 

package com.dao.impl;

import com.dao.AbstractWhitePeople;

public class WhiteFamalePeople extends AbstractWhitePeople {

	public void sex() {
		System.out.println("该白种人性别为女");
	}

}


 

package com.dao.impl;

import com.dao.AbstractWhitePeople;

public class WhiteMalePeople extends AbstractWhitePeople {

	public void sex() {
		System.out.println("该白种人性别为男");
	}

}


 

package com.dao.impl;

import com.dao.AbstractYelloPeople;

public class YellowFamalePeople extends AbstractYelloPeople {

	public void sex() {
		System.out.println("该黄种人性别为女");
	}

}


 

package com.dao.impl;

import com.dao.AbstractYelloPeople;

public class YellowMalePeople extends AbstractYelloPeople {

	public void sex() {
		System.out.println("该黄种人性别为男");
	}

}



4.定义工厂接口

package com.dao;

public interface Factory {
	public People createBlack();

	public People createYellow();

	public People createWhite();
}


5.定义工厂的抽象类,实现工厂接口

package com.dao;

public abstract class AbstractFactory implements Factory {
	private People people = null;
	
	public People createPeople (Class c){
		try {
			people = (People) Class.forName(c.getName()).newInstance();
		} catch (Exception e) {
			System.out.println("创建失败");
		}
		return people;
	}
}


这里也可以根据传入的不同字符,实例化不同的产品对象

 

6.工厂的具体实现方法

package com.dao.impl;

import com.dao.AbstractFactory;
import com.dao.People;

public class CreateMalePeople extends AbstractFactory {

	public People createYellow() {
		return super.createPeople(YellowMalePeople.class);
	}

	public People createWhite() {
		return super.createPeople(WhiteMalePeople.class);
	}

	public People createBlack() {
		return super.createPeople(BlackMalePeople.class);
	}

}


 

package com.dao.impl;

import com.dao.AbstractFactory;
import com.dao.People;

public class CreateFamalePeople extends AbstractFactory {

	public People createYellow() {
		return super.createPeople(YellowFamalePeople.class);
	}

	public People createWhite() {
		return super.createPeople(WhiteFamalePeople.class);
	}

	public People createBlack() {
		return super.createPeople(BlackFamalePeople.class);
	}

}


6.客户端代码

import com.dao.Factory;
import com.dao.People;
import com.dao.impl.CreateFamalePeople;
import com.dao.impl.CreateMalePeople;


public class Test {

	public static void main(String[] args) {
		//第一条生产线,生产女人
		Factory maleFactory = new CreateMalePeople();
		//第二条生产线,生产男人
		Factory famaleFactory = new CreateFamalePeople();
		
		//开始造人--男人
		People yellowMalePeople = maleFactory.createYellow();
		yellowMalePeople.sex();
		People blackMalePeople = maleFactory.createBlack();
		blackMalePeople.sex();
		People whiteMalePeople = maleFactory.createWhite();
		whiteMalePeople.sex();
		//开始造人--女人
		People yellowFamalelePeople = famaleFactory.createYellow();
		yellowFamalelePeople.sex();
		People blackFamalePeople = famaleFactory.createBlack();
		blackFamalePeople.sex();
		People whiteFamalePeople = famaleFactory.createWhite();
		whiteFamalePeople.sex();
	}

}


 

7.运行结果

该黄种人性别为男
该黑种人性别为男
该白种人性别为男
该黄种人性别为女
该黑种人性别为女
该白种人性别为女


可以看到这里用到了抽象类,为什么要用?

简单说就是为了不让他实例化对象。想想,不管是什么肤色的人种,还没性别来,怎么能让他实例化啊。。。。。。

当然,人妖除外

代码分析:

首先要理解产品族和产品等级结构的概念,这个可以百度。

记住:每一个产品族中所包含的产品数目与产品等级结构的数目是一致的。

阅读更多
个人分类: 学习笔记
想对作者说点什么? 我来说一句

设计模式笔记:工厂模式

hhh

Monkey_Dog Monkey_Dog

2016-05-31 13:04:56

阅读数:1173

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭