author :peterxu source : Blog.csdn blog:http://blog.csdn.net/peterreg/
Copyright Notice : Original works, allow reprint, reprint, please be sure to hyperlink form to indicate the original publication of the article, the author's information and this statement. Otherwise, legal liability will be held. This article address: http://blog.csdn.net/peterreg/archive/2008/04/08/2259404.aspx
The transfer value between classes plays an important role in our daily programming. Various implementations have a great impact on the scalability and performance of the program.
For example: Class A's object instance is class B, an attribute A1 to access object A in object B.
class A
{
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 a to B in the form of a property or constructor, and then invoke A1 by reference to object A in B.
Class B
{
Private A M_oa;
Public B (a)
{
This.m_oa = A;
}
}
Class B
{
Private A M_oa;
Public B ()
{
}
Public a A
{
set {This.m_oa = value;}
}
}
Disadvantage: Class A and Class B have a high degree of coupling, and class A needs to be extended more if other classes recall the function of Class A.
Improvement:Design a unified abstract for Class A, using the interface or abstract class mode, in a certain program can reduce a certain coupling.
For the knowledge of interfaces and abstract classes, refer to my previous article, "Interfaces and abstract classes" (Interface and Abstracts Class)
2. Use intermediate file format, such as XML, text file and database, etc.
Disadvantages: Inefficient, file format needs to be unified, files need to be packaged and reconciled package
Advantages: can transmit a lot of information
3. Shared Memory space
Disadvantages: There is a certain degree of complexity, the Internet has some reference code
4. The properties of Class A are static mode
Disadvantage: This method fails when some properties cannot be set to a static property
5. The agent
disadvantage: The efficiency is slightly lower, the influence is not big
Advantages: Good encapsulation and scalability of the program
trackback:http://tb.blog.csdn.net/trackback.aspx?postid=2259404