package com.lonton.designpatterns;
interface Human
{
public void Talk();
public void Walk();
}
class Boy implements Human
{
@Override
public void Talk()
{
System.out.println("Boy is talking...");
}
@Override
public void Walk()
{
// TODO Auto-generated method stub
System.out.println("Boy is walking...");
}
}
class Girl implements Human
{
@Override
public void Talk()
{
// TODO Auto-generated method stub
System.out.println("Girl is talking...");
}
@Override
public void Walk()
{
// TODO Auto-generated method stub
System.out.println("Girl is walking...");
}
}
public class Factory
{
public static Human createHuman(String sexy)
{
Human human = null;
if ("boy" == sexy)
{
human = new Boy();
}
else if ("girl" == sexy)
{
human = new Girl();
}
return human;
}
/**
* @param args
*/
public static void main(String[] args)
{
Human human = Factory.createHuman("girl");
human.Talk();
human.Walk();
}
}
Factory 工厂模式
最新推荐文章于 2024-04-25 09:25:45 发布