Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Look at the case above, I believe that we have already practiced their own, next we will put the decoration mode of the realization of hands on.

Step 1: Create milk tea interface class

public interface MilkTea {
	// Return the description of milk tea
	public String getDescription(a);
	// Return the price
	public double getPrice(a);
}
Copy the code

The second step: create two specific milk tea categories: chocolate milk tea, QQ milk tea

public class ChocolateMT implements MilkTea {
	private String description = "Chocolate milk tea";
	@Override
	public String getDescription(a) {
		return description;
	}
	@Override
	public double getPrice(a) {
		return 15; }}public class QQMT implements MilkTea {
	private String description = "QQ milk tea!";
	@Override
	public String getDescription(a) {
		return description;
	}
 
	@Override
	public double getPrice(a) {
		return 10; }}Copy the code

Step 3: Create a milk tea decoration category

public class Decorator implements MilkTea {
	private String description = "I'm just a decorator. I don't know what kind of milk tea it is.";
	@Override
	public String getDescription(a) {
		return description;
	}
	@Override
	public double getPrice(a) {
		return 0;		// The price depends on the type}}Copy the code

Step 4: Specific decoration: add coconut to milk tea

public class Coconut extends Decorator{
	private String description = "With coconut!";
	private MilkTea milkTea = null;
    
	public Coconut(MilkTea milkTea){
		this.milkTea = milkTea;
	}
	public String getDescription(a){
		return milkTea.getDescription()+"\n"+description;
	}
	public double getPrice(a){
		return milkTea.getPrice()+3;	//3 represents the price of coconut fruit}}Copy the code

Step 5: Specific decoration category: add pudding to milk tea

public class Pudding extends Decorator{
	private String description = 'With pudding!;
	private MilkTea milkTea = null;
    
	public Pudding(MilkTea milkTea){
		this.milkTea = milkTea;
	}
	public String getDescription(a){
		return milkTea.getDescription()+"\n"+description;
	}
	public double getPrice(a){
		return milkTea.getPrice()+5;	//5 is the price of pudding}}Copy the code

Step 6: Specific decoration: add pearls to milk tea

public class Pearl extends Decorator{
	private String description = "With pearls!;
	private MilkTea milkTea = null;
    
	public Pearl(MilkTea milkTea){
		this.milkTea = milkTea;
	}
	public String getDescription(a){
		return milkTea.getDescription()+"\n"+description;
	}
	public double getPrice(a){
		return milkTea.getPrice()+10;	//10 represents the price of pearls}}Copy the code

Step 7: Test the class

public class Test {
    public static void main(String[] args) {
        MilkTea milkTea = new ChocolateMT();	// Chose chocolate milk tea
        milkTea = new Pudding(milkTea);		// Add pudding for chocolate milk tea
        milkTea = new Coconut(milkTea); 	// Add coconut for chocolate milk tea
        System.out.println(milkTea.getDescription()+"\n Price of chocolate milk tea with pudding and coconut:+milkTea.getPrice()); }}Copy the code

The test results

Ah Q drew the class diagram of decoration mode by hand, you can refer to the class diagram for a careful study.

If you have different opinions or better idea, welcome to contact AH Q, add AH Q can join the technical exchange group to participate in the discussion!