(Follow-up on RPG Character Builder)
This implements a fluent interface for RPG game characters. Is this a valid implementation?
There are 3 characters: Paladin
, Wizard
and Elfo
. There are 3 items: Potion
, Sword
, Arch
.
[TestFixture]
public class RPGEngineTest
{
[Test]
public void GivenAPaladin_WithSword_Return()
{
//Arrange
var paladin = CharacterInterface
.Create(new Paladin())
.Named("Conan")
.WithSword()
.Build();
//Act
var result = paladin.Atack();
//Assert
Assert.AreEqual("Conan has split the enemy in two", result);
}
[Test]
public void GivenAPaladin_WithArch_Return()
{
//Arrange
var paladin = CharacterInterface
.Create(new Paladin())
.Named("Conan")
.WithArch()
.Build();
//Act
var result = paladin.Atack();
//Assert
Assert.AreEqual("Conan has hit the enemy's heart", result);
}
[Test]
public void GivenAnElfo_WithArch_Return()
{
//Arrange
var elfo = CharacterInterface
.Create(new Elfo())
.Named("Légolas")
.WithArch()
.Build();
//Act
var result = elfo.Atack();
//Assert
Assert.AreEqual("Légolas has hit the enemy's heart", result);
}
[Test]
public void GivenAnElfo_WithPotion_Return()
{
//Arrange
var elfo = CharacterInterface
.Create(new Elfo())
.Named("Légolas")
.WithPotion()
.Build();
//Act
var result = elfo.Atack();
//Assert
Assert.AreEqual("Légolas has converted the enemy into a monkey", result);
}
[Test]
public void GivenAMago_WithPotion_Return()
{
//Arrange
var mago = CharacterInterface
.Create(new Mago())
.Named("Merlín")
.WithPotion()
.Build();
//Act
var result = mago.Atack();
//Assert
Assert.AreEqual("Merlín has converted the enemy into a monkey", result);
}
[Test]
public void GivenAMago_WithSword_Return()
{
//Arrange
var mago = CharacterInterface
.Create(new Mago())
.Named("Merlín")
.WithSword()
.Build();
//Act
var result = mago.Atack();
//Assert
Assert.AreEqual("Merlín has split the enemy in two", result);
}
}
public interface IItem
{
string Use();
}
public class Sword : IItem
{
public string Use()
{
return "{0} has split the enemy in two";
}
}
public class Arch : IItem
{
public string Use()
{
return "{0} has hit the enemy's heart";
}
}
public class Potion : IItem
{
public string Use()
{
return "{0} has converted the enemy into a monkey";
}
}
public interface IAction
{
string Atack();
}
public interface ICharacter: IAction
{
IItem Item { get; set; }
string Name { get; set; }
}
public class Paladin : ICharacter
{
public IItem Item { get; set; }
public string Name { get; set; }
public string Atack()
{
return string.Format(Item.Use(), Name);
}
}
public class Elfo : ICharacter
{
public IItem Item { get; set; }
public string Name { get; set; }
public string Atack()
{
return string.Format(Item.Use(), Name);
}
}
public class Mago : ICharacter
{
public IItem Item { get; set; }
public string Name { get; set; }
public string Atack()
{
return string.Format(Item.Use(), Name);
}
}
public interface IName
{
IAddItem Named(string name);
}
public interface IAddItem
{
IBuild WithArch();
IBuild WithSword();
IBuild WithPotion();
}
public interface IBuild
{
ICharacter Build();
}
public class CharacterInterface : IName, IAddItem, IBuild
{
private ICharacter _character;
private CharacterInterface(ICharacter character)
{
_character= character;
}
public static IName Create(ICharacter character)
{
return new CharacterInterface(character);
}
public IAddItem Named(string name)
{
_character.Name = name;
return this;
}
public IBuild WithArch()
{
_character.Item = new Arch();
return this;
}
public IBuild WithSword()
{
_character.Item = new Sword();
return this;
}
public IBuild WithPotion()
{
_character.Item = new Potion();
return this;
}
public ICharacter Build()
{
return _character;
}
}
personaje
when the rest of your code is in English. Why not usecharacter
? \$\endgroup\$