decorator

package
v0.0.0-...-44f814d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2023 License: GPL-3.0 Imports: 1 Imported by: 0

README

In this Decorator pattern example, we have a Component interface that defines the basic operations. ConcreteComponent is a concrete implementation of the Component interface. The Decorator interface extends the Component interface and represents the base interface for all decorators. ConcreteDecoratorA and ConcreteDecoratorB are concrete implementations of the Decorator interface. The decorators wrap an instance of the component and add additional behavior to its operation. Each decorator invokes the operation of the wrapped component and adds its own behavior before or after the invocation. In the Example function, we create a ConcreteComponent instance. We then create decorators (ConcreteDecoratorA and ConcreteDecoratorB) and wrap the component instance with them. Finally, we call the Operation method on the final wrapped component, which triggers the entire chain of decorators and component. The output of the program will be:

ConcreteDecoratorB(ConcreteDecoratorA(ConcreteComponent))

This demonstrates how the Decorator pattern allows us to dynamically add or modify behavior of an object by wrapping it with decorators. Each decorator adds its own functionality while still maintaining the original interface and behavior of the component.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Example

func Example()

Types

type Component

type Component interface {
	Operation() string
}

Component represents the base component interface.

type ConcreteComponent

type ConcreteComponent struct{}

ConcreteComponent represents a concrete implementation of the component interface.

func (*ConcreteComponent) Operation

func (c *ConcreteComponent) Operation() string

type ConcreteDecoratorA

type ConcreteDecoratorA struct {
	// contains filtered or unexported fields
}

ConcreteDecoratorA represents a concrete decorator implementation.

func (*ConcreteDecoratorA) Operation

func (d *ConcreteDecoratorA) Operation() string

type ConcreteDecoratorB

type ConcreteDecoratorB struct {
	// contains filtered or unexported fields
}

ConcreteDecoratorB represents another concrete decorator implementation.

func (*ConcreteDecoratorB) Operation

func (d *ConcreteDecoratorB) Operation() string

type Decorator

type Decorator interface {
	Component
}

Decorator represents the base decorator interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL