Structure of Design Mode--Decoration Mode

Posted by itisprasad on Sun, 19 May 2019 18:36:44 +0200

Decoration mode:
Responsibilities:
Dynamic has to add new functionality to an object
Decoration mode is a technology that replaces inheritance without adding subclasses through inheritance
Expands new functionality of objects.It is more flexible to use the object's association relationship instead of the inheritance relationship.
Also avoid the rapid expansion of type systems
Implementation details:
Component abstract component role
Real objects and decorations have the same interface so that client objects can be real
Interact with decorative objects in the same way
ConcreteComponent specific component roles (real objects)
FIleInputStream, FileOutputStream in io stream
Decorator Decorator Decorative Role
Holds a reference to an abstract component.Decoration objects accept requests from all clients and take these
The request is forwarded to the real object.This allows new functionality to be added before and after the call to the real object
ConcreteDecorator specific decorative roles:
Adding new responsibilities to component objects
        
Scenarios used in development:
Design of Input and Output Streams in IO
* GUI Component Functions in Swing Package
Default implementation class for Decorator design pattern of request object provided in Servlet API
* HttpServletRequestWrapper,HttpServletRequestWrapper class, enhanced request
Functions of the object.
Processing of request, response,session objects in Struts2

Summary:
Decorator is also called Wrapper
Decoration mode reduces system coupling and dynamically increases or removes object responsibilities
Make the specific building and decoration classes that need to be decorated independently to increase
New specific building and decoration classes
Strengths:
Expanding object functionality is more flexible than inheritance and will not result in a sharp increase in the number of classes.
You can decorate an object multiple times, create combinations of different behaviors, and get functionality
More powerful objects
Specific building and decoration classes can change independently, and users can add new specifications as needed
Component subclasses and specific decorative subclasses
Disadvantages:
* Many small objects are generated and a large number of small objects occupy memory, affecting performance to some extent
Decoration mode is prone to errors, debugging and troubleshooting are more troublesome.
    
Differences between decorative and bridging modes:
Both modes solve the problem of having too many subclasses of objects, but their incentives are different
Bridging patterns are when the existing mechanisms of the object itself change along multiple dimensions, or are partially unstable
Decoration mode is designed to add new features
        

package com.luruixiao.pattern.decorator;

/**
 * Decoration Mode
 * Abstract construction
 * @author lenovo
 *
 */
public interface ICar {
	void move();
}

//ConcreteComponent Specific Component Role (Real Object)
 class Car implements ICar{
	@Override
	public void move() {
		System.out.println("Running on land");
	}
}
//Decorator Decorative Role
class SuperCar implements ICar{
	protected ICar car;
	public SuperCar() {
		super();
	}
	public SuperCar(ICar car) {
		super();
		this.car = car;
	}
	@Override
	public void move() {
		this.car.move();
	}
}
//ConcreteDecorator specific decorative role:
class FlyCar extends SuperCar{
	public FlyCar() {
		super();
	}
	public FlyCar(ICar car) {
		super(car);
	}
	public void flyMove() {
		System.out.println("Fly in the sky");
	}
	@Override
	public void move() {
		super.move();
		flyMove();
	}
}

class WaterCar extends SuperCar{

	public WaterCar() {
		super();
	}
	public WaterCar(ICar car) {
		super(car);
	}
	public void waterMove() {
		System.out.println("Swimming in water");
	}
	@Override
	public void move() {
		super.move();
		waterMove();
	}
}

class AICar extends SuperCar{

	public AICar() {
		super();
	}
	public AICar(ICar car) {
		super(car);
	}
	public void autoMove() {
		System.out.println("Auto Drive");
	}
	@Override
	public void move() {
		super.move();
		autoMove();
	}
}


package com.luruixiao.pattern.decorator;

public class Client {
	public static void main(String[] args) {
		Car car = new Car();
		car.move();
		System.out.println("Add a function for water running");
		WaterCar waterCar = new WaterCar(car);
		waterCar.move();
		System.out.println("Add a Sky Fly feature");
		FlyCar flyCar = new FlyCar(car);
		flyCar.move();
		System.out.println("Add three functions, water running, sky flying, auto-driving");
		AICar ai = new AICar(new WaterCar(new FlyCar(new Car())));
		ai.move();
	}
}	

 

Topics: Session