From 6ddfea459925d0912eec890b706b3e95ddc27026 Mon Sep 17 00:00:00 2001 From: Paulo Duarte Date: Sat, 29 Jun 2019 12:16:40 -0300 Subject: [PATCH] #58 Included Primitive Obssession pattern example --- README.md | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/README.md b/README.md index ce5c4ad..0cecbf8 100644 --- a/README.md +++ b/README.md @@ -1664,6 +1664,98 @@ Console.WriteLine(employee.GetName());// Employee name: John Doe +
+ Avoid Primitive Obsession + +It is important to differentiate a well-defined object from a lot of loose and repeated attributes in various places. + +**Bad:** + +```csharp + class Person + { + public string FirstName { get; set; } + public string LastName { get; set; } + public string Address { get; set; } + public string City { get; set; } + public string State { get; set; } + public string Country { get; set; } + public string ZipCode { get; set; } + } + + var badPerson = new Person(); + + badPerson.FirstName = "John"; + badPerson.LastName = "Doe"; + badPerson.Address = "Buck Drive"; + badPerson.City = "Montpelier"; + badPerson.State = "Vermont"; + badPerson.Country = "USA"; + badPerson.ZipCode = "99999"; + + Console.WriteLine($"{badPerson.FirstName} {badPerson.LastName}"); //John Doe + Console.WriteLine($"{badPerson.Address}, {badPerson.City}, {badPerson.State}, {badPerson.Country}, {badPerson.ZipCode}"); //Buck Drive, Montpelier, Vermont, USA, 99999 +``` + +**Good:** + +```csharp + struct Name + { + public string FirstName, LastName; + + public Name(string firstName, string lastName) + { + FirstName = firstName; + LastName = lastName; + } + + public override string ToString() + { + return $"{FirstName} {LastName}"; + } + } + + struct Address + { + public string Street, City, State, Country, ZipCode; + + public Address(string street, string city, string state, string country, string zipCode) + { + Street = street; + City = city; + State = state; + Country = country; + ZipCode = zipCode; + } + + public override string ToString() + { + return $"{Street}, {City}, {State}, {Country}, {ZipCode}"; + } + } + + class Person + { + public Name Name { get; set; } + public Address Address { get; set; } + } + + var goodPerson = new Person(); + + goodPerson.Name = new Name("John","Doe"); + goodPerson.Address = new Address("Buck Drive", "Montpelier", "Vermont", "USA", "99999"); + + Console.WriteLine($"{goodPerson.Name}"); //John Doe + Console.WriteLine($"{goodPerson.Address}"); //Buck Drive, Montpelier, Vermont, USA, 99999 +``` + +Although we have more code in the second way, we gain from reuse, maintainability and consistency of information. + +**[⬆ back to top](#table-of-contents)** + +
+ ## Classes