7 kyu

Adapter Pattern - Geese to Ducks

380 of 587Klypyon

Description:

Task

Given an interface IDuck, you are to create an Object Adapter (using Composition) in order to adapt the Goose class to support the IDuck interface.

IDuck Interface

public interface IDuck
{
    string Quack();
    void Fly();
}
class IDuck
{
public:
    virtual std::string Quack() = 0;
    
    virtual void Fly() = 0;
};

Goose Class

public class Goose
{
    string Honk();
    void Fly();
}
class Goose
{
public:
    std::string Honk();
    
    void Fly();
}

Adapter Pattern

The adapter pattern converts the interface of one class into the interface of another that the client expects. Adapters allow one class to work in an instance where it otherwise couldn't because of incompatible interfaces.

The adapter structure uses either composition (object adapters) or inheritance (class adapters, used in languages that support multiple inheritance) to provide the expected interface that the client relies on. The participants of the pattern involve the following:

  1. [Target] - Defines the domain-specific interface that the [Client] will use.
  2. [Client] - Collaborates with the objects conforming to the [Target] interface.
  3. [Adaptee] - Defines an existing interface that needs adapting.
  4. [Adapter] - Adapts the interface of the [Adaptee] to the [Target] interface.

Adapter UML

![``` [ Client ] ═══► [ Target ] [ Adaptee ] Request() SepcificRequest() ▲ ▲ ╚═════ [ Adapter ] ═════════════╝ Request() adaptee | └-----------------------► adaptee->SpecificRequest()

```](http://oi66.tinypic.com/2ajb70g.jpg)

For more information on the Adapter Pattern, see:

Design Patterns
Fundamentals

More By Author:

Check out these other kata created by Klypyon

Stats:

CreatedJul 23, 2016
PublishedJul 23, 2016
Warriors Trained1070
Total Skips78
Total Code Submissions2161
Total Times Completed587
C# Completions380
C++ Completions218
Total Stars23
% of votes with a positive feedback rating85% of 106
Total "Very Satisfied" Votes78
Total "Somewhat Satisfied" Votes24
Total "Not Satisfied" Votes4
Total Rank Assessments8
Average Assessed Rank
7 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • Klypyon Avatar
  • Dentzil Avatar
  • user5036852 Avatar
  • FArekkusu Avatar
  • hobovsky Avatar
Ad