Adapter for design mode Learning)

Source: Internet
Author: User

The adapter mode is a structure mode that describes how to combine classes or objects to obtain a larger structure.

Adapter mode:

Purpose:Convert a class orProgramTo adapt to the needs of the customer class.

Implementation points:It is implemented by means of inheritance or object combination, namely Class adapter and Object Adapter. It is also a design model that we often unconsciously use.

Code:

    Class  Targetclass
{
Public Void Upiple ()
{
Console. writeline ( " It works well " );
}
}
Class Classadapter: targetclass
{
Public Void Ipiple ()
{
Console. writeline ( " Use a class adapter to implement a U-shaped Port: " );
Base . Upiple ();
}
}
Class Objectadapter
{
Targetclass T1 = New Targetclass ();
Public Void Ipiple ()
{
Console. writeline ( " Implement the U-type port through the Object Adapter: " );
T1.upiple ();
}
}
// ------------------ Run ----------------------------
Class Program
{
Static Void Main ( String [] ARGs)
{
// If you are limited to certain conditions and can only use the I-port pipe, you can use an adapter to call the U-port pipe.
Classadapter adp1 = New Classadapter ();
Objectadapter adp2 = New Objectadapter ();
Adp1.ipiple ();
Adp2.ipiple ();
Console. Readline ();
}
}
 
 

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.