package methodOfFactory;
public class People
{
private String name;
private String sex;
public People(){}
public People(String name,String sex)
{
this.name = name;
this.sex = sex;
}
public void show(){}
public String toString()
{
return "Name: "+name+" Sex: "+sex;
}
}
package methodOfFactory;
public class Boy extends People {
public Boy(){}
public Boy(String name)
{
super(name,"male");
}
public void show(){}
}
package methodOfFactory;
public class ZhangSan extends Boy {
public ZhangSan()
{
super("zhang san");
}
public void show()
{
System.out.println("zhang san is my dota friend!");
}
}
package methodOfFactory;
public class Girl extends People {
public Girl(){}
public Girl(String name)
{
super(name,"male");
}
public void show(){}
}
package methodOfFactory;
public class LiSi extends Girl {
public LiSi()
{
super("li si");
}
public void show()
{
System.out.println("li si is my girl friend!");
}
}
package methodOfFactory;
public class WangWu extends Girl
{
public WangWu()
{
super("wang wu");
}
public void show()
{
System.out.println("wang wu is my wife!");
}
}
package methodOfFactory;
public interface Factory {
public abstract People getPeople(String name);
}
package methodOfFactory;
public class BoyFactory implements Factory {
@Override
public People getPeople(String name) {
if(name.equalsIgnoreCase("zhangsan"))
{
return new ZhangSan();
}
return null;
}
}
package methodOfFactory;
public class GirlFactory implements Factory
{
@Override
public People getPeople(String name)
{
if(name.equalsIgnoreCase("lisi"))
{
return new LiSi();
}else if(name.equalsIgnoreCase("wangwu"))
{
return new WangWu();
}
return null;
}
}
package methodOfFactory;
public class Main
{
public static void main(String[] args)
{
Factory f = new BoyFactory();
Boy b = (Boy) f.getPeople("zhangsan");
System.out.println(b);
b.show();
Factory fa = new GirlFactory();
Girl g = (Girl) fa.getPeople("lisi");
System.out.println(g);
g.show();
Girl gg = (Girl) fa.getPeople("wangwu");
System.out.println(gg);
gg.show();
}
}
/*
*工厂方法模型就要解决的是有多个工厂,每一个工厂生产特定产品,都是在工厂里面产生对象
*以后凡是要增加其他类型的产品的时候就可以新建一个工厂继承父工厂,然后在新建产品类就
*行,这样很好的体现了Java的一个设计原则,开闭原则
* */
public class People
{
private String name;
private String sex;
public People(){}
public People(String name,String sex)
{
this.name = name;
this.sex = sex;
}
public void show(){}
public String toString()
{
return "Name: "+name+" Sex: "+sex;
}
}
package methodOfFactory;
public class Boy extends People {
public Boy(){}
public Boy(String name)
{
super(name,"male");
}
public void show(){}
}
package methodOfFactory;
public class ZhangSan extends Boy {
public ZhangSan()
{
super("zhang san");
}
public void show()
{
System.out.println("zhang san is my dota friend!");
}
}
package methodOfFactory;
public class Girl extends People {
public Girl(){}
public Girl(String name)
{
super(name,"male");
}
public void show(){}
}
package methodOfFactory;
public class LiSi extends Girl {
public LiSi()
{
super("li si");
}
public void show()
{
System.out.println("li si is my girl friend!");
}
}
package methodOfFactory;
public class WangWu extends Girl
{
public WangWu()
{
super("wang wu");
}
public void show()
{
System.out.println("wang wu is my wife!");
}
}
package methodOfFactory;
public interface Factory {
public abstract People getPeople(String name);
}
package methodOfFactory;
public class BoyFactory implements Factory {
@Override
public People getPeople(String name) {
if(name.equalsIgnoreCase("zhangsan"))
{
return new ZhangSan();
}
return null;
}
}
package methodOfFactory;
public class GirlFactory implements Factory
{
@Override
public People getPeople(String name)
{
if(name.equalsIgnoreCase("lisi"))
{
return new LiSi();
}else if(name.equalsIgnoreCase("wangwu"))
{
return new WangWu();
}
return null;
}
}
package methodOfFactory;
public class Main
{
public static void main(String[] args)
{
Factory f = new BoyFactory();
Boy b = (Boy) f.getPeople("zhangsan");
System.out.println(b);
b.show();
Factory fa = new GirlFactory();
Girl g = (Girl) fa.getPeople("lisi");
System.out.println(g);
g.show();
Girl gg = (Girl) fa.getPeople("wangwu");
System.out.println(gg);
gg.show();
}
}
/*
*工厂方法模型就要解决的是有多个工厂,每一个工厂生产特定产品,都是在工厂里面产生对象
*以后凡是要增加其他类型的产品的时候就可以新建一个工厂继承父工厂,然后在新建产品类就
*行,这样很好的体现了Java的一个设计原则,开闭原则
* */