Design Pattern discovered in. Net Class Library: decoration pattern

Source: Internet
Author: User

Decoration mode:

Dynamically add some additional responsibilities to an object. The decorator mode is more flexible than the subclass generation function.

Applications in. Net:

Public abstract class log

{

Public abstract void write (string log );

}

Public class databaselog: Log

{

Public override void write (string log)

{

//... Record to the database

}

}

Public class textfilelog: Log

{

Public override void write (string log)

{

//... Record to text file

}

}

Public abstract class logwrapper: Log

{

Private log _ log;

Public logwrapper (log)

{

_ Log = log;

}

Public override void write (string log)

{

_ Log. Write (log );

}

}

Public class logerrorwrapper: logwrapper

{

Public logerrorwrapper (log _ log)

: Base (_ log)

{

}

Public override void write (string log)

{

Seterror (); // ...... Function Extension

Base. Write (log );

}

Public void seterror ()

{

// ...... Implements the record error Severity Level

}

}

Public class logprioritywrapper: logwrapper

{

Public logprioritywrapper (log _ log)

: Base (_ log)

{

}

Public override void write (string log)

{

Setpriority (); // ...... Function Extension

Base. Write (log );

}

Public void setpriority ()

{

//... Sets the record priority.

}

}

At this point, the logerrorwrapper class and logprioritywrapper class actually implement the extension of the features of the error severity level and priority level. Let's take a look at how the customer program calls it:

Public class Program

{

Public static void main (string [] ARGs)

{

Log = new databaselog ();

Logwrapper lew1 = new logerrorwrapper (log );

// Extended the record error Severity Level

Lew1.write ("Log message ");

Logprioritywrapper lpw1 = new logprioritywrapper (log );

// Extended record priority

Lpw1.write ("Log message ");

Logwrapper lew2 = new logerrorwrapper (log );

Logprioritywrapper lpw2 = new logprioritywrapper (lew2); // here is lew2

// The error severity and priority are also extended.

Lpw2.write ("Log message ");

}

}

A typical application of the decoration mode in. NET is stream, which has the following class structure:

Figure 8

System. io. stream abstract class, which can easily process data from filestream, networkstrem, or other entities, regardless of the data source, below is a method to output the byte 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 structure of the bufferedstream class has only one parameter. This parameter 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 ). Because
Bufferedstream is a subclass of the stream class, so 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.

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

Reference: http://www.cnblogs.com/zhongkeruanjian/archive/2006/03/20/353959.html

Http://terrylee.cnblogs.com/archive/2006/03/01/340592.html

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.