Novice Java design mode-from the pot bottom to anticipate the adorner mode

Source: Internet
Author: User

Starting today, the pattern of Java design patterns, as usual or write down their understanding of the experience it.

Decorator mode, what is adorner mode? With this problem, let's go to the Hotpot restaurant and have a hot pot.

"Boss, to a full spicy pot, do not give me the decorative pattern to the ingredients!" "I made a very hard point to emphasize the last word.

In less than a while, the boss brought me a hot pot and a few plates, hot pot filled with salt water, and the plate put pepper, pepper, fennel, garlic and other spices ....

At this time everyone may need to ask, this How to eat ah ... Do I have to make my own ingredients?

This is our contradiction! Customer needs a plate has been well-equipped hot pot soup bottom, but we initially give users of the pot may only have brine, we can not give the raw materials to customers, so that customers own ingredients.


So back to our decorator mode, using the terminology we eat hot pot to explain, the original to the customer's salt pot with ingredients, so that it becomes the customer needs of the full spicy hot pot base material.


Now, let's use the code to implement the decorating process.


Pre-order, we first define an abstract class, this abstract class called the Foodstuff class, he has a method called into the pot.

/** * Food Foodstuff * @author * */public abstract class Food {//foodstuff there is an abstract method, called into the pot, please forgive me, my English is so, with pinyin. Abstract public void Ruguo ();}


Another pot.

public class Guo {Food food;//defines a method of boiling, the object being boiled is the public void Zhu (foods) {This.food = Food;food.ruguo (); System.out.print ("Boiling open to Eat");}}

Let's start with the ingredients.

The first step is to add brine to the hot pot in our line

In order to differentiate, we add a delegate behind the name is the decorator

public class Shui extends food {@Overridepublic void Ruguo () {System.out.println ("Add 2 litres of salt water in the pot");}

The second step, the addition of ingredients, the same ingredients belong to the abstract category of foodstuffs.

First in the brine to add Pepper Bai, here to note that the pepper belongs to a decorative object, must need a same type of decorative objects, if not decorated objects, the individual existence of pepper has no meaning. Same below

public class Lajiao extends food {food food; Lajiao (food food) {This.food=food;} @Overridepublic void Ruguo () {Food.ruguo (); System.out.println ("Add 3 peppers in the Pot");}}

Add some garlic.

public class Dasuan extends food {food Food;dasuan) {This.food=food;} @Overridepublic void Ruguo () {Food.ruguo (); System.out.println ("Add 2 slices of garlic to the pot");}}

Add some spice, and put some octagonal fennel.

public class Huixiang extends food {food food; Huixiang (food food) {This.food=food;} @Overridepublic void Ruguo () {Food.ruguo (); System.out.println ("Add 5 slices of fennel to the pot");}}


Let's add these for the time being.

Okay, let's test it, boiling.

public class Tset {public static void main (string[] args) {//First pot Guo Guo = new Guo ();//Prepare a pot of brine---------> Decorated Object Food shu i = new Shui ();//place Pepper in brine-------------> Adorner food lajiao = new Lajiao (Shui);//add garlic to chilli brine----------> Adorner food Dasuan = New Dasuan (Lajiao);//add fennel to garlic pepper in brine-------> Adorner food huixiang = new Huixiang (Dasuan);//uniform into the pot, boiling Guo.zhu (Huixiang);}}

Operation Result:


OK, open the food. So a pot of hot pot is cooked ....

After writing this code, we will actually find that the object-oriented thinking makes our code so flexible.

If we do not need to eat all spicy hot pot, only need not to add pepper can ....

If we want to eat a dry pot bullfrog, only need to prepare a dry pot Bullfrog adapter can ...


Writing to this, I suddenly found that the original orientation of the thought in our life so ubiquitous, a chef is a simple dish-matching process is a decorative pattern embodiment, is this perhaps also Java so popular reason?


Well, it's time to summarize ...

What exactly is the adapter mode? To put it bluntly is a sentence, to the original object to add the functions you need.

But the adapter pattern also needs to meet several principles.

1. As a decorator must be to decorate an object, as we mentioned above the ingredients, leaving the salt water in fact there is no sense!

2. adorners and decorated objects must inherit the same interface or inherit from the same abstract class. Just like our brine, ingredients need to be implemented into the hot pot this way, of course, how to put it into their own to achieve. Just like 2 liters of water, garlic needs only 3 pieces.


What is the difference between an adapter and an adorner?

Writing to this, I suddenly felt that the adorner and the adapter are not clear ....

The adapter is a separate class that is a middleware that is compatible with the old interface and implements the new interface. At the same time responsible for two aspects. Adapter indecisive.

Adaptation of the scene, such as the original pass a parameter can get data, now requires two parameters to get the data.

Some people may say that the method of overloading the original class directly is not OK?

Remember, we may be in the development of calling other people's packages or other people's methods, and the principle of development time should not be arbitrary to modify other people's class, when we develop an adapter class is the most suitable.


While the adorner, Gubai disregard after.

Enrich the original method of the original method is more able to meet the needs of customers. But to ensure that the original method is also normal use.

Adapted to the scene, the original method a prints out the customer's name. New method to print the customer's name and gender. At this time we can create a new adorner, after printing the name and then add the print Gender code.


But no matter what kind of design pattern, there is always a principle.

Internal modification closed, external expansion development

This article original Oh, need to reprint please indicate the source, thank you.


Novice Java design mode-from the pot bottom to anticipate the adorner mode

Related Article

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.