java 23种设计模式解析-工厂方法
程序员文章站
2024-01-05 17:41:22
...
子类对象由工厂创建,
public abstract Factory {
abstract public Product factor();
}
public class FactoryA implements Factory {//生产对象A的工厂
@Override
public Product factor() {
return new ProductA();
}
}
public class FactoryB implements Factory {//生产对象A的工厂
@Override
public Product factor() {
return new ProductB();
}
}
public class ProductA implements Product{}//需求对象A
public class ProductB implements Product{} 需求对象B
public interface Product {
}
public class Test{
public static void main(String args[]){
Factory f=new FactoryA();
Product p= f.factor();
}
}