Skip to content

Instantly share code, notes, and snippets.

@Sathyaish
Created August 2, 2015 07:26
Show Gist options
  • Save Sathyaish/929b565e757761c07f97 to your computer and use it in GitHub Desktop.
Save Sathyaish/929b565e757761c07f97 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NullObjectDesignPattern
{
// Bad design example
abstract class Customer { public virtual string Name { get; set; } }
class GoldCustomer : Customer { public GoldCustomer(string name) { Name = name; } }
class DiamondCustomer : Customer { }
class Enquiry : Customer
{
public override string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
var nullOrDefaultCustomerObject = new Enquiry { Name = "Not a customer yet." };
var customerFactory = new Factory<Customer>(nullOrDefaultCustomerObject);
// will fall back to default
var goldCustomer1 = customerFactory.Create<GoldCustomer>();
Console.WriteLine($"{goldCustomer1.Name}, {goldCustomer1.GetType().Name }");
// will be a gold customer
var goldCustomer2 = customerFactory.Create<GoldCustomer>("John Doe");
Console.WriteLine($"{goldCustomer2.Name}, {goldCustomer2.GetType().Name }");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment