7 kyu

PatternCraft - Adapter

3,635 of 4,217brunolm

Description:

The Adapter Design Pattern can be used, for example in the StarCraft game, to insert an external character in the game.

The pattern consists in having a wrapper class that will adapt the code from the external source.

Your Task

The adapter receives an instance of the object that it is going to adapt and handles it in a way that works with our application.

In this example we have the pre-loaded classes:

class Marine {
  attack(target) {
    target.health -= 6;
  }
}

class Zealot {
  attack(target) {
    target.health -= 8;
  }
}

class Zergling {
  attack(target) {
    target.health -= 5;
  }
}

class Mario {
  jumpAttack() {
    console.log('Mamamia!');
    return 3;
  }
}
public class Target
{
    public int Health { get; set; }
}
public interface IUnit
{
    void Attack(Target target);
}

public class Marine : IUnit
{
    public void Attack(Target target)
    {
        target.Health -= 6;
    }
}

public class Zealot : IUnit
{
    public void Attack(Target target)
    {
        target.Health -= 8;
    }
}

public class Zergling : IUnit
{
    public void Attack(Target target)
    {
        target.Health -= 5;
    }
}

public class Mario
{
    public int jumpAttack()
    {
        Console.WriteLine("Mamamia!");
        return 3;
    }
}
class Marine
  attack: (target) ->
    target.health -= 6

class Zealot
  attack: (target) ->
    target.health -= 8

class Zergling
  attack: (target) ->
    target.health -= 5

class Mario
  jumpAttack: () ->
    console.log 'Mamamia!'
    3
class Target:
    def __init__(self, health):
        self.health = health

class Marine:
    @staticmethod
    def attack(target):
        target.health -= 6
    
class Zealot:
    @staticmethod
    def attack(target):
        target.health -= 8

class Zergling:
    @staticmethod
    def attack(target):
        target.health -= 5
    
class Mario:
    @staticmethod
    def jump_attack():
        print('Mamamia!')
        return 3

Complete the code so that we can create a MarioAdapter that can attack as other units do.

Note to calculate how much damage mario is going to do you have to call the jumpAttack method (jump_attack in Python).

Resources

PatternCraft series

The original PatternCraft series (by John Lindquist) is a collection of Youtube videos that explains some of the design patterns and how they are used (or could be) on StarCraft.

Design Patterns
Fundamentals

Stats:

CreatedJan 9, 2016
PublishedJan 10, 2016
Warriors Trained5316
Total Skips118
Total Code Submissions15684
Total Times Completed4217
JavaScript Completions3635
C# Completions461
CoffeeScript Completions16
Python Completions161
Total Stars66
% of votes with a positive feedback rating85% of 264
Total "Very Satisfied" Votes204
Total "Somewhat Satisfied" Votes43
Total "Not Satisfied" Votes17
Ad
Contributors
  • brunolm Avatar
  • hilary Avatar
  • myjinxin2015 Avatar
  • Blind4Basics Avatar
  • hobovsky Avatar
  • Paul-Cl-ark Avatar
  • kazus Avatar
Ad