Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jill Balzano authored and Jill Balzano committed Aug 4, 2020
0 parents commit 00715a0
Show file tree
Hide file tree
Showing 174 changed files with 21,848 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
Binary file added 9781484261798.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
152 changes: 152 additions & 0 deletions Behavioral/ChainOfResponsibility/BrokerChain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System;
using System.Collections.Generic;
using DotNetDesignPatternDemos.Behavioral.ChainOfResponsibility.MethodChain;
using static System.Console;

namespace DotNetDesignPatternDemos.Behavioral.ChainOfResponsibility.ModifierChain.BrokerChain
{
// command query separation is being used here

public class Query
{
public string CreatureName;

public enum Argument
{
Attack, Defense
}

public Argument WhatToQuery;

public int Value; // bidirectional

public Query(string creatureName, Argument whatToQuery, int value)
{
CreatureName = creatureName;
WhatToQuery = whatToQuery;
Value = value;
}
}

public class Game // mediator pattern
{
public event EventHandler<Query> Queries; // effectively a chain

public void PerformQuery(object sender, Query q)
{
Queries?.Invoke(sender, q);
}
}

public class Creature
{
private readonly Game game;
public string Name;
private readonly int attack;
private readonly int defense;

public Creature(Game game, string name, int attack, int defense)
{
this.game = game;
this.Name = name;
this.attack = attack;
this.defense = defense;
}

public int Attack
{
get
{
var q = new Query(Name, Query.Argument.Attack, attack);
game.PerformQuery(this, q);
return q.Value;
}
}

public int Defense
{
get
{
var q = new Query(Name, Query.Argument.Defense, defense);
game.PerformQuery(this, q);
return q.Value;
}
}

public override string ToString() // no game
{
return $"{nameof(Name)}: {Name}, " +
$"{nameof(attack)}: {Attack}, " +
$"{nameof(defense)}: {Defense}";
// ^^^^^^ using a property ^^^^^^^^^
}
}

public abstract class CreatureModifier : IDisposable
{
protected readonly Game game;
protected readonly Creature creature;

protected CreatureModifier(Game game, Creature creature)
{
this.game = game;
this.creature = creature;
game.Queries += Handle;
}

protected abstract void Handle(object sender, Query q);

public void Dispose()
{
game.Queries -= Handle;
}
}

public class DoubleAttackModifier : CreatureModifier
{
public DoubleAttackModifier(Game game, Creature creature)
: base(game, creature) {}

protected override void Handle(object sender, Query q)
{
if (q.CreatureName == creature.Name &&
q.WhatToQuery == Query.Argument.Attack)
q.Value *= 2;
}
}

public class IncreaseDefenseModifier : CreatureModifier
{
public IncreaseDefenseModifier(Game game, Creature creature) : base(game, creature)
{
}

protected override void Handle(object sender, Query q)
{
if (q.CreatureName == creature.Name &&
q.WhatToQuery == Query.Argument.Defense)
q.Value += 2;
}
}

public class Demo
{
public static void Main()
{
var game = new Game();
var goblin = new Creature(game, "Strong Goblin", 2, 2);
WriteLine(goblin);

using (new DoubleAttackModifier(game, goblin))
{
WriteLine(goblin);
using (new IncreaseDefenseModifier(game, goblin))
{
WriteLine(goblin);
}
}

WriteLine(goblin);
}
}
}
150 changes: 150 additions & 0 deletions Behavioral/ChainOfResponsibility/Exercise.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace DotNetDesignPatternDemos.Behavioral.ChainOfResponsibility
{
namespace Coding.Exercise
{
public abstract class Creature
{
protected Game game;
protected readonly int baseAttack;
protected readonly int baseDefense;

protected Creature(Game game, int baseAttack, int baseDefense)
{
this.game = game;
this.baseAttack = baseAttack;
this.baseDefense = baseDefense;
}

public virtual int Attack { get; set; }
public virtual int Defense { get; set; }
public abstract void Query(object source, StatQuery sq);
}

public class Goblin : Creature
{
public override void Query(object source, StatQuery sq)
{
if (ReferenceEquals(source, this))
{
switch (sq.Statistic)
{
case Statistic.Attack:
sq.Result += baseAttack;
break;
case Statistic.Defense:
sq.Result += baseDefense;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
else
{
if (sq.Statistic == Statistic.Defense)
{
sq.Result++;
}
}
}

public override int Defense
{
get
{
var q = new StatQuery {Statistic = Statistic.Defense};
foreach (var c in game.Creatures)
c.Query(this, q);
return q.Result;
}
}

public override int Attack
{
get
{
var q = new StatQuery {Statistic = Statistic.Attack};
foreach (var c in game.Creatures)
c.Query(this, q);
return q.Result;
}
}

public Goblin(Game game) : base(game, 1, 1)
{
}

protected Goblin(Game game, int baseAttack, int baseDefense)
: base(game, baseAttack, baseDefense)
{
}
}

public class GoblinKing : Goblin
{
public GoblinKing(Game game) : base(game, 3, 3)
{
}

public override void Query(object source, StatQuery sq)
{
if (!ReferenceEquals(source, this)
&& sq.Statistic == Statistic.Attack)
{
sq.Result++; // every goblin gets +1 attack
}
else base.Query(source, sq);
}
}

public enum Statistic
{
Attack,
Defense
}

public class StatQuery
{
public Statistic Statistic;
public int Result;
}

public class Game
{
public IList<Creature> Creatures = new List<Creature>();
}
}

namespace Coding.Exercise.Tests
{
[TestFixture]
public class TestSuite
{
[Test]
public void ManyGoblinsTest()
{
var game = new Game();
var goblin = new Goblin(game);
game.Creatures.Add(goblin);

Assert.That(goblin.Attack, Is.EqualTo(1));
Assert.That(goblin.Defense, Is.EqualTo(1));

var goblin2 = new Goblin(game);
game.Creatures.Add(goblin2);

Assert.That(goblin.Attack, Is.EqualTo(1));
Assert.That(goblin.Defense, Is.EqualTo(2));

var goblin3 = new GoblinKing(game);
game.Creatures.Add(goblin3);

Assert.That(goblin.Attack, Is.EqualTo(2));
Assert.That(goblin.Defense, Is.EqualTo(3));
}
}
}
}
Loading

0 comments on commit 00715a0

Please sign in to comment.