Design Mode learning notes-adapter mode (adapter Patten)

Source: Internet
Author: User

I am quite touched by the design model of terrylee. I will take the following notes for future reference.

Code

    //  --------------------------------------------------------
// Sometimes some previousCodeCannot be modified,
// But it must be used in new projects.
// However, the interface terms in the new project are different.
// How can the code in the old project adapt to the Code in the new project?
// You can use adapter Patten.
// --------------------------------------------------------

# Region Code that cannot be modified, but must be used
Public Interface Ireader
{
Void Read ()
{}
}

Public Class Txtreader: ireader
{
# Region Ireader members

Public Void Read ()
{
Throw New Notimplementedexception ();
}

# Endregion
}

Public Class Xmlreader: ireader
{
# Region Ireader members

Public Void Read ()
{
Throw New Notimplementedexception ();
}

# Endregion
}

# Endregion

# Region Required new interface

Public Interface Igetwayreader
{
Void Getwayreader ();
}

# Endregion

# Region Class Adaptation Mode

Public Class Getwaytxtreader: ireader, igetwayreader
{

# Region Ireader members

Public Void Read ()
{
Throw New Notimplementedexception ();
}

# Endregion

# Region Igetwayreader members

Public Void Getwayreader ()
{
Read ();
}

# Endregion
}

# Endregion

# Region Object Adaptation Mode

Public Class Getwayreader: igetwayreader
{
Ireader reader;

Public Getwayreader (ireader reader)
{
Reader = Reader;
}

# Region Igetwayreader members

Public Void Getwayreader ()
{
Reader. Read ();
}

# Endregion
}

# Endregion

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.