Handle class and interface class two ways to minimize compilation dependencies between files

Source: Internet
Author: User
Handle class and interface class two ways to minimize compilation dependencies between files
Clause 31 minimizes compilation dependencies between files/Handle class in short, separating the interface and implementation of a class.
The interface stores a pointer, points to the implementation class, and then the client invokes the interface.

This way, when the implementation changes, the customer does not have to recompile.
Handle Class (Person.h PersonImpl.h PersonImpl.cpp) The customer of person is separated from the implementation breakdown of person, the modification of person implementation does not need the person client to recompile. The interface is decoupled from the implementation. One is responsible for providing the interface and the other is responsible for implementing the interface. The main class (person) contains a pointer member Shared_ptr<personimpl>pimpl. (Pimpl pointer to implementation).
The person customer is completely separated from the person implementation. The key to this separation is to replace "defined dependencies" with "declared dependencies."
In reality, let the header file be as satisfying as possible, and if not, make it dependent on the declaration (not the definition) in the other file.
* * * Interface class in short, the class is abstracted as an interface, and then the client invokes the interface so that the implementation of the class will not be affected if it changes.
The implementation of a class is accomplished by inheriting the abstract base class. The client invokes the constructor interface as static because it is defined in the abstract base class, and the abstract base class cannot be instantiated and can only be partially instantiated when the derived class is instantiated, so it is defined as static.


A static function belongs to the entire class, and can be called by the class name:: Function name, and the concrete constructor is defined in the derived class. Another way to make handle class is to make a person known as an abstract base class called the Interface Class (Person2.h Person2.cpp RealPerson2.h) The person class defined as an abstract base class and an interface.
The customer of this class must compose the application with a pointer or reference to person. Interface class customers typically call the factory (factory) function or the virtual constructor. They return pointers (or smart pointers) to dynamically allocated objects that support interface class interfaces.
Such functions are often declared static within the interface class.
The representational class (concrete classes) that supports the interface class interface must be defined, and the real constructor must be invoked. It provides the inherited virtual function's implementation. */

The full instance code implementation for handle class is given below:
Person.h
#include <string>
#include <memory>
using namespace std;

Class Personimpl; 	the person implementation class's predecessor Declaration

class Person
{public
: Person
	(const string& name);
	String getname () const;
	
Private:
	
	tr1::shared_ptr<personimpl> Pimpl;	Pointer, point to the implementation, or personimpl* Pimpl
	string name;

Person.cpp
#include "stdafx.h"
#include "Person.h"
#include "PersonImpl.h" person

::P erson (const string& Name: Pimpl (new Personimpl (name))
{
	this->name = name;
}

String Person::getname () const
{return
	pimpl->getname ();
}

PersonImpl.h
#include <string>
#include <iostream>
using namespace std;
Class Personimpl
{public
:
	personimpl (const string& name)
	{
		this->name = name;
		cout << "Impl" << Endl;
	}
	String getname () const;
Private:
	string name;

PersonImpl.cpp
#include "stdafx.h"
#include "PersonImpl.h"
string personimpl::getname () const
{return
	this- >name;
}

Main.cpp
#include "stdafx.h"
#include "Person.h"
#include <memory>
#include <iostream>
using namespace std;
int _tmain (int argc, _tchar* argv[])
{
	//Handle Class person
	p ("Yerasel");
	cout << p.getname () << Endl;


	return 0;
}

The above method is the Pimpl method, it is proposed by Microsoft's Herb Sutter, this method is to minimize the coupling between the interface and the implementation, so as to avoid the impact of interface changes on the program recompilation. In short, if your large program contains relationships with complex header files that can be daunting for the huge compile-time cost of a small change to a header file, you need to use the PIMPL approach to improve the situation.
Here's how to interface class: Person2.h

#ifndef per2_h
#define PER2_H
#include <string>
#include <memory>
using namespace std;

Class Person2
{public
:

	Person2 (const string& name)
	{
		this->thename = name;
	}
	Virtual ~person2 () {};
	
	Static tr1::shared_ptr<person2> Create (const string& name);
	Virtual string getname () const = 0;
Private:
	string thename;
};
#endif

Person2.cpp
#include "stdafx.h"

#include "Person2.h"
#include "RealPerson2.h"
#include <memory>


TR1: :shared_ptr<person2> person2::create (const string& name)
{return
	Tr1::shared_ptr<person2 > (new RealPerson2 (name));
}

RealPerson2.h
#ifndef real2_h
#define REAL2_H
#include "Person2.h"
#include <iostream>
using namespace std;

Class Realperson2:public Person2
{public
:
	RealPerson2 (const string& name)
		:P erson2 (name), Thename (name)
	{
	}
	virtual ~realperson2 () {}
	string getname () const
	{return
		this-> thename;
	}
Private:
	string thename;
};

#endif

Main.cpp
#include "Person.h"  
#include <memory>  
#include <iostream>  
using namespace std;  
int _tmain (int argc, _tchar* argv[])  
{  
//Interface Class  
    tr1::shared_ptr<person2>p2 = (Person2:: Create ("Jandosim"));  
    cout << p2->getname () << Endl;  
    return 0;  
}


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.