软件构造|工厂模式 装饰器模式

工厂模式

在工厂模式中,我们定义了一个用于创建对象的接口,让子类去决定要进行实例化的类,延迟了类的实例化。

代码实例:

// 抽象产品
public interface Fruit {
	public void display();
}

// 抽象产品
public class Apple implements Fruit{
	@Override
	public void display() {
		System.out.println("this is an apple");
	}
}

// 具体产品
public class Orange implements Fruit {
	@Override
	public void display() {
		System.out.println("this is an orange");
	}
}
// 抽象工厂
public interface FruitFactory {
	public Fruit getFruit(String info) ;
}
// 具体工厂
public class GoodFruitFactory implements FruitFactory {
	@Override
	public Fruit getFruit(String info) {
		switch (info) {
		case "apple":
			return new Apple();
		case "orange":
			return new Orange();
		default:
			throw new RuntimeException();
		}
	}
}

如上,我们定义了抽象产品约束产品的共同特征,实现了两个具体产品。在具体工厂中,我们根据输入的字符串,返回相应的对象。具体使用为:

public class Main {
	public static void main(String[] args) {
		Fruit apple = new GoodFruitFactory().getFruit("apple");
		Fruit orange = new GoodFruitFactory().getFruit("orange");
		
		apple.display();
		orange.display();
	}
}
/*
 * 具体输出为:
this is an apple
this is an orange
*
*/

上面的代码中,我们观察到,我们利用GoodFruitFactory()getFruit函数,通过不同的字符串,获取到了不同的Fruit子类型。

方便起见,我们还可以修改工厂模式为静态工厂模式。

public interface FruitFactory {
	public static Fruit getFruit(String info)  {
		switch (info) {
		case "apple":
			return new Apple();
		case "orange":
			return new Orange();
		default:
			throw new RuntimeException();
		}
	}
}

此时使用方式为:

Fruit apple = FruitFactory.getFruit("apple");
Fruit orange = FruitFactory.getFruit("orange");
apple.display();
orange.display();

类似于Integer.valueOf()的函数调用。

上述代码的类图如下:
在这里插入图片描述

装饰器模式

装饰器模式中,我们使用装饰器来装饰组件,使之具有新的不同的属性。具体组件和抽象装饰器都实现了抽象组件接口。具体组件初始是不加装饰的基类,而抽象装饰器中一般会委托一个抽象构件并对其进行装饰。具体装饰器继承了抽象装饰器,负责装饰特定的属性。

// 抽象组件
public interface Component {
	public void color();
}
// 具体组件

public class ConcreteComponent implements Component{
	public void color() {
		System.out.println();
	}
}


public class Decorator implements Component {
	private final Component component;
	
	public Decorator(Component component) {
		this.component = component;
	}
	
	@Override
	public void color() {
		component.color();
	}
}

/// 具体装饰器

public class ConcreteDecoratorRed extends Decorator {

	public ConcreteDecoratorRed(Component component) {
		super(component);
	}
	
	@Override
	public void color() {
		super.color();
		System.out.println("red");
	}
}

// 具体装饰器

public class ConcreteDecoratorGreen extends Decorator {

	public ConcreteDecoratorGreen(Component component) {
		super(component);
	}
	

	public void color() {
		super.color();
		System.out.println("green");
	}
}

具体使用:

public static void main(String[] args) {
		Component someColor = new ConcreteDecoratorRed(new ConcreteDecoratorGreen (new ConcreteComponent()));
		someColor.color();
	}


输出为:


green
red

类之间的关系为:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值