一、大致结构
1、抽象产品类
2、抽象工厂类
3、具体工厂类
4、具体产品类
5、静态工厂类
二、代码(模拟买东西送东西的场景)
1、抽象产品类
MainProduct.java
package com.abstractfactory;
public abstract class MainProduct {
public abstract void setName(String name);
public abstract String getName();
}
FreeProduct.java
package com.abstractfactory;
public abstract class FreeProduct {
public abstract void setName(String name);
public abstract String getName();
}
2、抽象工厂类
AbstractFactory.java
package com.abstractfactory;
public abstract class AbstractFactory {
public abstract MainProduct createMainProduct();
public abstract FreeProduct createFreeProduct();
}
3、具体产品类
Computer.java
package com.abstractfactory;
/**
* 电脑,附赠品是鼠标垫
* @author may
*
*/
public class Computer extends MainProduct {
private String name;
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
Mousepad.java
package com.abstractfactory;
/**
* 鼠标垫,电脑的附赠品
* @author may
*
*/
public class Mousepad extends FreeProduct {
private String name;
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
Barbecue.java
package com.abstractfactory;
/**
* 烧烤,附赠品是水
* @author may
*
*/
public class Barbecue extends MainProduct {
private String name;
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
Water.java
package com.abstractfactory;
public class Water extends FreeProduct {
//品牌名
private String name;
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
4、具体工厂类
BarbecueFactory.java
package com.abstractfactory;
/**
* 买烧烤,送水
* @author may
*
*/
public class BarbecueFactory extends AbstractFactory {
@Override
public MainProduct createMainProduct() {
return new Barbecue() ;
}
@Override
public FreeProduct createFreeProduct() {
return new Water();
}
}
ComputerFactory.java
package com.abstractfactory;
/**
* 买台电脑,送鼠标垫
* @author may
*
*/
public class ComputerFactory extends AbstractFactory {
@Override
public MainProduct createMainProduct() {
return new Computer();
}
@Override
public FreeProduct createFreeProduct() {
return new Mousepad();
}
}
5、配置文件
FactoryList.properties
ComputerFactory=com.abstractfactory.ComputerFactory
BarbecueFactory=com.abstractfactory.BarbecueFactory
6、加载配置类
PropertiesUtil.java
package com.abstractfactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class PropertiesUtil {
private static Properties props = null;
static {
props = new Properties();
try {
props.load(PropertiesUtil.class.getResourceAsStream("/FactoryList.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getProperty(String name) {
return props.getProperty(name);
}
public static void getFactoryList() {
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(
PropertiesUtil.class.getResourceAsStream("/FactoryList.properties"), "utf-8"));
String line = in.readLine();
while (line != null) {
line = line.substring(0, line.indexOf("="));
System.out.println(line);
line = in.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
7、静态工厂类
CreateFactory.java
package com.abstractfactory;
public class CreateFactory {
public static AbstractFactory createFactory(String name) {
String className = PropertiesUtil.getProperty(name);
AbstractFactory factory = null;
try {
factory = (AbstractFactory) Class.forName(className).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return factory;
}
}
8、测试类
Customer.java
package com.abstractfactory;
public class Customer {
public static void main(String[] args) {
AbstractFactory factory = CreateFactory.createFactory("ComputerFactory");
MainProduct mainProduct = factory.createMainProduct();
mainProduct.setName("华硕笔记本电脑");
FreeProduct freeProduct = factory.createFreeProduct();
freeProduct.setName("超级鼠标垫");
System.out.println("你购买了" + mainProduct.getName() + "附赠你" + freeProduct.getName() + "一个");
}
}