1. 简单工厂模式
1.1 结构
1.2 代码示例
抽象产品类
public abstract class Product{
public abstract void operation();
}
具体产品类ProductA.class & ProductB.class
public class ProductA extends Product{
@Override
public void operation(){
System.out.println("创建产品A成功!");
}
}
public class ProductB extends Product{
@Override
public void operation(){
System.out.println("创建产品B成功!");
}
}
具体工厂类(static,静态工厂)
public class ProductFactory{
public static void createdProduct(String type){
if("A".equals(type){
return new ProductA();
}else if("B".equals(type){
return new ProductB();
}
return null;
}
}
客户端使用
public class Client(){
public static void main(String[] args){
Product a = ProductFactory.createdProduct("A");
a.operation();
Product b = ProductFactory.createdProduct("B");
b.operation()
}
}
2. 工厂方法模式
2.1 结构
2.2 代码示例
抽象产品类
public abstract class Product{
public abstract void operation();
}
具体产品类ProductA.class & ProductB.class
public class ProductA extends Product{
@Override
public void operation(){
System.out.println("创建产品A成功!");
}
}
public class ProductB extends Product{
@Override
public void operation(){
System.out.println("创建产品B成功!");
}
}
抽象工厂类
public interface AbstractFactory{
Product createProduct();
}
具体工厂类FactoryA & FactoryB
public class FactoryA implements AbstractFactory{
@Override
public Product createProduct(){
return new ProductA();
}
}
public class FactoryB implements AbstractFactory{
@Override
public Product createProduct(){
return new ProductB();
}
}
客户端使用
public class Client{
public static void main(String[] args){
Factory factoryA = new FactoryA();
Product productA = factoryA.createdProduct();
productA.operation();
Factory factoryB = new FactoryB();
Product productB = factoryB.createdProduct();
productA.operation();
}
}
3. 抽象工厂模式
3.1 结构
3.2 代码结构
抽象产品类 ProductA.java & ProductB.java
public interface ProductA(){
void operationA();
}
public interface ProductB(){
void perationB;
}
具体产品类 ProductA1.java & ProductA2.java & ProductB1.java & ProductB2.java
public class ProductA1 implements ProductA{
@Override
public void operationA(){
System.out.println("创建具体产品A1!");
}
}
public class ProductA2 implements ProductA{
@Override
public void operationA(){
System.out.println("创建具体产品A2!");
}
}
public class ProductB1 implements ProductB{
@Override
public void operationA(){
System.out.println("创建具体产品B1!");
}
}
public class ProductB2 implements ProductB{
@Override
public void operationA(){
System.out.println("创建具体产品B2!");
}
}
抽象工厂类
public interface AbstractFactory{
ProductA createProductA();
ProductB createProductB();
}
具体工厂类 Factory1.java & Factory2.java
public class Factory1 implements AbstractFactory{
@Override
public ProductA createProductA(){
return new ProductA1();
}
@Override
public ProductB createProductB(){
return new ProductB1();
}
}
public class Factory2 implements AbstractFactory{
@Override
public ProductA createProductA(){
return new ProductA2();
}
@Override
public ProductB createProductB(){
return new ProductB2();
}
}
客户端使用
public class Client{
public static void main(String[] args) {
AbstractFactory factory1 = new Factory1();
ProductA productA1 = factory1.createProductA();
ProductB productB1 = factory1.createProductB();
productA1.operationA();
productB1.operationB();
AbstractFactory factory2 = new Factory2();
ProductA productA2 = factory2.createProductA();
ProductB productB2 = factory2.createProductB();
productA2.operationA();
productB2.operationB();
}
}