Last active
September 18, 2015 09:16
-
-
Save jpartogi/6b0a56f2b2303cb46b0b to your computer and use it in GitHub Desktop.
Bad OO Design in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BankAccount | |
{ | |
public BankAccount(string AccountNumber) | |
{ | |
this.AccountNumber = AccountNumber; | |
} | |
public string AccountNumber { get; private set; } | |
public double AccountBalance { get; set; } | |
public double Interest { get; private set; } | |
private const double InterestRate = 5.5; | |
// calculating interest business logic | |
public void CalculateInterest() | |
{ | |
Interest = (InterestRate * AccountBalance) / 100; | |
} | |
public void SaveMoney(double amount) | |
{ | |
AccountBalance = AccountBalance + amount; | |
} | |
public void WithdrawMoney(double amount) | |
{ | |
AccountBalance = AccountBalance - amount; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Shape | |
{ | |
const int CIRCLE = 1; | |
const int RECTANGLE = 2; | |
int TYPE = 0; | |
private double radius; | |
private double width; | |
private double length; | |
public double Area() | |
{ | |
switch (TYPE) | |
{ | |
case CIRCLE: | |
return Math.PI * radius; | |
case RECTANGLE: | |
return width * length; | |
default: | |
return 0; | |
} | |
} | |
public void CreateCircle(double radius) | |
{ | |
this.radius = radius; | |
this.TYPE = CIRCLE; | |
} | |
public void CreateRectangle(double length, double width) | |
{ | |
this.width = width; | |
this.length = length; | |
this.TYPE = RECTANGLE; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IShape | |
{ | |
public double Area(); | |
} | |
public class Rectangle : IShape | |
{ | |
public double width {get; set; } | |
public double length {get; set; } | |
private Rectangle(){} | |
public Rectangle(double width, double length) | |
{ | |
this.width = width; | |
this.length = length; | |
} | |
public double Area() | |
{ | |
return this.width * this.length; | |
} | |
} | |
public class Square : Rectangle | |
{ | |
public double edge { get; set; } | |
public Square(double edge) : base(edge, edge) | |
{ | |
this.edge = edge; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Vehicle | |
{ | |
void Fly(); | |
void Drive(); | |
} | |
public class Car : Vehicle | |
{ | |
public void Drive() | |
{ | |
// Implement drive here | |
} | |
public void Fly() | |
{ | |
throw new Exception("Can not fly"); | |
} | |
} | |
public class Plane : Vehicle | |
{ | |
public void Drive() | |
{ | |
throw new Exception("Not really a good idea"); | |
} | |
public void Fly() | |
{ | |
// Implement fly here | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IOutputter | |
{ | |
void Print(string output); | |
} | |
public class ConsoleOutputter : IOutputter | |
{ | |
public void Print(string output) | |
{ | |
System.Console.Write(output); | |
} | |
} | |
public class MockOutputter : IOutputter | |
{ | |
public void Print(string output) | |
{ | |
// do nothing | |
} | |
} | |
public class CommandInvoker | |
{ | |
public void Invoke(ConsoleOutputter outputter) | |
{ | |
outputter.Print("Hello World"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment