Author:PeterXuSource: Blog. CSDN Blog: http://blog.csdn.net/peterreg/
Copyright Notice: Original works can be reproduced. During reprinting, you must mark the original Publishing, author information, and this statement in the form of hyperlinks. Otherwise, legal liability will be held. Address: http://blog.csdn.net/peterreg/archive/2008/04/08/2259404.aspx
The transfer between classes plays an important role in our daily programming. Various implementation methods have a great impact on program scalability and performance.
For example, if the object of Class A is an instance of Class B, object B needs to access an attribute A1 of object.
Class
{
Private int m_iA1;
Public int A1
{
Get {return this. m_iA1 ;}
Set {this. m_iA1 = value ;}
}
}
1. Pass object A as A parameter to Class B, that is, pass object A to Class B in the form of an attribute or constructor, and then call A1 through reference of object A in object B.
Class B
{
Private A m_oA;
Public B (A)
{
This. m_oA =;
}
}
Class B
{
Private A m_oA;
Public B ()
{
}
Public
{
Set {this. m_oA = value ;}
}
}
Disadvantages: Class A and Class B have A high Coupling Degree. Once other classes think of the function of Class A, Class A needs to be extended.
Improvement:A unified abstraction is designed for Class A. The interface or abstract class mode can reduce coupling in A certain program.
For more information about interfaces and abstract classes, see my previous article"
2. Use the format of intermediate files, such as XML, text files, and databases.
Disadvantages:Inefficient, unified file format, and packaging and Package
Advantages:It can transmit a large amount of information.
3. Shared memory space
Disadvantages:There is a certain degree of complexity, there are some reference code on the Internet
4. Attributes of Class A adopt Static mode Static
Disadvantages:This method is invalid when some attributes cannot be set as static attributes.
5. Proxy
Disadvantages:Efficiency is slightly lower, with little impact
Advantages:Good program encapsulation and scalability