Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save markschabacker/554208 to your computer and use it in GitHub Desktop.

Select an option

Save markschabacker/554208 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
internal class AutomatedNoteStrategyCustomer : IsAAutomatedNoteStrategy<Customer>
{
private Customer _parent;
public AutomatedNoteStrategyCustomer(Customer Parent)
{
_parent = Parent;
}
public string Text
{
get { return string.Format("Automated Note for {0}",_parent.Name); }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
internal class AutomatedNoteStrategyInvoice : IsAAutomatedNoteStrategy<Invoice>
{
private Invoice _invoice;
public AutomatedNoteStrategyInvoice(Invoice Parent)
{
_invoice = Parent;
}
public string Text
{
get { return string.Format("Automated Note cost {0}",_invoice.Cost.ToString()); }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
public class AutomatedNoteStrategyVisitor : IIsANoteParentVisitor
{
public IsAAutomatedNoteStrategy AutomatedNoteStrategy { get; private set; }
public void VisitInvoice(Invoice invoice)
{
this.AutomatedNoteStrategy = new AutomatedNoteStrategyInvoice(invoice);
}
public void VisitCustomer(Customer customer)
{
this.AutomatedNoteStrategy = new AutomatedNoteStrategyCustomer(customer);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
public class Customer : IsANoteParent
{
public string Name { get { return "Customer Name"; } }
public void AcceptVisitor(IIsANoteParentVisitor visitor)
{
visitor.VisitCustomer(this);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
public interface IIsANoteParentVisitor
{
void VisitInvoice(Invoice invoice);
void VisitCustomer(Customer customer);
// Visit(IsANoteParent) methods go here as more classes are added
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
public class Invoice : IsANoteParent
{
public Invoice()
{
Cost = .02M;
}
public decimal Cost { get; private set; }
public void AcceptVisitor(IIsANoteParentVisitor visitor)
{
visitor.VisitInvoice(this);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
public interface IsAAutomatedNoteStrategy
{
string Text { get; }
}
// In this example you don't really need the generic version of the strategy interface
public interface IsAAutomatedNoteStrategy<ParentT> : IsAAutomatedNoteStrategy
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
public interface IsANoteParent
{
void AcceptVisitor(IIsANoteParentVisitor visitor);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
public class Note<ParentT> where ParentT : IsANoteParent
{
private IsAAutomatedNoteStrategy automated_note_strategy;
private ParentT _parent;
public Note(ParentT parent)
{
_parent = parent;
AutomatedNoteStrategyVisitor noteStrategyVisitor = new AutomatedNoteStrategyVisitor();
parent.AcceptVisitor(noteStrategyVisitor);
this.automated_note_strategy = noteStrategyVisitor.AutomatedNoteStrategy;
}
public string Text { get; set; }
public void SetAutomatedText()
{
Text = automated_note_strategy.Text;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment