A simple way to understand the decorator pattern of design patterns

Source: Internet
Author: User
Tags extend

Gossip not much to say, today to talk about decorator mode:

First of all, I think to learn design patterns, remember that its definition is really important, or at the beginning you do not understand, but after understanding the connotation of the pattern, you will find the definition of incisive.

Decorator Mode: Dynamically attaches responsibility to objects, and to extend functionality, adorners provide a more resilient alternative than inheritance.

First, when designing the class, we have to understand the idea that the class we are designing is open to the extension and closed to the modification. When we design a class that does not meet our needs, we may design a class to inherit it, but this will cause a high degree of coupling between objects, decoupling, or the need to maintain a lot of unnecessary trouble, this time, we can consider the use of decorator mode, Decorator mode Simply put the object in the class where we want to extend the function, call his method, and return the type we need along with our defined method. This may be a bit similar to the adapter pattern, but the points they are targeting are different, and you can continue to follow my explanation of the adaptive model.

The theory of things may be difficult to understand, we still use an example to illustrate it:

I still like the example of that coffee: when we go to the café. I wonder if I will encounter such a problem. Originally you ordered a glass of ordinary coffe, and then his price is 10 yuan, but you tasted after a bit bitter, want to add brown sugar, and then add this sugar price is 2 yuan; and then after a while your friend came over, he called a common coffee, and then added some milk (assuming you can add), The price of this milk is 3 yuan, if you are asked to design a program responsible for this part of the content, what would you do?

Will anyone think that Java is not an object-oriented language? We can design the object to Ah, first create an abstract class coffee, and then in the list may be added seasoning of possible food, and then payment, the call method is not OK. It is true that this is possible, but the above-mentioned coupling is high, and by this time we have come to our decorators:

First draw the diagram:


All we have to do is point to coffee, add sugar when you're 1, and milk when you get coffee 2.

(1) Creating an Coffee interface

Package com.zqu.yqy.scdn.test.test006;

Public abstract class Coffee {
	String coffeeinformation = "Normal coffee";
	Public String getcoffeeinformation () {
		return coffeeinformation;
	}
	public abstract double Cost ();

}
Implement class Coffee1,coffee2:

Package com.zqu.yqy.scdn.test.test006;

public class Coffee1 extends coffee {public
	coffee1 () {
		coffeeinformation = "coffee1";
	}

	@Override public
	double Cost () {
		return 10.0;
	}

}
Package com.zqu.yqy.scdn.test.test006;

public class Coffee2 extends coffee {public
	coffee2 () {
		coffeeinformation = "Coffee2";
	}

	@Override public
	double Cost () {
		
		return 12.0;
	}

}
(2) Seasoning type

Package com.zqu.yqy.scdn.test.test006;

Public abstract class Addthings extends coffee {public
	abstract String getcoffeeinformation ();


Implementation class:

Package com.zqu.yqy.scdn.test.test006;

public class milk extends addthings{
	coffee coff;
	Public milk (coffee c) {
		this.coff = c;
	}

	@Override public
	String getcoffeeinformation () {
		string addthings = coff.getcoffeeinformation () + "add" + "milk";
		return addthings;
	}

	@Override public
	double Cost () {
	
		return 3.0+coff.cost ();
	}

}
Package com.zqu.yqy.scdn.test.test006;

public class Sugar extends Addthings {
	coffee coff;
	Public sugar (coffee c) {
		this.coff = c;
	}
	@Override public
	String getcoffeeinformation () {
		string addthings = coff.getcoffeeinformation () + "add" + " Sugar ";
		return addthings;
	}

	@Override public
	double Cost () {
		
		return 2.0+coff.cost ();
	}
	

}

(3) Implementation class:

Package com.zqu.yqy.scdn.test.test006;

public class Cost {public
	static void Main (string[] args) {
		coffee C1 = new Coffee1 ();
		Coffee C2 = new Coffee2 ();
		Coffee A1 = new sugar (c1);
		Coffee A2 = new Milk (c1);
		Coffee a3 = new Milk (C2);
		System.out.println (a1.getcoffeeinformation () + "" +a1.cost ());
		System.out.println (a2.getcoffeeinformation () + "" +a2.cost ());
		System.out.println (a3.getcoffeeinformation () + "" +a3.cost ());
		
	}
	

}
Result screenshot:


You will find it very magical to realize, we want the function, perhaps some people will be curious, why our seasoning class also inherit coffee of it. That's a good question, but I'm sure you'll do it again with the code I commented out, and you'll magically find out that our coffee1 or coffee2 can add milk. This you will understand in practice, such a purpose, is not very good ...

All right. About the decorator mode is introduced here, next I will introduce, adapter mode, you will find that there will be a lot of similarities.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.