Java设计模式

这篇博客详细介绍了Java设计模式,包括创建型模式中的工厂方法、抽象工厂、生成器和原型,结构型模式如适配器、桥接、组合、装饰器等,以及行为型模式如责任链、命令、解释器等。通过具体的步骤和示例,帮助读者理解并掌握这些设计模式的应用。

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

一、创建型模式

工厂方法

工厂方法即Factory Method,是一种对象创建型模式。
工厂方法的目的是使得创建对象和使用对象是分离的,并且客户端总是引用抽象工厂和抽象产品:

主要解决:主要解决接口选择的问题。

步骤1:

创建一个接口:

//shape:形状
public interface Shape{
	void draw();
}

步骤2

创建实现接口的实体类

//rectangle:长方形
public class Rectangle implements Shape{
	@Override
	public void draw(){
		System.out.println("Inside Rectangle::draw() nethod.");
	}
}
//Square:正方形
public class Square implements Shape{
	@Override
	public void draw(){
		System.out.println("Inside Square::draw() method.");
	}
}
public class Circle implements Shape{
	@Override
	public void draw(){
		System.out.println("Inside Circle::draw() method.");
	}
}

步骤3

创建一个工厂,生产基于给定信息的实体类的对象

public class ShapeFactory{
	//使用getShape方法获取形状类型的对象
	public Shape getShape(String shapeType){
		if(shapeType==null){
			return null;
		}
		if(shapeType.equalsIgnoreCase("CIRCLE")){
			return new Circle();
		}else if(shapeType.equalsIgnoreCase("RECTANGLE")){
			return new Rectangle();
		}else if(shapeType.equalsIgnoreCse("SQUARE")){
			return new Square();
		}
		return null;
	}
}

步骤4

使用该工厂,通过传递类型信息来获取实体类的对象。

public class FactoryPatternDemo{
	public static void main(){
		ShapeFactory shapeFactory=new ShapeFactory();
		//获取Circle的对象,并调用它的draw方法
		Shape shape1=shapeFactory.getSahpe("CIRCLE");
		//调用Circle的draw方法
		shape1.draw();
		//获取Recatangle的对象,并调用它的draw方法
		Shape shape2=shapeFactory.getShape("RECTANGLE");
		//调用Rectangle的draw方法
		shape2.draw();
		//获取Square的对象,并调用它的draw方法
		Shape shape3=shapeFactory.getShape("SQUARE");
		//调用Square的deaw方法
		shape3.draw();
	}
}

步骤5

执行程序员,输出结果:
Inside Circle::draw() method. Inside Rectangle::draw() method. Inside Square::draw() method.

抽象工厂

抽象工厂模式(Abstract Factory Patterm)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂弄湿提供对象。

步骤1

//为形成创建一个接口
public interface Shape{
	void draw();
}

步骤2

//创建实现接口的实体类
public class Rectangle implements Shape{
	@Override
	public void draw(){
		System.out.println("Inside Rectangle::draw() method.");
	}
}
public class Square implements Shape{
	@Override
	public void draw(){
		System.out.println("Inside Square::draw() method.");
	}
}
public class Circle implements Shape{
	@Override
	public void draw(){
		System.out.println("Inside Circle::draw() method.");
	}
}

步骤3

为颜色创建一个接口

public interface Color{
	void fill();
}

步骤4

创建实现接口的实体类

public class Red implements Color{
	@Override
	public void fill(){
		System.out.println("Inside Red::fill() method.");
	}
}
public class Green implements Color{
	@Override
	public void fill(){
		System.out.println("Inside Green::fill() method.");
	}
}
public class Blue implements Color{
	@Override
	public void fill(){
		System.out.println("Inside Blue::fill() method.");
	}
}

步骤5

为Color和Shape对象创建抽象类来获取工厂。

public abstract class AbstractFactory{
	public abstract Color getColor(String color);
	public abstract Shape getShape(String shape);
}

步骤6

创建扩展了AbstractFactory的工厂类,基于给定的信息生成实体类的对象

public class ShapeFactory extends AbstractFactory{
	@Override
	public Shape getShape(String shapeType){
		if(shapeType==null){
			return null;
		}
		if(shapeType.equalsIgnoreCase("CIRCLE")){
			return new Circle();
		}else if(shapeType.equalsIgnoreCase("RECTANGLE")){
			return new Rectangle();
		}else if(shapeType.equalsIgnoreCase("SQUARE")){
			return new Square();
		}
		return null;
	}
	@Override
	public Color getColor(String color){
		return null;
	}
}
public class ColorFactory extends AbstractFactory{
	@Override
	public Shape getShape(String shapeType){
		return null;
	}
	@Override
	public Color getColor(String color){
		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;
	}
}

步骤7

创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂

public class FactoryProducer{
	public static AbstractFactory getFactory(String choice){
		if(choice.equalsIgnoreCase("SHAPE")){
			return new ShapeFactory();
		}else if(choice.equalsIgnoreCase("COLOR")){
			return new ColorFactory();
		}
		return null;
	}
}

步骤8

使用FactoryProducer来获取AbstractFactory,通过传递类型信息来获取实体类的对象。

public class AbstractFactoryPatternDemo{
	public static void main(String[] args){
		//获取形状工厂
		AbstractFactory shapeFactory=FactoryProducer.getFactory("SHAPE");
		//获取形状为Circle的对象
		Shape shape1=shapeFactory.getShape("CIRCLE");
		//调用Circle的draw方法
		shape1.draw();
	}
}

生成器

原型

单例

二、结构型模式

适配器

桥接

组合

装饰器

外观

享元

代理

三、行为型模式

责任链

命令

解释器

迭代器

中介

备忘录

观察者

状态

策略

模板方法

访问者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值