All design patterns of this series are reproduced, original address: http://www.cnblogs.com/zhenyulu/articles/36058.html
Textbook: C # design model, e-Industry Press, ISBN 7-5053-8979-3. 33 RMB with a CD.
Course content: Design Mode
Source: "design patterns: Elements of reusable software" created by Alexander's architectural model and Gamma et al. (1995 ". This book is often referred to as "Gang of Four" or "gof", pioneering creation of "Design Patterns".
Some people also say that "36" is "mode ".
I. C # object-oriented Program Design Review
Click http://files.cnblogs.com/zhenyulu/csharp.rarto download the package:
Fields and attributes. CS
Attribute and method scope of action. CS
Add 1 to one hundred. CS
Sort by API (2). CS
Sort by API (1). CS
Resolution quantity. CS
Sort by bubble. CS
Table 9. CS
Static and non-static. CS
Constructor. CS
Method overload. CS
Polymorphism. CS
Recursive factorial. CS
Print triangle. CS
Value Transfer call and Reference call. CS
Ii. Examples of design patterns
In the design mode, there is a mode called builder mode. Its principle is as follows:
We can understand Builder as a rice cooker, and put the builder in rice and water. After builder build, we can take out the fragrant rice.
C # has a class called stringbuilder. After Entering necessary information, you can retrieve the corresponding string. The usage is as follows:
Using System;
Using System. text;
Class Exam
{
Public Static Void Main ()
{
Stringbuilder sb = New Stringbuilder ();
SB. append ( ' A ' , 2 );
SB. append ( ' B ' , 3 );
SB. append ( ' C ' , 4 );
Console. writeline (sb. tostring ()); // Print aabbbcccc
SB. Remove ( 0 , SB. Length ); // Clear all information in sb
}
}
The execution result is aabbbcccc.
Use stringbuilder to rewrite the following printed triangle program and write a new program.
Using System;
Public Class Exam
{
Public Static Void Main ()
{
Console. Write ( " Enter the number of rows: " );
Int Lines = Int . Parse (console. Readline ());
Console. writeline ( "" );
For ( Int I = 1 ; I <= Lines; I ++ )
{
For ( Int K = 1 ; K <= Lines - I; k ++ )
Console. Write ( " " );
For ( Int J = 1 ; J <= I * 2- 1 ; J ++ )
Console. Write ( " * " );
Console. writeline ( "" );
}
}
}
A:
Using System;
Using System. text;
Class Exam
{
Public Static Void Main ()
{
Console. Write ( " Enter the number of rows: " );
Int Lines = Int . Parse (console. Readline ());
Console. writeline ( "" );
Stringbuilder sb = New Stringbuilder ();
For ( Int I = 1 ; I <= Lines; I ++ )
{
SB. append (' ', Lines-I );
SB. append ('*', I*2-1);
Console. writeline (sb. tostring ());
SB. Remove (0, SB. Length );
}
}
} 3. Do you have chicken or eggs first?
Is there a chicken first or an egg first? See the followingCode:
Using System;
Class Client
{
Public Static Void Main ()
{
Base B= NewBase ();
Derived d= NewDerived ();
B. D=D;
Console. writeline (B. D. M );
}
}
Class Base
{
Public IntN= 9;
PublicDerived D;
}
Class Derived: Base
{
Public IntM= 10;
}
Derived inherits from the base. It can be said that there is no derived without the base, but a member in the base is of the derived type. Is there a chicken first or an egg first? This program can be compiled and executed normally and print the result 10.
4. are large bottles or small bottles?
Another example:
Using System;
Class Client
{
Public Static Void Main ()
{
A= NewA ();
B= NewB ();
A. B=B;
B.=A;
}
}
Class A
{
PublicB;
}
Class B
{
PublicA;
}
The above Code seems to describe the relationship between "A contains B and B contains a". Is it a large bottle or a small bottle?
V.. Net nature
For the program "chicken or eggs first", after the system runs, the memory structure is as follows:
It can be seen from the figure that there is no problem of chicken and eggs, but the problem of type and value and pointer reference.
The memory structure after the system is running is as follows:
Because it is a pointer reference, it does not matter whether a large bottle or a small bottle.
For more information, see. Net Article 1st: Public Language Runtime Library.
References:
Min Hong, Java and mode, e-Industry Press
[Us] James W. Cooper, C # design model, Electronic Industry Press
[Us] Alan shalloway James R. Trott, design patterns explained, China Power Press
[Us] Robert C. Martin, Agile Software Development-principles, models and practices, Tsinghua University Press
[Us] Don box, Chris sells, 1st. Net essence: Public Language Runtime Library, China Power Press
Http://www.dofactory.com/Patterns/Patterns.aspx