Skip to content

Instantly share code, notes, and snippets.

@aliozgur
Last active May 21, 2018 16:42
Show Gist options
  • Save aliozgur/14cb5d0d174c8fab545794da26531804 to your computer and use it in GitHub Desktop.
Save aliozgur/14cb5d0d174c8fab545794da26531804 to your computer and use it in GitHub Desktop.
GoF-Prototype
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace GofPrototype
{
[Serializable]
public abstract class PersonPrototype
{
public string Name
{
get;
set;
}
public string Surname
{
get;
set;
}
public PersonPrototype Clone()
{
return this.MemberwiseClone() as PersonPrototype;
}
public PersonPrototype DeepCopy()
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, this);
stream.Seek(0, SeekOrigin.Begin);
return formatter.Deserialize(stream) as PersonPrototype;
}
}
public PersonPrototype DeepCopy2()
{
var jsonStr = JsonConvert.SerializeObject(this);
var type = this.GetType();
return JsonConvert.DeserializeObject(jsonStr,type) as PersonPrototype;
}
}
[Serializable]
public class Employee:PersonPrototype
{
public int SicilNo
{
get;
set;
}
}
[Serializable]
public class Student:PersonPrototype
{
public int StudentId
{
get;
set;
}
}
public class Manager:Employee
{
public List<Employee> DirectReports
{
get;
set;
}
public Manager ReportsTo
{
get;
set;
}
}
}
using System.Collections.Generic;
using GofPrototype1;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace GofPrototype.Tests
{
[TestClass]
public class PersonPrototypeTests
{
private Manager CreateMockManager()
{
var manager = new Manager
{
Name = "Ali",
Surname = "Özgür",
SicilNo = 1000,
ReportsTo = new Manager
{
Name = "Mehmet",
Surname = "Genç",
SicilNo = 10,
},
DirectReports = new List<Employee>{
new Employee{
Name = "Ercü"
},
new Employee{
Name = "Onur"
},
new Employee{
Name = "Özlem"
}
}
};
return manager;
}
[TestMethod]
public void EmployeeClone()
{
var proto = new Employee { Name = "Ali", Surname = "Özgür", SicilNo = 1 };
var clone = proto.Clone();
Assert.IsTrue(proto.Name == clone.Name);
Assert.AreNotSame(proto, clone);
}
[TestMethod]
public void StudentDeepCopy()
{
var proto = new Student { Name = "Ali", Surname = "Özgür", StudentId = 1 };
var clone = proto.DeepCopy() as Student;
Assert.IsTrue(proto.Name == clone.Name);
Assert.IsTrue(proto.StudentId == clone.StudentId);
Assert.AreNotSame(proto, clone);
}
[TestMethod]
public void ManagerDeepCopy2()
{
var manager = CreateMockManager();
var clone = manager.DeepCopy2() as Manager;
Assert.AreNotSame(manager, clone);
Assert.AreNotSame(manager.ReportsTo, clone.ReportsTo);
Assert.AreNotSame(manager.DirectReports[0], clone.DirectReports[0]);
}
[TestMethod]
public void ManagerClone()
{
var manager = CreateMockManager();
var clone = manager.Clone() as Manager;
Assert.AreNotSame(manager, clone);
Assert.AreSame(manager.ReportsTo, clone.ReportsTo);
Assert.AreSame(manager.DirectReports[0], clone.DirectReports[0]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment