Skip to content

Instantly share code, notes, and snippets.

@DominicFinn
Created April 27, 2017 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DominicFinn/0ae81322e2c25766b924e5a63e974576 to your computer and use it in GitHub Desktop.
Save DominicFinn/0ae81322e2c25766b924e5a63e974576 to your computer and use it in GitHub Desktop.
Classes for my blog post on Object Creation.
sealed class BadDojo
{
public IList<Student> Students { get; set; }
public Sensei Sensei { get; set; }
public string Summary => $"this dojo is run by {Sensei.Name} and has {Students.Count} students";
}
abstract class Person
{
public string Name { get; set; }
protected Person(string name)
{
Name = name;
}
}
sealed class Sensei : Person
{
public Sensei(string name) : base(name)
{
}
}
sealed class Student : Person
{
public Student(string name) : base(name)
{
}
}
sealed class GoodDojo
{
private readonly IList<Student> students;
public Sensei Sensei { get; }
public IEnumerable<Student> Students => this.students;
public GoodDojo(Sensei sensei)
{
Sensei = sensei;
this.students = new List<Student>();
}
public GoodDojo(Sensei sensei, IEnumerable<Student> students)
{
Sensei = sensei;
this.students = students.ToList();
}
public string Summary => $"this dojo is run by {Sensei.Name} and has {Students.Count()} students";
}
class Program
{
static void Main(string[] args)
{
var badDojo = new BadDojo()
{
Sensei = new Sensei("Barney")
};
Console.WriteLine(badDojo.Summary);
}
}
class Program
{
static void Main(string[] args)
{
var goodDojo = new GoodDojo(new Sensei("Dom"));
Console.WriteLine(goodDojo.Summary);
Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment