创建型设计模式,主要用于在程序中代替直接用new 操作生成对象。根据需要生成合适的对象,操作灵活。
工厂模式有几种常见类型:简单工厂模式、抽象工厂模式、工厂方法模式
简单工厂模式:
不直接使用new创建对象。不是严格意义上的设计模式,但是一个好的办法,可以将客户程序从具体的类里面解耦。
工厂方法模式:
使用类的继承。通过覆盖父类的抽象工厂方法来创建自己的产品。通常用来创建一种产品。
抽象工厂模式:
使用接口。通常用来创建产品族或者产品组合。在接口里面统一声明需要创建的方法,返回合适的对象值。
类图:

实现:
package Factory;
public class FootedGlassCupProduct extends Product {
PropertyFactory propertyFactory;
public FootedGlassCupProduct(PropertyFactory propertyFactory) {
name="矮脚杯";
description="用来喝茶吧";
this.propertyFactory=propertyFactory;
}
@Override
void prepare() {
properties=this.propertyFactory.initProperty();
}
}
package Factory;
import java.util.HashMap;
import java.util.Map;
public class FootedGlassCupPropertyFactory implements PropertyFactory {
@Override
public Map<String, String> initProperty() {
Map<String,String> footedGlassProperty=new HashMap<String,String>();
footedGlassProperty.put("material","glass");
footedGlassProperty.put("height","25");
footedGlassProperty.put("circle","15");
return footedGlassProperty;
}
}
package Factory;
public class GlassProductStore extends ProductStore {
@Override
public Product orderProduct(String name) {
Product product;
product=createProduct(name);
return product;
}
@Override
public Product createProduct(String name) {
if("高脚杯".equals(name)){
return new TallGlassCupProduct(new TallGlassCupPropertyFactory());
}else if("矮脚杯".equals(name)){
return new FootedGlassCupProduct(new FootedGlassCupPropertyFactory());
}
return null;
}
}
package Factory;
import java.util.Map;
public abstract class Product {
String name;
String description;
Map<String,String> properties;
void blanking(){
System.out.println("下料");
if(properties!=null){
for(Map.Entry entry:properties.entrySet()){
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
}
System.out.println("下料完成");
}
public void assemble(){
System.out.println("成型");
}
abstract void prepare();
}
package Factory;
public abstract class ProductStore {
public abstract Product orderProduct(String name);
public abstract Product createProduct(String name);
}
package Factory;
import java.util.Map;
public interface PropertyFactory {
public Map<String,String> initProperty();
}
package Factory;
public class TallGlassCupProduct extends Product {
PropertyFactory propertyFactory;
public TallGlassCupProduct(PropertyFactory propertyFactory) {
name="高脚杯";
description="用来喝酒吧";
this.propertyFactory=propertyFactory;
}
@Override
void prepare() {
properties=this.propertyFactory.initProperty();
}
}
package Factory;
import java.util.HashMap;
import java.util.Map;
public class TallGlassCupPropertyFactory implements PropertyFactory {
@Override
public Map<String, String> initProperty() {
Map<String,String> footedGlassProperty=new HashMap<String,String>();
footedGlassProperty.put("material","glass");
footedGlassProperty.put("height","25");
footedGlassProperty.put("circle","15");
return footedGlassProperty;
}
}
package Factory;
public class TestFactory {
public static void test(){
WoodProductStore woodProductStore=new WoodProductStore();
Product product=woodProductStore.orderProduct("木碗");
product.prepare();
product.blanking();
product.assemble();
}
}
package Factory;
public class WoodBowlProduct extends Product {
PropertyFactory propertyFactory;
public WoodBowlProduct(PropertyFactory propertyFactory) {
name="木碗";
description="保温健康,不易碎";
this.propertyFactory= propertyFactory;
}
@Override
void prepare() {
properties=this.propertyFactory.initProperty();
}
}
package Factory;
import java.util.HashMap;
import java.util.Map;
public class WoodBowlPropertyFactory implements PropertyFactory {
@Override
public Map<String, String> initProperty() {
Map<String,String> footedGlassProperty=new HashMap<String,String>();
footedGlassProperty.put("material","wood");
footedGlassProperty.put("height","8");
footedGlassProperty.put("top_circle","20");
footedGlassProperty.put("bottom_circle","10");
return footedGlassProperty;
}
}
package Factory;
public class WoodPlateProduct extends Product {
PropertyFactory propertyFactory;
public WoodPlateProduct(PropertyFactory propertyFactory) {
name="木盘";
description="用来盛放水果不错";
this.propertyFactory=propertyFactory;
}
@Override
void prepare() {
properties=this.propertyFactory.initProperty();
}
}
package Factory;
import java.util.HashMap;
import java.util.Map;
public class WoodPlatePropertyFactory implements PropertyFactory {
@Override
public Map<String, String> initProperty() {
Map<String,String> footedGlassProperty=new HashMap<String,String>();
footedGlassProperty.put("material","wood");
footedGlassProperty.put("height","4");
footedGlassProperty.put("top_circle","30");
footedGlassProperty.put("bottom_circle","20");
footedGlassProperty.put("flower_decorate","F20");
return footedGlassProperty;
}
}
package Factory;
public class WoodProductStore extends ProductStore {
@Override
public Product orderProduct(String name) {
Product product;
product=createProduct(name);
return product;
}
@Override
public Product createProduct(String name) {
if("木碗".equals(name)){
return new WoodBowlProduct(new WoodBowlPropertyFactory());
}else if("木盘".equals(name)){
return new WoodPlateProduct(new WoodPlatePropertyFactory());
}
return null;
}
}
网友评论