设计模式 工厂模式 分为哪几种?
发布网友
发布时间:2022-04-12 22:22
我来回答
共2个回答
热心网友
时间:2022-04-12 23:52
工厂方法(Factory Method),和抽象工厂(Abstract Factory),他们的不同之处主要是抽象工厂就是对多个工厂方法的实现,这点可以从他们的实现代码看出来。
Factory Method:
public class Factory{
public static Sample creator(int which){
//getClass 产生Sample 一般可使用动态类装载装入类。
if (which==1)
return new SampleA();
else if (which==2)
return new SampleB();
}
}
Abstract Factory:
public abstract class Factory{
public abstract Sample creator();
public abstract Sample2 creator(String name);
}
public class SimpleFactory extends Factory{
public Sample creator(){
.........
return new SampleA
}
public Sample2 creator(String name){
.........
return new Sample2A
}
}
public class BombFactory extends Factory{
public Sample creator(){
......
return new SampleB
}
public Sample2 creator(String name){
......
return new Sample2B
}
}
热心网友
时间:2022-04-13 01:10
ggggggggggggggggggggggggggggggggg