抽象工厂模式
从设计层面上看,抽象工厂模式就是对简单工厂模式的改进或者进一步的抽象。将工厂抽象成两层,AbstractFactory 和具体的实现工厂子类。程序员可以根据句创建对象类型使用对应的工厂子类。这样可以将简单工厂变成工厂簇,更利于代码的维护和扩展
上面都是抄的,我理解的意思就是它实际跟工厂方法差不多。这个抽象方法实际就是将工厂方法模式和简单工厂的一个整合。其他没有什么特别之处,与工厂方法模式比较。唯一的好处我个人理解就是少了继承。片面的讲,它满足了七大原则中的 合成复用原则
- 抽象工厂模式定义了一个 interface 用于创建相关或有依赖关系的对象簇,而无需指明具体的类
- 抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合
例子:
public abstract class Fresh {
protected String name;
public abstract void prepare();
public void washing() {
System.out.println(name + "正在清洗");
}
public void cut() {
System.out.println(name + "正在分切");
}
public void box() {
System.out.println(name + "正在打包");
}
}
public class Panda extends Fresh {
public Panda() {
super.name = "鲜活大熊猫";
}
@Override
public void prepare() {
System.out.println(super.name + "正在调货");
}
}
还有三个实体类与 Panda 实体类内容一致,就不贴了
public interface Factory {
Fresh createFresh(String type);
}
public class CDFactory implements Factory {
@Override
public Fresh createFresh(String type) {
System.out.println("成都工厂");
if ("panda".equals(type)) {
return new Panda();
} else if ("fungi".equals(type)) {
return new Fungi();
}
return null;
}
}
public class GZFactory implements Factory {
@Override
public Fresh createFresh(String type) {
System.out.println("广州工厂");
if ("octopus".equals(type)) {
return new Panda();
} else if ("crab".equals(type)) {
return new Fungi();
}
return null;
}
}
public class Boxhorse {
Factory factory;
public Boxhorse(Factory factory){
this.factory = factory;
}
public void shopping() {
if (null == factory) {
System.out.println("工厂未实例化");
return;
}
do {
Fresh fresh = factory.createFresh(this.getType());
if (null != fresh) {
fresh.prepare();
fresh.washing();
fresh.cut();
fresh.box();
} else {
System.out.println("不卖了");
break;
}
} while (true);
}
private String getType() {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("input pizza type: ");
try {
return bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
public class Main {
public static void main(String[] args) {
Factory cd = new CDFactory();
Factory gz = new GZFactory();
new Boxhorse(cd).shopping();
System.out.println("=============================================");
new Boxhorse(gz).shopping();
}
}
不足之处还望指出
抽象方法模式结合单例
这个模式我们也结合我们的单例模式完成一个工厂认证,实体类与上面的使用的是同一个,下面只贴不一样的代码
public interface Factory {
/**
* 创建工厂
* @param type
* @return
*/
Fresh createFresh(String type);
/**
* 获取认证材料
* @return
*/
String getAuth();
/**
* 获取厂名
* @return
*/
String getFactoryName();
}
enum Authorization {
INSTANCE;
List<String> authorized = new ArrayList<>();
List<String> certificate = new ArrayList<>(Arrays.asList("A1", "A2"));
/**
* 取得证书
* @return 证书
*/
String getAuth() {
String auth = null;
if (certificate.size() > 0) {
auth = certificate.get(0);
authorized.add(auth);
certificate.remove(0);
}
return auth;
}
/**
* 检查证书
* @param auth 证书
* @return 是否存在
*/
boolean check(String auth) {
return authorized.contains(auth);
}
}
public class CDFactory implements Factory {
String auth = null;
String factoryName = "成都工厂";
public CDFactory() {
this.auth = Authorization.INSTANCE.getAuth();
}
@Override
public Fresh createFresh(String type) {
System.out.println(factoryName);
if ("panda".equals(type)) {
return new Panda();
} else if ("fungi".equals(type)) {
return new Fungi();
}
return null;
}
@Override
public String getAuth() {
System.out.println("正在获取" + factoryName + "的证书");
return auth;
}
@Override
public String getFactoryName() {
return factoryName;
}
}
public class GZFactory implements Factory {
public String auth = null;
public String factoryName = "广州工厂";
public GZFactory() {
this.auth = Authorization.INSTANCE.getAuth();
}
@Override
public Fresh createFresh(String type) {
System.out.println(factoryName);
if ("octopus".equals(type)) {
return new Panda();
} else if ("crab".equals(type)) {
return new Fungi();
}
return null;
}
@Override
public String getAuth() {
System.out.println("正在获取" + factoryName + "的证书");
return auth;
}
@Override
public String getFactoryName() {
return factoryName;
}
}
public class ZLFactory implements Factory {
String auth = null;
String factoryName = "脏乱工厂";
public ZLFactory() {
this.auth = Authorization.INSTANCE.getAuth();
}
@Override
public Fresh createFresh(String type) {
System.out.println(factoryName);
return null;
}
@Override
public String getAuth() {
System.out.println("正在获取" + factoryName + "的证书");
return auth;
}
@Override
public String getFactoryName() {
return factoryName;
}
}
public class Boxhorse {
Factory factory;
public Boxhorse(Factory factory){
// 检查证书
if (Authorization.INSTANCE.check(factory.getAuth())) this.factory = factory;
}
public void shopping() {
if (null == factory) {
System.out.println("工厂没有证书");
return;
}
do {
Fresh fresh = factory.createFresh(this.getType());
if (null != fresh) {
fresh.prepare();
fresh.washing();
fresh.cut();
fresh.box();
} else {
System.out.println("不卖了");
break;
}
} while (true);
}
private String getType() {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("input pizza type: ");
try {
return bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
public class Main {
public static void main(String[] args) {
Factory cd = new CDFactory();
Factory gz = new GZFactory();
Factory zl = new ZLFactory();
new Boxhorse(cd).shopping();
System.out.println("=============================================");
new Boxhorse(gz).shopping();
System.out.println("=============================================");
new Boxhorse(zl).shopping();
}
}