赞
踩
工厂模式分三种:简单工厂模式、工厂方法模式、抽象工厂模式
有四种产品ProductA1、ProductA2、ProductB1、ProductB2,假设ProductA1与ProductA2属于一类,ProductB1、ProductB1属于一类,使用简单工厂模式、工厂方法模式、抽象工厂模式实现。
package com.mine.design.factory.simple;
public interface IProduct {
void produce();
}
package com.mine.design.factory.simple;
public class ProductA1 implements IProduct {
public void produce() {
System.out.println("生成产品A1");
}
}
package com.mine.design.factory.simple;
public class ProductA2 implements IProduct {
public void produce() {
System.out.println("生成产品A2");
}
}
package com.mine.design.factory.simple;
public class ProductB1 implements IProduct {
public void produce() {
System.out.println("生产产品B1");
}
}
package com.mine.design.factory.simple; public class SimpleFactory { public static IProduct getProduct(String type) { IProduct product = null; if ("1".equals(type)) { product = new ProductA1(); product.produce(); } else if ("2".equals(type)) { product = new ProductA2(); product.produce(); } else if ("3".equals(type)) { product = new ProductB1(); product.produce(); } else if ("4".equals(type)) { product = new ProductB2(); product.produce(); } return product; } }
package com.mine.design.factory.simple; public class Client { public static void main(String[] args) { // 生产产品A SimpleFactory.getProduct("1"); // 生产产品A2 SimpleFactory.getProduct("2"); // 生产产品B1 SimpleFactory.getProduct("3"); // 生产产品B2 SimpleFactory.getProduct("4"); } }
product相关类参考简单工厂模式
package com.mine.design.factory.method;
import com.mine.design.factory.simple.IProduct;
public interface IFactory {
IProduct getProduct();
}
package com.mine.design.factory.method;
import com.mine.design.factory.simple.IProduct;
import com.mine.design.factory.simple.ProductA1;
public class FactoryA1 implements IFactory {
public IProduct getProduct() {
ProductA1 product = new ProductA1();
product.produce();
return product;
}
}
package com.mine.design.factory.method;
import com.mine.design.factory.simple.IProduct;
import com.mine.design.factory.simple.ProductA2;
public class FactoryA2 implements IFactory {
public IProduct getProduct() {
ProductA2 product = new ProductA2();
product.produce();
return product;
}
}
package com.mine.design.factory.method;
import com.mine.design.factory.simple.IProduct;
import com.mine.design.factory.simple.ProductB1;
public class FactoryB1 implements IFactory {
public IProduct getProduct() {
ProductB1 product = new ProductB1();
product.produce();
return product;
}
}
package com.mine.design.factory.method;
import com.mine.design.factory.simple.IProduct;
import com.mine.design.factory.simple.ProductB2;
public class FactoryB2 implements IFactory {
public IProduct getProduct() {
ProductB2 product = new ProductB2();
product.produce();
return product;
}
}
package com.mine.design.factory.abs;
import com.mine.design.factory.simple.IProduct;
public interface IFactory {
IProduct getProduct(String type);
}
package com.mine.design.factory.abs; import com.mine.design.factory.simple.IProduct; import com.mine.design.factory.simple.ProductA1; import com.mine.design.factory.simple.ProductA2; public class FactoryA implements IFactory { public IProduct getProduct(String type) { IProduct product = null; if ("1".equals(type)) { product = new ProductA1(); product.produce(); } else if ("2".equals(type)) { product = new ProductA2(); product.produce(); } return product; } }
package com.mine.design.factory.abs; import com.mine.design.factory.simple.IProduct; import com.mine.design.factory.simple.ProductB1; import com.mine.design.factory.simple.ProductB2; public class FactoryB implements IFactory { public IProduct getProduct(String type) { IProduct product = null; if ("1".equals(type)) { product = new ProductB1(); product.produce(); } else if ("2".equals(type)) { product = new ProductB2(); product.produce(); } return product; } }
package com.mine.design.factory.abs; public class Client { public static void main(String[] args) { // 生产产品A new FactoryA().getProduct("1"); // 生产产品A2 new FactoryA().getProduct("2"); // 生产产品B1 new FactoryB().getProduct("1"); // 生产产品B2 new FactoryB().getProduct("2"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。