Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Liskov Substitution example #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 36 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2094,21 +2094,24 @@ RenderLargeRectangles(rectangles);
**Good:**

```csharp
using System;
using System.Collections.Generic;

abstract class ShapeBase
{
protected double Width = 0;
protected double Height = 0;
public abstract double GetArea();

abstract public double GetArea();

public Drawable Render(double area)
public void Render(double area)
{
// ...
//...
}
}

class Rectangle : ShapeBase
{
private double Width = 0;
private double Height = 0;

public void SetWidth(double width)
{
Width = width;
Expand All @@ -2119,7 +2122,7 @@ class Rectangle : ShapeBase
Height = height;
}

public double GetArea()
public override double GetArea()
{
return Width * Height;
}
Expand All @@ -2129,38 +2132,43 @@ class Square : ShapeBase
{
private double Length = 0;

public double SetLength(double length)
public void SetLength(double length)
{
Length = length;
}

public double GetArea()
public override double GetArea()
{
return Math.Pow(Length, 2);
}
}

Drawable RenderLargeRectangles(Rectangle rectangles)
{
foreach (rectangle in rectangles)
{
if (rectangle is Square)
{
rectangle.SetLength(5);
}
else if (rectangle is Rectangle)
{
rectangle.SetWidth(4);
rectangle.SetHeight(5);
}

var area = rectangle.GetArea();
rectangle.Render(area);
}
public class Program
{
public static void Main() {
IList<ShapeBase> list = new List<ShapeBase>(){
new Square(),
new Rectangle(),
new Rectangle()
};

foreach (ShapeBase sb in list) {
if (sb is Square) {
((Square)sb).SetLength(5); //a
}

if (sb is Rectangle) {
((Rectangle)sb).SetHeight(4);//a
((Rectangle)sb).SetWidth(5);//b
}

double area = sb.GetArea();

Console.WriteLine(area.ToString());
}
}
}

var shapes = new[] { new Rectangle(), new Rectangle(), new Square() };
RenderLargeRectangles(shapes);
```

**[⬆ back to top](#table-of-contents)**
Expand Down