java 设计模式 示例_Java示例中的装饰器设计模式

java 设计模式 示例

Decorator design pattern is used to modify the functionality of an object at runtime. At the same time other instances of the same class will not be affected by this, so individual object gets the modified behavior. Decorator design pattern is one of the structural design pattern (such as Adapter Pattern, Bridge Pattern, Composite Pattern) and uses abstract classes or interface with composition to implement.

装饰器设计模式用于在运行时修改对象的功能。 同时,同一类的其他实例将不受此影响,因此单个对象将获得修改后的行为。 装饰器设计模式是结构设计模式之一(例如Adapter PatternBridge PatternComposite Pattern ),并使用抽象类或带有组合的接口来实现。

装饰设计模式 (Decorator Design Pattern)

We use inheritance or composition to extend the behavior of an object but this is done at compile time and its applicable to all the instances of the class. We can’t add any new functionality of remove any existing behavior at runtime – this is when Decorator pattern comes into picture.

我们使用继承或组合来扩展对象的行为,但这是在编译时完成的,并且适用于该类的所有实例。 我们无法添加任何新功能来删除运行时的任何现有行为-这是装饰器模式出现的时候。

Suppose we want to implement different kinds of cars – we can create interface Car to define the assemble method and then we can have a Basic car, further more we can extend it to Sports car and Luxury Car. The implementation hierarchy will look like below image.

假设我们要实现不同类型的汽车-我们可以创建接口Car来定义组装方法,然后可以拥有基本汽车,并且可以将其扩展到跑车和豪华车。 实现层次结构如下图所示。

But if we want to get a car at runtime that has both the features of sports car and luxury car, then the implementation gets complex and if further more we want to specify which features should be added first, it gets even more complex. Now imagine if we have ten different kind of cars, the implementation logic using inheritance and composition will be impossible to manage. To solve this kind of programming situation, we apply decorator pattern in java.

但是,如果我们要在运行时获得兼具跑车和豪华车功能的汽车,则实现会变得复杂,而且如果要进一步指定要首先添加哪些功能,它会变得更加复杂。 现在想象一下,如果我们有十种不同类型的汽车,使用继承和组合的实现逻辑将无法管理。 为了解决这种编程情况,我们在Java中应用了装饰器模式。

We need to have following types to implement decorator design pattern.

我们需要以下类型来实现装饰器设计模式。

  1. Component Interface – The interface or abstract class defining the methods that will be implemented. In our case Car will be the component interface.
    package com.journaldev.design.decorator;
    
    public interface Car {
    
    	public void assemble();
    }

    组件接口 –定义将要实现的方法的接口或抽象类 。 在我们的例子中, Car将成为组件接口。
  2. Component Implementation – The basic implementation of the component interface. We can have BasicCar class as our component implementation.
    package com.journaldev.design.decorator;
    
    public class BasicCar implements Car {
    
    	@Override
    	public void assemble() {
    		System.out.print("Basic Car.");
    	}
    
    }

    组件实现 –组件接口的基本实现。 我们可以将BasicCar类作为我们的组件实现。
  3. Decorator – Decorator class implements the component interface and it has a HAS-A relationship with the component interface. The component variable should be accessible to the child decorator classes, so we will make this variable protected.
    package com.journaldev.design.decorator;
    
    public class CarDecorator implements Car {
    
    	protected Car car;
    	
    	public CarDecorator(Car c){
    		this.car=c;
    	}
    	
    	@Override
    	public void assemble() {
    		this.car.assemble();
    	}
    
    }

    Decorator – Decorator类实现组件接口,并且与组件接口具有HAS-A关系。 子变量装饰器类应该可以访问component变量,因此我们将使此变量受保护。
  4. Concrete Decorators – Extending the base decorator functionality and modifying the component behavior accordingly. We can have concrete decorator classes as LuxuryCar and SportsCar.
    package com.journaldev.design.decorator;
    
    public class SportsCar extends CarDecorator {
    
    	public SportsCar(Car c) {
    		super(c);
    	}
    
    	@Override
    	public void assemble(){
    		super.assemble();
    		System.out.print(" Adding features of Sports Car.");
    	}
    }

    混凝土装饰器 –扩展基本装饰器功能并相应地修改组件的行为。 我们可以有具体的装饰类,例如LuxuryCarSportsCar
    package com.journaldev.design.decorator;
    
    public class SportsCar extends CarDecorator {
    
    	public SportsCar(Car c) {
    		super(c);
    	}
    
    	@Override
    	public void assemble(){
    		super.assemble();
    		System.out.print(" Adding features of Sports Car.");
    	}
    }

装饰器设计模式–类图 (Decorator Design Pattern – Class Diagram)

装饰设计模式测试程序 (Decorator Design Pattern Test Program)

package com.journaldev.design.test;

import com.journaldev.design.decorator.BasicCar;
import com.journaldev.design.decorator.Car;
import com.journaldev.design.decorator.LuxuryCar;
import com.journaldev.design.decorator.SportsCar;

public class DecoratorPatternTest {

	public static void main(String[] args) {
		Car sportsCar = new SportsCar(new BasicCar());
		sportsCar.assemble();
		System.out.println("\n*****");
		
		Car sportsLuxuryCar = new SportsCar(new LuxuryCar(new BasicCar()));
		sportsLuxuryCar.assemble();
	}

}

Notice that client program can create different kinds of Object at runtime and they can specify the order of execution too.

注意,客户端程序可以在运行时创建不同种类的对象,并且它们也可以指定执行顺序。

Output of above test program is:

以上测试程序的输出为:

Basic Car. Adding features of Sports Car.
*****
Basic Car. Adding features of Luxury Car. Adding features of Sports Car.

装饰器设计模式-重点 (Decorator Design Pattern – Important Points)

  • Decorator design pattern is helpful in providing runtime modification abilities and hence more flexible. Its easy to maintain and extend when the number of choices are more.

    装饰器设计模式有助于提供运行时修改功能,因此更加灵活。 选择数量更多时,它易于维护和扩展。
  • The disadvantage of decorator design pattern is that it uses a lot of similar kind of objects (decorators).

    装饰器设计模式的缺点是它使用许多类似的对象(装饰器)。
  • Decorator pattern is used a lot in Java IO classes, such as FileReader, BufferedReader etc.

    装饰器模式在Java IO类中使用很多,例如FileReader,BufferedReader等。

翻译自: https://www.journaldev.com/1540/decorator-design-pattern-in-java-example

java 设计模式 示例

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值