设计模式系列-3抽象工厂模式

抽象工厂模式是一种创建型设计模式,它能创建一系列相关的对象,而无需指定具体类。通过类图、代码实现及总结,本文详细介绍了抽象工厂模式的概念及其在Java中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

抽象工厂模式

工厂模式是用来创建对象的工厂,那么抽象工厂模式就是创建工厂的工厂。

类图

在这里插入图片描述

代码实现

  • 定义Color接口
package com.pattern.abstractfactory;

public interface Color {
	public void fill();
}
  • 实现三个具体的类
package com.pattern.abstractfactory;

public class Red implements Color {

	@Override
	public void fill() {
		// TODO Auto-generated method stub
		System.out.println("Red is filled.");
	}

}
package com.pattern.abstractfactory;

public class Green implements Color {

	@Override
	public void fill() {
		// TODO Auto-generated method stub
		System.out.println("Green is filled.");
	}

}
package com.pattern.abstractfactory;

public class Blue implements Color {

	@Override
	public void fill() {
		// TODO Auto-generated method stub
		System.out.println("Blue is filled.");
	}

}
  • 定义具体的Color工厂
package com.pattern.abstractfactory;

import com.pattern.factory.Shape;

public class ColorFactory extends AbstractFactory {

	@Override
	public Color getColor(String color) {
		// TODO Auto-generated method stub
		if (color == null) {
			return null;
		}
		
		if (color.equalsIgnoreCase("Red")) {
			return new Red();
		} else if(color.equalsIgnoreCase("Green")) {
			return new Green();
		} else if (color.equalsIgnoreCase("Blue")) {
			return new Blue();
		}
		
		return null;
	}

	@Override
	public Shape getShape(String shape) {
		// TODO Auto-generated method stub
		return null;
	}

}
  • 定义抽象工厂接口
package com.pattern.abstractfactory;

import com.pattern.factory.*;

public abstract class AbstractFactory {
	public abstract Color getColor(String color);
	public abstract Shape getShape(String shape);
}
  • 定义工厂产生器
package com.pattern.abstractfactory;

public class FactoryProducer {
	public static AbstractFactory getFactory(String choice) {
		if (choice.equalsIgnoreCase("Color")) {
			return new ColorFactory();
		} else if (choice.equalsIgnoreCase("Shape")) {
			return new ShapeFactory();
		}
		
		return null;
	}
}
  • 主类
package com.pattern.abstractfactory;

import com.pattern.factory.Shape;

public class FactoryDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		AbstractFactory shapeFactory = FactoryProducer.getFactory("shape");
		Shape circle = shapeFactory.getShape("circle");
		circle.draw();
		Shape square = shapeFactory.getShape("square");
		square.draw();
		Shape rectangle = shapeFactory.getShape("rectangle");
		rectangle.draw();
		
		AbstractFactory colorFactory = FactoryProducer.getFactory("color");
		Color red = colorFactory.getColor("red");
		red.fill();
		Color green = colorFactory.getColor("green");
		green.fill();
		Color blue = colorFactory.getColor("blue");
		blue.fill();
	}

}

总结

  • 抽象工厂设计模式是产生工厂的工厂,其类似于python中的元类,用于生产类的类。

参考

[1] 易百教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值