Big talk design mode - decoration mode

1, Clothing matching system

Now you are required to write a system that can match people with different clothes, such as Avatar system similar to QQ and online games. How do you develop it?

2, Dishes dress up first edition

//Person class
public class Person {

private String name;

public Person(String name) {
	this.name = name;
}

public void show() {
	System.out.println("Dressed" + name);
}

}
//Clothing abstract class
public abstract class Finery {

public abstract void show();

}
//Various clothing subcategories
public class Tshirt extends Finery {

@Override
public void show() {
	System.out.println("T Shirt");
}

}

public class BigTrouser extends Finery {

@Override
public void show() {
	System.out.println("loose ");
}

}

public class Sneakers extends Finery {

@Override
public void show() {
	System.out.println("gym shoes");
}

}
//Client code
public class RunMain {

public static void main(String[] args) {

	Person xc = new Person("side dish");
	// The first dress
	Finery t = new Tshirt();
	Finery b = new BigTrouser();
	Finery s = new Sneakers();
	
	t.show();
	b.show();
	s.show();
	xc.show();

}

}
3, Decoration mode

//Define an object interface to dynamically add responsibilities to objects
public abstract class Component {

public abstract void Operation();

}
//Define a specific object and add responsibilities to the object
public class ConcreteComponent extends Component {

@Override
public void Operation() {
	System.out.println("Operation of specific objects");
}

}
//Decorate abstract classes, inherit components, and extend the functions of Component classes from external classes, but for components, it is unnecessary to know the existence of Decorator
public class Decorator extends Component {

protected Component component;

// Set Component
public void setComponent(Component component) {
	this.component = component;
}

// Overriding Operation() actually executes the Operation() of the Component
@Override
public void Operation() {
	if(component!=null){
		component.Operation();
	}
}

}
//ConcreteDecorator is a specific decorative object, which can add responsibilities to components
public class ConcreteDecoratorA extends Decorator {

// This class has unique functions to distinguish it from ConcreteDecoratorB
private String addedState;

// First run the Operation() of the original Component, and then execute the functions of this class, such as addedState, which is equivalent to decorating the original Component
@Override
public void Operation() {
	super.Operation();
	this.addedState = "New State";
	System.out.println("Specific decoration object A Operation of");
}

}
public class ConcreteDecoratorB extends Decorator {

@Override
public void Operation() {
	super.Operation();
	addedBehavior();
	System.out.println("Specific decoration object B Operation of");
}

//This class has a unique method to distinguish it from ConcreteDecoratorA
private void addedBehavior() {
	
}

}
public class RunMain {

public static void main(String[] args) {

	ConcreteComponent cc = new ConcreteComponent();
	ConcreteDecoratorA cda = new ConcreteDecoratorA();
	ConcreteDecoratorB cdb = new ConcreteDecoratorB();
	
	// First instantiate the object cc through the ConcreteComponent, then instantiate the object cda through the concretedecorator a to wrap the cc, then instantiate the object cdb through the concretedecorator B to wrap the cda, and finally execute the cdb Operation()
	cda.setComponent(cc);
	cdb.setComponent(cda);
	cdb.Operation();
	
}

}
4, Side dishes dressing up second edition

Through the decoration pattern, dynamically add some additional responsibilities to an object. In terms of adding functions, the decoration pattern is more flexible than generating subclasses.

//Concrete Person class ConcreteComponent
public class ConcretePerson {

private String name;

public ConcretePerson() {}

public ConcretePerson(String name) {
	this.name = name;
}

public void show() {
	System.out.println("Dressed" + name);
}

}
//Clothing Decorator
public class Finery extends ConcretePerson {

protected ConcretePerson person;

// Set Component
public void setPerson(ConcretePerson person) {
	this.person = person;
}

// Overriding Operation() actually executes the Operation() of the Component
@Override
public void show() {
	if(person!=null){
		System.out.println("This is the operation of specific decoration\n");
		person.show();
	}
}

}
//Concrete decorator
public class Tshirt extends Finery {

@Override
public void show() {
	System.out.println("T Shirt");
	super.show();
}

}

public class BigTrouser extends Finery {

@Override
public void show() {
	System.out.println("loose ");
	super.show();
}

}

public class Sneakers extends Finery {

@Override
public void show() {
	System.out.println("gym shoes");
	super.show();
}

}
//Client code
public class RunMain {

public static void main(String[] args) {

	ConcretePerson xc = new ConcretePerson("side dish");
	
	Tshirt t = new Tshirt();
	BigTrouser b = new BigTrouser();
	Sneakers s = new Sneakers();
	
	t.setPerson(xc);
	b.setPerson(t);
	s.setPerson(b);
	
	s.show();
	
}

}
5, Summary

Decoration mode is a way to dynamically add more functions to existing functions.

When the system needs new functions, add new code to the old classes. These newly added codes usually decorate the core responsibilities or main behaviors of the original classes. These newly added things are only to meet the needs of some special behaviors that will be executed only under certain circumstances.

Decoration pattern provides a very good solution. It puts each function to be decorated in a separate class and lets this class wrap the object to be decorated. Therefore, when special behavior needs to be performed, the customer code can selectively and sequentially use the decoration function to wrap the object according to the needs at run time.

Decoration patterns effectively distinguish the core responsibilities of classes from decoration functions. Moreover, it can remove the repeated decoration logic in related classes.

Tags: C# Back-end

Posted on Thu, 25 Nov 2021 23:04:47 -0500 by tboneuls