advertisement

Applying SOLID Principles

50 %
50 %
advertisement
Information about Applying SOLID Principles
Technology

Published on February 12, 2014

Author: mikeabney

Source: slideshare.net

Description

How do you tell whether one design is better than another? This talk provides the answer. By walking through a simple example business problem and its software solution, we examine a few key principles that can be applied to make designs simpler, easier to maintain and change, and overall better.
advertisement

OO SOLID Applying SOLID principles to improve object-oriented designs

Say we have a mechanic shop...

ServiceOrder AddPart(Part) Total() 1..* Part Name Price

! public class Part
 {
 private decimal price;
 
 public Part(string name, decimal price)
 {
 this.price = price;
 }
 
 public decimal Price
 {
 get { return price; }
 set { price = value; }
 }
 
 }

! public class ServiceOrder {
 private List<Part> parts;
 
 public ServiceOrder() {
 parts = new List<Part>();
 }
 
 public void AddPart(Part part) {
 parts.Add(part);
 }
 
 public decimal Total() {
 decimal total = 0;
 foreach (Part aPart in parts) {
 total += aPart.Price;
 }
 return total;
 }
 }

Now we want to specify additional quantities of each part. (because we are lazy)

ServiceOrder partsAndQtys : Dictionary AddPart(Part) Total() 1..* Part Name Price

! public class ServiceOrder
 {
 private Dictionary<Part, int> partsAndQuantities;
 
 public ServiceOrder() {
 partsAndQuantities = new Dictionary<Part, int>();
 }
 
 public void AddPart(Part part) {
 this.AddPart(part, 1);
 }
 
 public void AddPart(Part part, int quantity) {
 int currentQuantity = 0;
 if (partsAndQuantities.ContainsKey(part)) {
 currentQuantity = partsAndQuantities[part];
 }
 int newQuantity = currentQuantity + quantity;
 partsAndQuantities.Add(part, newQuantity);
 }
 
 public decimal Total() {
 decimal total = 0;
 foreach (Part aPart in partsAndQuantities.Keys) {
 int quantity = partsAndQuantities[aPart];
 total += aPart.Price * quantity;
 }
 return total;
 }
 }

ServiceOrder AddPart(Part, Qty) Total() 1 1..* LineItem Part Quantity Part * 1 Name Price

! public class LineItem
 {
 private Part part;
 
 private int quantity;
 
 public LineItem(Part part, int quantity) {
 this.part = part;
 this.quantity = quantity;
 }
 
 public Part Part {
 get { return part; }
 }
 
 public int Quantity {
 get { return quantity; }
 private set { quantity = value; }
 }
 }

public class ServiceOrder
 {
 private List<LineItem> lineItems;
 
 public ServiceOrder() {
 lineItems = new List<LineItem>();
 }
 
 public void AddPart(Part part) {
 this.AddPart(part, 1);
 }
 
 public void AddPart(Part part, int quantity) {
 LineItem lineItem = new LineItem(part, quantity);
 lineItems.Add(lineItem);
 }
 
 public decimal Total() {
 decimal total = 0;
 foreach (LineItem aLineItem in lineItems) {
 total += aLineItem.Part.Price * aLineItem.Quantity;
 }
 
 return total;
 }
 }

S ingle Responsibility Principle

Now we want to add a markup for each part. (because we want to track profit margin)

ServiceOrder AddPart(Part, Qty) Total() 1 1..* Part LineItem Part Quantity * 1 Name MarkupPercentage WholesalePrice

! public class Part
 {
 private string name;
 private decimal wholesalePrice;
 private int markupPercentage;
 
 public Part(string name, decimal price) :this(name, price, 0)
 {}
 
 public Part(string name, decimal price, int markupPercentage) {
 this.name = name;
 this.wholesalePrice = price;
 this.markupPercentage = markupPercentage;
 }
 
 public string Name {
 get { return name; }
 }
 
 public decimal WholesalePrice {
 get { return wholesalePrice; }
 }
 
 public int MarkupPercentage {
 get { return markupPercentage; }
 }
 }

! public class ServiceOrder
 {
 private List<LineItem> lineItems;
 
 public ServiceOrder() {
 lineItems = new List<LineItem>();
 }
 
 public void AddPart(Part part) {
 this.AddPart(part, 1);
 }
 
 public void AddPart(Part part, int additionalQuantity) {
 LineItem lineItem = new LineItem(part, additionalQuantity);
 lineItems.Add(lineItem);
 }
 
 public decimal Total() {
 decimal total = 0;
 foreach (LineItem aLineItem in lineItems) {
 total += aLineItem.Part.WholesalePrice
 * (aLineItem.Part.MarkupPercentage/100m + 1)
 * aLineItem.Quantity;
 }
 return total;
 }
 }

ServiceOrder AddPart(Part, Qty) Total() 1 1..* Part LineItem Part Quantity LineTotal() * 1 Name MarkupPercentage WholesalePrice

! // New methods in the Part class 
 public decimal Price {
 get { return wholesalePrice * MarkupMultiplier(); }
 }
 ! private decimal MarkupMultiplier() {
 return (markupPercentage / 100m + 1);
 } ! ! 
 // And one in the LineItem class
 
 public decimal LineTotal() {
 return part.Price * quantity;
 }

! // In the ServiceOrder class...
 
 public decimal Total() {
 
 decimal total = 0;
 foreach (LineItem aLineItem in lineItems) {
 total += aLineItem.LineTotal();
 }
 return total;
 
 }

O pen/Closed Principle

Now we want to add labor costs. (because we want to make more money)

Part Name MarkupPercentage WholesalePrice Price()

Part Name MarkupPercentage WholesalePrice Labor Price Price()

! public class Labor : Part
 {
 public Labor(string name, decimal price) : base(name, price) {}
 }
 


Item Name CalculatePrice() Labor Price CalculatePrice() Part MarkupPercentage WholesalePrice CalculatePrice()

! public interface Item
 {
 string Name { get; }
 decimal CalculatePrice();
 }

L iskov Substitution Principle

Now we want to generate receipts. (because we want to seem professionalish)

Presentation Layer Business Layer ReceiptGenerator ServiceOrder PrintReceipt() AddPart(Part, Qty) Total()

! public class ReceiptGenerator
 {
 private FixItMechanicsSystem system;
 private EpsonTMT88IVPrinter epsonTMT88IVPrinter;
 private string initString = "sasllkslsl398383fa"; 
 public void PrintReceipt() {
 ServiceOrder order = system.FinalizeOrder(); 
 string receiptString = "Total........ $" + order.Total();
 epsonTMT88IVPrinter.Initialize(initString);
 epsonTMT88IVPrinter.PrintBwMed239(receiptString);
 epsonTMT88IVPrinter.Flush(); }
 }

Presentation Layer ReceiptGenerator PrintReceipt() Business Layer Receipt Total() ServiceOrder AddPart(Part, Qty) Total()


 // In the ReciptGenerator class.
 
 public void PrintReceipt() {
 IReceipt receipt = system.FinalizeOrder(); 
 string receiptString = "Total........ $" + receipt.Total();
 epsonTMT88IVPrinter.Initialize(initString);
 epsonTMT88IVPrinter.PrintBwMed239(receiptString);
 epsonTMT88IVPrinter.Flush();
 }

I nterface Segregation Principle

We need to print to a new printer. (because we’re growing and they don’t make the old one anymore)

ReceiptGenerator PrintReceipt() EpsonT88IVPrinter 1 Initialize(String) PrintBwMed239(String) Flush()

! public class ReceiptGenerator
 {
 private FixItMechanicsSystem system;
 private EpsonTMT88IV epsonTMT88IVPrinter;
 private string initString = "sasllkslsl398383fa"; 
 public void PrintReceipt() {
 Receipt receipt = system.FinalizeOrder(); 
 string receiptString = "Total........ $" + receipt.Total();
 epsonTMT88IVPrinter.Initialize(initString);
 epsonTMT88IVPrinter.PrintBwMed239(receiptString);
 epsonTMT88IVPrinter.Flush(); }
 }

ReceiptGenerator PrintReceipt() EpsonPrinterAdapter 1 EpsonT88IVPrinter 1 GenericPrinter Print(String) SamsungPrinterAdapter 1 SamsungSRP350 Initialize(String) Initialize(String) PrintBwMed239(String) ProcessPages(String) Flush()


 public class ReceiptGenerator
 {
 private FixItMechanicsSystem system;
 private IGenericPrinter printer;
 
 public void PrintReceipt() {
 Receipt receipt = system.FinalizeOrder();
 string receiptString = "Total........ $" + receipt.Total();
 printer.Print(receiptString);
 }
 }


 public class EpsonPrinterAdapter : IGenericPrinter
 {
 private EpsonTMT88IV epsonTMT88IVPrinter;
 private string initString = "sasllkslsl398383fa";
 
 public void Print(string printString) {
 epsonTMT88IVPrinter.Initialize(initString);
 epsonTMT88IVPrinter.PrintBwMed239(printString);
 epsonTMT88IVPrinter.Flush();
 }
 }


 public class SamsungPrinterAdapter : IGenericPrinter
 {
 private SamsungSRP350 samsungSRP350Printer;
 private string initString = "aksreiufgu";
 
 public void Print(string printString) {
 samsungSRP350Printer.Init(initString);
 samsungSRP350Printer.ProcessPages(printString);
 }
 }

D ependency Inversion Principle

S ingle Responsibility Principle O pen/Closed Principle L iskov Substitution Principle I nterface Segregation Principle D ependency Inversion Principle

Questions?

X Poor Design • Duplication • Ambiguity • Complexity

✓ Good Design ✓ Low Coupling ✓ High Cohesion

Questions?

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Applying S.O.L.I.D. Principles in Microsoft .NET/C# ...

Applying S.O.L.I.D. Principles in Microsoft .NET/C# | TechEd Europe 2014 | Channel 9. ... it and at this time is when I start to apply SOLID principles.
Read more

Applying S.O.L.I.D. Principles in .NET/C# - YouTube

Applying S.O.L.I.D. Principles in .NET/C# ... Solid Principles by Uncle Bob Martin - Duration: 2:33:20. Michael Kappel 44,037 views. 2:33:20
Read more

Applying SOLID Principles in .NET - video.ch9.ms

Applying SOLID Principles in .NET Subject: TechEd 2014 Description: Template: Jordan Cayabyab, Artitudes Design Formatting: Audience Type: Last modified by:
Read more

design - Applying SOLID principles - Software Engineering ...

Well, you should apply the S of SOLID in this case. A table holds all constraints defined on it. A constraint holds all tables it references.
Read more

Applying S.O.L.I.D. Principles in Microsoft .NET/C# - YouTube

Applying S.O.L.I.D. Principles in Microsoft .NET/C# ... Bob Martin SOLID Principles of Object Oriented and ... and SOLID Design — SVNUG ...
Read more

Applying the SOLID principles - Learning TypeScript [Book]

Applying the SOLID principles As we have previously mentioned, interfaces are fundamental features when it comes to following the SOLID principles, and we ...
Read more

Applying S.O.L.I.D. Principles in .NET/C# | TechEd North ...

Applying S.O.L.I.D. Principles in .NET/C# | TechEd North ... Please can you let me know if there is any tool to measure if the code follows SOLID principles.
Read more

High Cohesion & Low Coupling using SOLID Principles ...

Achieving Low Coupling & High Cohesion by applying SOLID principles ; Author: vaid_sumit; Updated: 20 Jun 2015; Section: Design and Architecture ...
Read more

SOLID Principles in C#

SOLID Principles in C#. ... Intro to SOLID principles SOLID principles are the design principles that enable us to manage with most of the software design ...
Read more

S.O.L.I.D. Software Development, One Step at a Time

You will see how the five SOLID principles can help you to achieve strong encapsulation, ... Get your free trial subscription to CODE Magazine!
Read more