女娲造物,她可以造人和动物,用阴绳造女人和雌性动物,用阳绳造男人和雄性动物,人要吃东西,会说话。动物也要吃东西,还会睡觉.
package com.strongit.factory;
interface Person{
public void eat();
public void talk();
}
class Man implements Person{
public void eat() {
// TODO Auto-generated method stub
System.out.println("男人在吃东西。。。。");
}
public void talk() {
// TODO Auto-generated method stub
System.out.println("男人在说话。。。。");
}
}
interface Animal{
public void eat();
public void sleep();
}
class Bull implements Animal{
public void eat() {
// TODO Auto-generated method stub
System.out.println("公牛在吃东西。。。。");
}
public void sleep() {
// TODO Auto-generated method stub
System.out.println("公牛在睡觉。。。。");
}
}
class Cow implements Animal{
public void eat() {
// TODO Auto-generated method stub
System.out.println("母牛在吃东西。。。。");
}
public void sleep() {
// TODO Auto-generated method stub
System.out.println("母牛在睡觉。。。。");
}
}
class Woman implements Person{
public void eat() {
// TODO Auto-generated method stub
System.out.println("女人在吃东西。。。。");
}
public void talk() {
// TODO Auto-generated method stub
System.out.println("女人在说话。。。。");
}
}
//表示女娲
interface NWFactory{
public Person createPerson(String type);
public Animal createAnimal(String type);
}
//阳绳-->用来造男人和雄性动物(Bull)
class YangSheng implements NWFactory{
Man man = null;
Bull bull = null;
public Bull createAnimal(String type) {
try {
bull = (Bull)Class.forName("com.strongit.factory."+type).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bull;
}
public Man createPerson(String type) {
try {
man = (Man)Class.forName("com.strongit.factory."+type).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return man;
}
}
//阴绳-->用来造女人和雌性动物(Cow)
class YinSheng implements NWFactory{
Woman woman = null;
Cow cow = null;
public Cow createAnimal(String type) {
try {
cow = (Cow)Class.forName("com.strongit.factory."+type).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cow;
}
public Woman createPerson(String type) {
try {
woman = (Woman)Class.forName("com.strongit.factory."+type).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return woman;
}
}
public class FactoryDemo1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//实例化一个阳绳,ys
YangSheng ys=new YangSheng();
//实例化一个阴绳,ys1
YinSheng ys1=new YinSheng();
//造男人和女人,p1是男人,p2是女人
Person p1=ys.createPerson("Man");
Person p2=ys1.createPerson("Woman");
//造动物,a1是公牛(Bull),a2是母牛(Cow)
Animal a1=ys.createAnimal("Bull");
Animal a2=ys1.createAnimal("Cow");
a1.eat();
a1.sleep();
a2.eat();
a2.sleep();
p1.eat();
p1.talk();
p2.eat();
p2.talk();
}
}
源代码下载:
http://www.blogjava.net/Files/vjame/FactoryDemo03.rar