标签:actor 子类 print extends 创建 return system ati 实例化
工厂方法模式:定义一个用于创建对象的接口,让子类去决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。
1.抽象工厂
public interface PeopleFactory{
public People createPeople();
}
2.抽象产品和具体实现类。
public abstract class People{
public abstract void doSth();
}
public class Man extends People {
@Override
public void doSth() {
System.out.println("I‘m a man,I‘m coding.");
}
}
public class Girl extends People {
@Override
public void doSth() {
System.out.println("I‘m a girl,I‘m eating.");
}
}
3.具体工厂,用来具体实现哪个产品。
public class ManFactory implements PeopleFactory{
@Override
public People createPeople() {
return new Man();
}
}
public class GirlFactory implements PeopleFactory{
@Override
public People createPeople() {
return new Girl();
}
}
标签:actor 子类 print extends 创建 return system ati 实例化
原文地址:http://www.cnblogs.com/emoji1213/p/7616095.html