Understanding the appearance patterns in design patterns _c language through C + + program samples

Source: Internet
Author: User

Give a small example of life, generally open to learn or after the industry will realize that a kind of depressed: you have to go to n places to deal with N procedures (now the university merged more trouble, because it is possible that n places are separated from the relatively far).

But in fact what we need is a proof of the last procedure, and we are not interested in what to do before and where to go.

In fact, in the development of software systems will often come to this situation, you may have implemented some interfaces (modules) that are distributed among several classes (e.g. A and B, C, D): A implements some interfaces, B implements some interfaces (or a represents a standalone module, B, C, D Represents some other standalone module). Then your client programmer (who uses your design developer) has very little to know that your different interfaces are actually implemented in that class, most simply want to simply combine the interfaces of your a-d class, and he doesn't want to know where these interfaces are implemented.

Here's the client programmer is above the life wants to transact the procedure the depressed person! In real life we may be able to quickly think of finding a person acting on everything to solve your problem (you just have to maintain a simple interface with him!). In software system design and development, we can solve the problem by a pattern called façade.

We solve the above problem through the appearance pattern, its typical structure diagram is:

The idea, idea and realization of the appearance pattern are very simple, but the thought is very meaningful. And the appearance design pattern is one of the most widely used and the most popular models in the actual development and design.

application Example:
If you want to eat a big meal, then I think you want, first to buy food, and then go back home to cook their own dishes, and finally after dinner to wash dishes to clean.
So you need 3 categories, shopper, Cook, dishwasher, to complete the purchase of vegetables, cooking, washing dishes and so on, obviously this is very troublesome, can you simplify the process? Of course! Go to a restaurant to eat.
The restaurant is clearly a cosmetic pattern at this time, and he encapsulates the complex process into the interior, and the user doesn't care about the details.

Implementation code:
Shopper Procurement Category

Class Shopper 
{public 
: 
 void Shopforgroceries () 
 { 
  cout<< "buy food" <<endl; 
 } 
 
}; 

Cook Chef Class

Class Cook 
{public 
: 
 void Docook () 
 { 
  cout<< "cooking" <<endl; 
 } 
 
}; 


Dishwasher Type Dishwasher

Class Dishwasher 
{public 
: 
 void Washdish () 
 { 
  cout<< "dishwasher" <<endl; 
 } 
}; 

Restaurant class

Class restaurant 
{public 
: 
 restaurant () 
 { 
 
 } 
 void Haveameal () 
 { 
  m_ Shopper.shopforgroceries (); 
  M_cook.docook (); 
  M_washer.washdish (); 
 } 
Private: 
 shopper M_shopper; 
 Cook M_cook; 
 Dishwasher m_washer; 
}; 

Client:

Restaurant RT; 
Rt.haveameal (); 

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.