Description of the Cheshire Cat mentioned in design patterns and design patterns meditation.

Source: Internet
Author: User

Recently, I was reading gof's design patterns. Before that, I saw John vlissides's design patterns in meditation. In the "Meditation" p42 footer note, the author mentioned

"In C ++, such a definition is difficult to implement through any mandatory method [schmidt96a]. For example, if you use a simple # define statement to define private as public, we can change all private members into common members. One way to avoid such tampering is to not define member variables in the header file at all. On the contrary, we define the implementation details of member variables and other confidential information in another separate, non-public header file. A closely related model is bridge ......"

.

The lower part of the p102 page of design patterns

"Carolan [car89] describes this separation mechanism with Cheshire cat. In C ++, the class interface of the implementor class can be defined in a private header file, which is not provided to the customer. In this way, you have completely hidden the implementation part of a class from the customer ."

What is Cheshire Cat )? (From: http://baike.soso.com/h7921075.htm? SP = l7921076)

Cheshire cat is the fairy tale Alice's adventure in Wonderland created by Lewis Carroll (1832-1898), a British writer) the fictional role in the image is a smiling cat with the ability to appear or disappear out of thin air. Even after it disappears, its smile is still in the air. Carol's role may be inspired by the English saying "smile like a Cheshire cat" (grin like a Cheshire cat), which has different origins, some say that Chai-gun is rich in a kind of cheese in the form of smiling cat, some say that there is a local nobility, his coat sleeves painted a lion, the lion painted very bad, it looks like a smiling cat, and some people say it's the nickname of A caterling, who has an ugly smile from Chai-gun. In Alice's Wonderland, the cat in Chai-gun appeared in Chapter 6, and sat in front of the house's fireplace when she appeared. Alice saw it smiling all the time, then I asked the Duke why it was laughing. The Duke told her that it was a chai-gun cat. "Please wocould you tell me," said Alice, a little timidy, for she was not quite sure whether it was good manners for her to speak first, "why your cat grins like that? "" It's a Cheshire cat, 'said the Duchess, "and that's why. "after leaving the house of the duke, cat Chai-gun gave Alice the path to the house of the rabbit and the hatter in March, and later appeared in front of Alice on the Queen's golf ball, even the Queen who throws his head can't do anything about it. Perhaps because of its mysterious nature, Chai-gun cat, who is always smiling and not involved in anything, is always one of the most popular roles in Alice's wonderland. Later generations often see this cat in the works like Alice roaming wonderland, for example, a cartoon "Prince of black tea" and a collection of "host department of yinglan High School"; a cat bus with a grin in the animated movie "Dragon cat, the smiling face cat in Pandora's heart is also influenced by the image of the CAT in Chai gun.

So what is Cheshire Cat talking about? Go to http://www.acm.org/sigsto check the result. According to the gof references, Cheshire Cat is from John Carolan's article "constructing bullet-proof classes" in 1989, So Google will simply search this article ~

Did not find the original, but found an article referencing the original: http://wiki.hsr.ch/Prog3/files/overload72-FINAL_DesigningHeaderFiles.pdf

Cheshire Cat● A private “representation” class is written that embodies thesame functionality and interface as the naïve class – however,unlike the naïve version, this is defined and implementedentirely within the implementation file. The public interface ofthe class published in the header is unchanged, but the privateimplementation details are reduced to a single member variablethat points to an instance of the “representation” class – eachof its member functions forwards to the corresponding functionof the “representation” class.● The term “Cheshire Cat” is an old one, coined by John Carollanover a decade ago [2]. Sadly it seems to have disappeared fromuse in contemporary C++ literature. It appears described as aspecial case of the “Bridge” pattern in Design Patterns [3], butthe name “Cheshire Cat” is not mentioned. Herb Sutter [4]discusses it under the name “Pimpl idiom”, but considers it onlyfrom the perspective of its use in reducing physicaldependencies. It has also been called “Compilation Firewall”.● Cheshire Cat requires “boilerplate” code in the form offorwarding functions that are tedious to write and (if thecompiler fails to optimise them away) can introduce a slightperformance hit. It also requires care with the copy semantics(although it is possible to factor this out into a smart pointer[5]). As the relationship between the public and implementationclasses is not explicit it can cause maintenance issues.

The translation is as follows:

Chai Jun Cat 1. A private "display" class is written as a class that contains the same functions and interfaces as a simple class, but unlike a class of a non-simple version, it (private "display" Class) it is defined and implemented in an implementation file. The public interface of the class released in the header file remains unchanged. However, the details of the private implementation are reduced to a single member variable, which points to an instance of the "display" class, all member functions (called) of it (the class released in the header file) are transferred ("delegate" is more appropriate ~) Shows the corresponding functions of the class. 2. Cheshire Cat is an old saying created 10 years ago by John carollan. Unfortunately, this statement seems to be no longer used in modern c ++ literature. It is described as a special form of the Bridge pattern in design patterns, but Cheshire cat is not mentioned. Herb Suter discussed it with the name "pimpl idiom", but only focused on its role in reducing physical dependencies. It is also called "firewall compilation ". 3. Cheshire cat (Cheshire Cat) needs to forward the Code ("hardcoded") in the form of a function "), these code is usually lengthy and (if the compiler does not optimize them) will lead to performance degradation. It also needs to focus on replication semantics (although this can be extracted into smart pointers ). Because the relationship between public and implementation classes is not explicit, it (Cheshire Cat) may cause maintenance problems.

The sample code of Cheshire Cat is as follows:

// cheshire_cat.hpp Cheshire Cat -// implementation hiding example#ifndef INCLUDED_CHESHIRE_CAT_HPP_ARG_20060303#define INCLUDED_CHESHIRE_CAT_HPP_ARG_20060303#include <string>#include <utility>namespace cheshire_cat{class telephone_list{public:telephone_list(const std::string& name);~telephone_list();std::string get_name() const;std::pair<bool, std::string>get_number(const std::string& person)const;telephone_list&add_entry(const std::string& name, const std::string& number);private:class telephone_list_implementation;telephone_list_implementation* rep;telephone_list(const telephone_list& rhs);telephone_list& operator=(const telephone_list& rhs);};}#endif

The simple sample code is as follows:

// naive.hpp - implementation hiding// example.#ifndef INCLUDED_NAIVE_HPP_ARG_20060303#define INCLUDED_NAIVE_HPP_ARG_20060303#include <string>#include <utility>#include <map>#include <algorithm>#include <ctype.h>namespace naive {/** Telephone list. Example of implementing a *telephone list using a naive implementation.*/class telephone_list{public:/** Create a telephone list.* @param    name    The name of the list.*/telephone_list(const std::string& name);/** Get the list's name.* @return   the list's name.*/std::string get_name() const;/** Get a person's phone number.* @param    person  The person's name* (must be an exact match)* @return   pair of success flag and (if* success) number.*/std::pair<bool, std::string>get_number(const std::string& person)const;/** Add an entry. If an entry already* exists for this person it is overwritten.**   @param  name    The person's name*   @param  number  The person's number*/telephone_list&add_entry(const std::string& name, const std::string& number);private:typedef std::map<std::string,std::string> dictionary_t;std::string  name;dictionary_t dictionary;telephone_list(const telephone_list& rhs);telephone_list& operator=(const telephone_list& rhs);};}#endif

You can find that if you use

#define private public

Then all private permission variables in the simple code will be visible! It cannot be hidden at all!

In the Cheshire Cat code, because the member variables are not defined in the file (at least not so many sensitive member variables are defined), the Cheshire Cat Code does not expose implementation details, but at most exposes

class telephone_list_implementation;
telephone_list_implementation* rep;
 
 

If the implementation of class telephone_list_implementation is in a lib or DLL file, you cannot

#define private public

Method To expose the details of class telephone_list_implementation! Because the user does not have the source code of class telephone_list_implementation ......

The user obtains the most telephone_list_implementation * rep, but the details of telephone_list_implementation * rep cannot be exposed.

 
 

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.