Design mode [10] Decorative mode

define

Add some extra responsibilities to an object dynamically. It is more flexible than subclassing in terms of extending functionality.

What is the main solution?

In general, we often use inheritance to extend a class, because inheritance introduces static characteristics to the class, and subclasses can become bloated as more and more extension capabilities are added.

structure

  1. Abstract Component Role: Defines an abstract interface to specify objects that are ready to receive additional responsibilities.
  2. Concrete Component roles: Implement abstract artifacts and add responsibilities to them by decorating roles.
  3. Decorator role: Inherits an abstract artifact and contains instances of a concrete artifact that can be subclassed to extend its functionality.
  4. The ConcreteDecorator role: Implements methods associated with abstract decorators and adds additional responsibilities to concrete component objects.

implementation

package decorator;
public class DecoratorPattern
{
    public static void main(String[] args)
    {
        Component p=new ConcreteComponent();
        p.operation();
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        Component d=newConcreteDecorator(p); d.operation(); }}// Abstract the component role
interface  Component
{
    public void operation(a);
}
// Specify the role of the component
class ConcreteComponent implements Component
{
    public ConcreteComponent(a)
    {
        System.out.println("Create concrete artifact roles");       
    }   
    public void operation(a)
    {
        System.out.println("Call the method operation() of the concrete component role"); }}// Abstract decorates the role
class Decorator implements Component
{
    private Component component;   
    public Decorator(Component component)
    {
        this.component=component;
    }   
    public void operation(a)
    { component.operation(); }}// Decorate the character
class ConcreteDecorator extends Decorator
{
    public ConcreteDecorator(Component component)
    {
        super(component);
    }   
    public void operation(a)
    {
        super.operation();
        addedFunction();
    }
    public void addedFunction(a)
    {
        System.out.println("Add additional functionality for specific component roles addedFunction()"); }} Program running results are as follows: Create the method of the component characters to invoke specific components operation () -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- and call the method of the component role operation () for a specific component characters add extra functionality addedFunction ()Copy the code

Apps on Android

The Activity inherits from ContextThemeWrapper, ContextThemeWrapper inherits from ContextWrapper, and ContextWrapper inherits from Context. ContextWrapper is the decorator we’re looking for.

conclusion

Advantages: Decorator and decorator classes can develop independently without coupling to each other. The decorator pattern is an inherited replacement pattern, and the decorator pattern can dynamically extend the functionality of an implementation class.

** Disadvantages: ** multi-layer decoration is more complex.

Usage Scenarios:

Extend the functionality of a class.

2, dynamic increase function, dynamic cancel.

** Note: ** can replace inheritance.

The resources

JAVA Design Patterns summary of 23 design patterns

Decorator mode

Detailed explanation of decoration mode (decoration design mode)

Thank you for reading this article. I hope you can use it on our wechat official account (see the QR code below),The Denver nuggets,Jane’s book,CSDNCommunicate together.

CoderWonder_GZH.jpg