Skip to content

Instantly share code, notes, and snippets.

@dbuksbaum
Last active August 29, 2015 14:20
Show Gist options
  • Save dbuksbaum/d59d0726000f623c0114 to your computer and use it in GitHub Desktop.
Save dbuksbaum/d59d0726000f623c0114 to your computer and use it in GitHub Desktop.
Patterns 101 – Factory Pattern
class CreatorLEDTelevision : ITelevisionCreator
{
public Television CreateTelevision()
{
return new LED();
}
}
class CreatorLCDTelevision : ITelevisionCreator
{
public Television CreateTelevision()
{
return new LCD();
}
}
class CreatorPlasmaTelevision : ITelevisionCreator
{
public Television CreateTelevision()
{
return new Plasma();
}
}
namespace Caliburn.Micro
{
using System;
using System.Reflection;
using System.Windows;
/// <summary>
/// Represents the conventions for a particular element type.
/// </summary>
public class ElementConvention
{
/// <summary>
/// The type of element to which the conventions apply.
/// </summary>
public Type ElementType;
/// <summary>
/// Gets the default property to be used in binding conventions.
/// </summary>
public Func<DependencyObject, DependencyProperty> GetBindableProperty;
/// <summary>
/// The default trigger to be used when wiring actions on this element.
/// </summary>
public Func<System.Windows.Interactivity.TriggerBase> CreateTrigger;
/// <summary>
/// The default property to be used for parameters of this type in actions.
/// </summary>
public string ParameterProperty;
/// <summary>
/// Applies custom conventions for elements of this type.
/// </summary>
/// <remarks>Pass the view model type, property path, property instance, framework element and its convention.</remarks>
public Func<Type, string, PropertyInfo, FrameworkElement, ElementConvention, bool> ApplyBinding =
(viewModelType, path, property, element, convention) => ConventionManager.SetBinding(viewModelType, path, property, element, convention);
}
}
interface ITelevisionCreator
{
Television CreateTelevision();
}
class LCD : Television
{
}
class LED : Television
{
}
class Plasma : Television
{
}
abstract class Television
{
private readonly string _typeName;
protected Television()
{
_typeName = GetType().Name;
}
public void TurnOn()
{
Console.WriteLine("Type == {0} Action == TurnOn()", _typeName);
}
public void TurnOff()
{
Console.WriteLine("Type == {0} Action == TurnOff()", _typeName);
}
public void SetChannel(int channelNumber)
{
Console.WriteLine("Type == {0} Action == SetChannel({1})", _typeName, channelNumber);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment