Discovery of design patterns in the Net Framework class library we use (3)

Source: Internet
Author: User

Iterator Pattern)


Many programming tasks are a set of operations. Whether it is a simple object list or a complex B-tree set, we need to frequently access the elements in the set. We often use some methods to access the elements in these sets, for example, from front to back, from back to front, or access in ascending or descending order.
One of the most common basic methods is to store the object list in the array, the array type has been built in VB. net and C # language, they all have a circular structure for listing array elements: in C #, It is foreach, in VB. net is for eeah. Here is an example of an enumerated array:

Int [] values = new int [] {1, 2, 3, 4, 5 };

Foreach (int I in values)

{

Console. Write (I. tostring () + "");

}
These statements use an iterator internally. All you know is that the elements in the array can be enumerated in each loop.
To make these statements run properly, the array object must be ienumerable in the expression, and all the collection classes that implement the ienumerable object can be traversed (that is, enumeration). The ienumerable interface has a method: getenumerator, this method returns a type that implements ienumerator, which enables ienumerator to enumerate the set. It has a current attribute, and has methods that can enumerate elements from start to end in the forward collection-movenext and rest. These object collection classes are all system. in the collections namespace, these collection classes are the same as arrays, And the ienumerable interface is used to enumerate the elements in the set.

If you look at the language msil (Microsoft intermediate language) generated by the C # compiler using the foreach statement, you will find that most collection classes use enumerators for enumeration. The following example uses ienumerator for enumeration. It implements the same functions as the previous example:

Int [] values = new int [] {1, 2, 3, 4, 5 };

Ienumerator E = (ienumerable) values). getenumerator ();

While (E. movenext ())

{

Console. Write (E. Current. tostring () + "");

}
. NET Framework uses ienumerable and ienumerator to apply the iterator mode. The iterator mode allows you to easily traverse a set without displaying the internal processing process. The iteration class that implements ienumerator is separated from the Collection class that implements ienumerable. This class is responsible for processing the enumerated State (including how to express the elements in the current array and how to iterate the elements in the set ), and include the enumerated algorithm. This method allows you to enumerate the set iterator using different methods at the same time without adding any complexity to the set!


Decorator pattern)

Effective executable programs include reading, writing, and writing. Resources are abstracted as byte streams no matter where the Read and Write resources come from ,. net using system. io. stream class to express this abstraction. Whether it comes from text files, TCP/IP network communication, or other entities, filestream and networkstrem inherit the stream class, you can easily process the data regardless of the data source. Below is a method to output the bytes in the stream to the console:

Public static void printbytes (Stream S)

{

Int B;

While (B = FS. readbyte ()> = 0)

{

Console. Write (B + "");

}

}

Reading a byte from a stream at a time is not a very effective method. For example, the hardware can (and most suitable) continuously read data blocks from a disk into a large block, if you know that you want to read some characters, it is better to extract the data block from the disk at one time and process these characters in the memory. In the Net Framework, the bufferedstream class is responsible for processing such operations. The bufferedstream class constructor has a parameter that can be any stream object to be cached. The bufferedstream class reloads many stream methods, for example, read and write provide more powerful functions (this function is actually a buffer, Translator's note ). Since bufferedstream is a subclass of the stream class, you can use it like other stream classes. (It is worth noting that filestream has built-in buffer function)
Similarly, you can use the system. Security. cryptography. cryptostream class to freely encrypt and decrypt any stream object. The following example shows how to call my printing method using several different types of stream objects:

Memorystream MS = new memorystream (New byte [] {1, 2, 3, 4, 5, 6, 7, 8 });

Printbytes (MS );

Bufferedstream buff = new bufferedstream (MS );

Printbytes (buff );

Buff. Close ();

Filestream FS = new filestream (".../decorator.txt", filemode. Open );

Printbytes (FS );

FS. Close ();

This seamless combination method, the ability to dynamically add functions to objects is an example of the decoration mode. As shown in the following figure:

For each stream instance, you can package the stream into a bufferedstream to add a buffer function to the stream without changing the data interface (that is, the stream-type interface ), because it is only a combination object (for example, after the filestream instance is passed as a parameter to the constructor of the bufferedstream instance, this bufferedstream is a filestream instance with the buffer function, other stream instances can also use this operation to expand their functions. This is the essence of the decoration mode! This method is more elegant than inheriting a specific stream type during compilation (for example, if the inherited method is used, filestream implements the buffer function, instead, you have to have a subclass that inherits the filestream class with the buffer function. The biggest problem with this method is that the entire inheritance tree is very bloated. Note ), because the previous method can be implemented at runtime! The core function of the decoration mode is that all the decorators implement an interface or reload an abstract base class (such as stream abstract base class) and provide extra functions. For example, bufferedstream reloads the read method to provide the function of reading data from a buffer, rather than directly reading data like other streams. As in the previous example, any composite modifier provides the same functionality as its base class, no matter how complicated it is.



Note: The Inheritance subclass of system. Io. Stream in Net Framework:

System. Io. Stream
System. Data. oracleclient. oraclebfile
System. Data. oracleclient. canclelob

System. Io. bufferedstream

System. Io. filestream

System. Io. memorystream

System. net. sockets. networkstream

System. Security. cryptography. cryptostream


Bufferedstream and cryptostream are the decorators in the decorator mode. They provide special functions for other stream objects.

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.