Interpreting design Pattern----Adapter Mode (Adapter pattern)

Source: Internet
Author: User

In the works of Jin Yong, the three great Martial magic is rare treasure, how many people get him and ..., careful analysis of these three martial, or North is better, hehe. From the point of view of software design, this is not a kind of reuse of the idea, but a bit cruel. In the field of software design, "reuse" in some cases will also appear many problems, such as the platform is incompatible, the development of different languages or interfaces, and many other reasons, the bad will not appear both waste of other people's existing resources, and their own system can not be completed? It's a bit like a star-sucking----and damaging.

Attempt to do a good job, can be once and for all to get a good, such a desire to the above mentioned in general, this is ineffective and hopeless. Because the change in demand is the most volatile, we can not at the beginning of the design will be the need for the changes are considered thoroughly, because we are not prophets, and not wise, just a little understanding of design technology craftsmen. So how do we deal with change?

Is there a magic set of laws that can absorb the internal forces of others while not losing the internal forces of others and will absorb the skill for their own use? Oh, perhaps is looking at this article of you to invent this set of laws, will also become the same as Zhang a generation of Guru (see I said that cut ...) )。 In fact, in software design, as in the case of this story in general, for example, there is a design of Class A (there is a method of public void Aa () {}), we need to call him in the current system, but the system is provided by Interface IB (interface under the definition of a method void Bb ();), How do we get these two mismatched interfaces to work together at this point?

1public class A
2{
3 public void Aa ()
4 {
5//... Logic slightly
6}
7}
8public interface IB
9{
-Bb ();
13//?????????
14//How can we match the AA method of Class A to the BB () method of the IB interface?

Here, if a friend who is familiar with design patterns should have thought of an object-oriented design pattern--Adapter (Adapter) pattern--he is used to solve the problem raised above. The adapter pattern transforms the interface of a class into another interface that the client expects, so that two classes that do not match the original interface can work together. Some people also call this pattern packaging (wrapper) mode.

Let's take a look at a very imprecise example (this is detected in a blog message of a predecessor, specifically don't remember clearly), how to achieve the "doubled"? The schematic code is as follows:

The Sparrow that can fly

1namespace DesignPattern.Adapter.FlyingTigers
2{
3/**////<summary>
4///Sparrow will fly
5///</ Summary>
6 public class Sparrow
7 {
8 private string name;
9 public string name
{One get
{return Name;}
set {name = value;}
The public
Sparrow () {} is public
Sparrow (string name)
{
this.name = name; /
}
Public
void Fly ()
Console.WriteLine ("I am {0}, I can fly!", this. Name);
25}

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.