抽象工厂模式(Abstract Factory Pattern)是相当于一个超级工厂,用来创建其他工厂。
一、需求举例:
项目中需求开发应用场景:
1.工程师工厂负责生成初级工程师,中级工程师和高级工程师,分别负责完成简单项目,中等难度项目和高难度项目开发;
2.产品经理工厂负责生成初级产品经理,中级产品经理和高级产品经理,分别负责完成简单项目规划,中等难度项目规划和高难度项目规划;
3.初级工程师完成初级产品经理的项目规划,中级工程师完成中级产品经理的项目规划,高级工程师完成高级产品经理的项目规划。
二、类图:
三、代码实例:
1.工程师类
1.1 工程师接口:Engineer
package com.lance.abstractfactory.engineer;
public interface Engineer {
void develop(String program);
}
1.2.初级工程师类:JuniorEngineer
package com.lance.abstractfactory.engineer;
public class JuniorEngineer implements Engineer {
@Override
public void develop(String program) {
System.out.println("Junior Engineer develop " + program);
}
}
1.3.中级工程师类:
package com.lance.abstractfactory.engineer;
public class IntermediateEngineer implements Engineer {
@Override
public void develop(String program) {
System.out.println("Intermediate Engineer develop " + program);
}
}
1.4.高级工程师类:SeniorEngineer
package com.lance.abstractfactory.engineer;
import com.lance.abstractfactory.engineer.Engineer;
public class SeniorEngineer implements Engineer {
@Override
public void develop(String program) {
System.out.println("Senior Engineer develop " + program);
}
}
2.产品经理类
2.1 产品经理接口:ProductManager
package com.lance.abstractfactory.productmanager;
public interface ProductManager {
void plan(String program);
}
2.2 初级产品经理:JuniorProductManager
package com.lance.abstractfactory.productmanager;
public class JuniorProductManager implements ProductManager{
@Override
public void plan(String program) {
System.out.println("Junior Product Manager plan " + program);
}
}
2.3 中级产品经理:IntermediateProductManager
package com.lance.abstractfactory.productmanager;
public class IntermediateProductManager implements ProductManager{
@Override
public void plan(String program) {
System.out.println("Intermediate Product Manager plan " + program);
}
}
2.4 高级产品经理: SeniorProductManager
package com.lance.abstractfactory.productmanager;
public class SeniorProductManager implements ProductManager{
@Override
public void plan(String program) {
System.out.println("Senior Product Manager plan " + program);
}
}
3.工厂类
3.1 抽象工厂接口:AbstractFactory
package com.lance.abstractfactory.factory;
import com.lance.abstractfactory.productmanager.ProductManager;
import com.lance.abstractfactory.engineer.Engineer;
public abstract class AbstractFactory {
public abstract Engineer getEngineer();
public abstract ProductManager getProductManager();
}
3.2 初级工厂:JuniorFactory
package com.lance.abstractfactory.factory;
import com.lance.abstractfactory.engineer.Engineer;
import com.lance.abstractfactory.engineer.JuniorEngineer;
import com.lance.abstractfactory.productmanager.JuniorProductManager;
import com.lance.abstractfactory.productmanager.ProductManager;
public class JuniorFactory extends AbstractFactory{
@Override
public Engineer getEngineer() {
return new JuniorEngineer();
}
@Override
public ProductManager getProductManager() {
return new JuniorProductManager();
}
}
3.3 中级工厂:IntermediateFactory
package com.lance.abstractfactory.factory;
import com.lance.abstractfactory.engineer.Engineer;
import com.lance.abstractfactory.engineer.IntermediateEngineer;
import com.lance.abstractfactory.productmanager.IntermediateProductManager;
import com.lance.abstractfactory.productmanager.ProductManager;
public class IntermediateFactory extends AbstractFactory {
@Override
public Engineer getEngineer() {
return new IntermediateEngineer();
}
@Override
public ProductManager getProductManager() {
return new IntermediateProductManager();
}
}
3.4 高级工厂:SeniorFactory
package com.lance.abstractfactory.factory;
import com.lance.abstractfactory.engineer.Engineer;
import com.lance.abstractfactory.engineer.SeniorEngineer;
import com.lance.abstractfactory.productmanager.ProductManager;
import com.lance.abstractfactory.productmanager.SeniorProductManager;
public class SeniorFactory extends AbstractFactory {
@Override
public Engineer getEngineer() {
return new SeniorEngineer();
}
@Override
public ProductManager getProductManager() {
return new SeniorProductManager();
}
}
4.主类:AbstractFactoryPattern
package com.lance.abstractfactory;
import com.lance.abstractfactory.engineer.Engineer;
import com.lance.abstractfactory.factory.AbstractFactory;
import com.lance.abstractfactory.factory.IntermediateFactory;
import com.lance.abstractfactory.factory.JuniorFactory;
import com.lance.abstractfactory.factory.SeniorFactory;
import com.lance.abstractfactory.productmanager.ProductManager;
public class AbstractFactoryPattern {
public static void main(String[] args) {
System.out.println("==========start==========");
String program = "easy program";
AbstractFactory factory = new JuniorFactory();
ProductManager productManager = factory.getProductManager();
productManager.plan(program);
Engineer engineer = factory.getEngineer();
engineer.develop(program);
System.out.println("=======================");
program = "middle program";
factory = new IntermediateFactory();
productManager = factory.getProductManager();
productManager.plan(program);
engineer = factory.getEngineer();
engineer.develop(program);
System.out.println("=======================");
program = "difficult program";
factory = new SeniorFactory();
productManager = factory.getProductManager();
productManager.plan(program);
engineer = factory.getEngineer();
engineer.develop(program);
System.out.println("==========end==========");
}
}
输出结果:
==========start==========
Junior Product Manager plan easy program
Junior Engineer develop easy program
=======================
Intermediate Product Manager plan middle program
Intermediate Engineer develop middle program
=======================
Senior Product Manager plan difficult program
Senior Engineer develop difficult program
==========end==========
网友评论