<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: SOLID Class Design: The Liskov Substitution Principle</title>
	<atom:link href="http://www.tomdalling.com/blog/software-design/solid-class-design-the-liskov-substitution-principle/feed" rel="self" type="application/rss+xml" />
	<link>http://www.tomdalling.com/blog/software-design/solid-class-design-the-liskov-substitution-principle</link>
	<description>Thoughts of a software developer</description>
	<lastBuildDate>Tue, 08 May 2012 09:24:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Tom Dalling</title>
		<link>http://www.tomdalling.com/blog/software-design/solid-class-design-the-liskov-substitution-principle/comment-page-1#comment-1437</link>
		<dc:creator>Tom Dalling</dc:creator>
		<pubDate>Tue, 08 May 2012 09:24:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.tomdalling.com/?p=267#comment-1437</guid>
		<description>I guess it doesn&#039;t technically violate LSP. However, because there is no difference between flying and non-flying birds in your code, you can remove INotFlyingBird and IFlyingBird. Just inherit straight from IBird. Although, if you&#039;re only subclassing to provide a new value for IBird::MovingDistance, you might as well have a single class called &quot;Bird&quot; with a setMovingDistance method on it.</description>
		<content:encoded><![CDATA[<p>I guess it doesn&#8217;t technically violate LSP. However, because there is no difference between flying and non-flying birds in your code, you can remove INotFlyingBird and IFlyingBird. Just inherit straight from IBird. Although, if you&#8217;re only subclassing to provide a new value for IBird::MovingDistance, you might as well have a single class called &#8220;Bird&#8221; with a setMovingDistance method on it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Efim Zabarsky</title>
		<link>http://www.tomdalling.com/blog/software-design/solid-class-design-the-liskov-substitution-principle/comment-page-1#comment-1431</link>
		<dc:creator>Efim Zabarsky</dc:creator>
		<pubDate>Tue, 01 May 2012 08:52:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.tomdalling.com/?p=267#comment-1431</guid>
		<description>Great article, thanks!

My question, can i do next inheritance, that, in my opinion, not violates LSP?

public interface IBird    
{        
int MovingDistance();    
}    

public abstract class IFlyingBird : IBird    
{       
public abstract int FlyingDistance();        
public int MovingDistance()       
{
            return FlyingDistance();
}    
}    

public abstract class INotFlyingBird : IBird    
{        
public abstract int SteppingDistance();  
public int MovingDistance()        
{            
return SteppingDistance();     
}    
}    

public class KingFisher : IFlyingBird    
{        
public override int FlyingDistance()        
{            
return 600;        
}    
}    

public class Ostrich : INotFlyingBird    
{        
public override int SteppingDistance()        
{            
return 123;        
}    
}

using:
static void Main(string[] args)        
{           
 IBird[] birds = new IBird[2];            
birds[0] = new KingFisher();            
birds[1] = new Ostrich();            
DoFlyBirds(birds);        
}        

private static void DoFlyBirds(IBird[] birds)        
{            
foreach (IBird b in birds)            
{                
Console.WriteLine(&quot;Bird {0} can overcome {1} kilometers&quot;, b.GetType().Name,  b.MovingDistance());            
}        
}

Thanks</description>
		<content:encoded><![CDATA[<p>Great article, thanks!</p>
<p>My question, can i do next inheritance, that, in my opinion, not violates LSP?</p>
<p>public interface IBird   <br />
{       <br />
int MovingDistance();   <br />
}    </p>
<p>public abstract class IFlyingBird : IBird   <br />
{       <br />
public abstract int FlyingDistance();       <br />
public int MovingDistance()       <br />
{<br />
            return FlyingDistance();<br />
}   <br />
}    </p>
<p>public abstract class INotFlyingBird : IBird   <br />
{       <br />
public abstract int SteppingDistance(); <br />
public int MovingDistance()       <br />
{           <br />
return SteppingDistance();     <br />
}   <br />
}    </p>
<p>public class KingFisher : IFlyingBird   <br />
{       <br />
public override int FlyingDistance()       <br />
{           <br />
return 600;       <br />
}   <br />
}    </p>
<p>public class Ostrich : INotFlyingBird   <br />
{       <br />
public override int SteppingDistance()       <br />
{           <br />
return 123;       <br />
}   <br />
}</p>
<p>using:<br />
static void Main(string[] args)       <br />
{           <br />
 IBird[] birds = new IBird[2];           <br />
birds[0] = new KingFisher();           <br />
birds[1] = new Ostrich();           <br />
DoFlyBirds(birds);       <br />
}        </p>
<p>private static void DoFlyBirds(IBird[] birds)       <br />
{           <br />
foreach (IBird b in birds)           <br />
{               <br />
Console.WriteLine(&#8220;Bird {0} can overcome {1} kilometers&#8221;, b.GetType().Name,  b.MovingDistance());           <br />
}       <br />
}</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Dalling</title>
		<link>http://www.tomdalling.com/blog/software-design/solid-class-design-the-liskov-substitution-principle/comment-page-1#comment-583</link>
		<dc:creator>Tom Dalling</dc:creator>
		<pubDate>Wed, 30 Mar 2011 05:58:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.tomdalling.com/?p=267#comment-583</guid>
		<description>Yes, in that respect this Bird/FlightfulBird example isn&#039;t great. If this were real software, I wouldn&#039;t use subclassing for every different type of bird. I&#039;d just have a single Bird class, with a &quot;setCanFly&quot; method on it. That would make the whole problem go away.

When you say that your code snippet looks similar to the &quot;Solution 1&quot; code and therefor violates the OCP, I&#039;d say it&#039;s actually more similar to the &quot;Solution 2&quot; code which doesn&#039;t violate the OCP. This is because when you make a new flightless bird class, you don&#039;t have to modify the ArrangeBirdInPattern function. In &quot;Solution 1&quot;, you have to modify the function every time a new flightless bird class is created.

Like most software development advice, the LSP is just a rule of thumb. You can think of an LSP violation as a kind of code smell. It indicates that there may be a better way to structure the code.

As for the Java exceptions, I had a quick google (doesn&#039;t that sound dirty) and found this: 

class: java.nio.ByteBuffer
method: public ByteBuffer put(ByteBuffer src)
throws: ReadOnlyBufferException (extends UnsupportedOperationException) - If this buffer is read-only

If you have a subclass of ByteBuffer that is read-only, I would say that it does violate the LSP. If you can never call &quot;put&quot;, then it shouldn&#039;t have a &quot;put&quot; method at all. You could get around this by having two interfaces like ReadableByteBuffer and WritableByteBuffer. Then you can choose not to implement WritableByteBuffer if the class is read-only.</description>
		<content:encoded><![CDATA[<p>Yes, in that respect this Bird/FlightfulBird example isn&#8217;t great. If this were real software, I wouldn&#8217;t use subclassing for every different type of bird. I&#8217;d just have a single Bird class, with a &#8220;setCanFly&#8221; method on it. That would make the whole problem go away.</p>
<p>When you say that your code snippet looks similar to the &#8220;Solution 1&#8243; code and therefor violates the OCP, I&#8217;d say it&#8217;s actually more similar to the &#8220;Solution 2&#8243; code which doesn&#8217;t violate the OCP. This is because when you make a new flightless bird class, you don&#8217;t have to modify the ArrangeBirdInPattern function. In &#8220;Solution 1&#8243;, you have to modify the function every time a new flightless bird class is created.</p>
<p>Like most software development advice, the LSP is just a rule of thumb. You can think of an LSP violation as a kind of code smell. It indicates that there may be a better way to structure the code.</p>
<p>As for the Java exceptions, I had a quick google (doesn&#8217;t that sound dirty) and found this: </p>
<p>class: java.nio.ByteBuffer<br />
method: public ByteBuffer put(ByteBuffer src)<br />
throws: ReadOnlyBufferException (extends UnsupportedOperationException) &#8211; If this buffer is read-only</p>
<p>If you have a subclass of ByteBuffer that is read-only, I would say that it does violate the LSP. If you can never call &#8220;put&#8221;, then it shouldn&#8217;t have a &#8220;put&#8221; method at all. You could get around this by having two interfaces like ReadableByteBuffer and WritableByteBuffer. Then you can choose not to implement WritableByteBuffer if the class is read-only.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Dalling</title>
		<link>http://www.tomdalling.com/blog/software-design/solid-class-design-the-liskov-substitution-principle/comment-page-1#comment-584</link>
		<dc:creator>Tom Dalling</dc:creator>
		<pubDate>Wed, 30 Mar 2011 05:58:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.tomdalling.com/?p=267#comment-584</guid>
		<description>Yes, in that respect this Bird/FlightfulBird example isn&#039;t great. If this were real software, I wouldn&#039;t use subclassing for every different type of bird. I&#039;d just have a single Bird class, with a &quot;setCanFly&quot; method on it. That would make the whole problem go away.

When you say that your code snippet looks similar to the &quot;Solution 1&quot; code and therefor violates the OCP, I&#039;d say it&#039;s actually more similar to the &quot;Solution 2&quot; code which doesn&#039;t violate the OCP. This is because when you make a new flightless bird class, you don&#039;t have to modify the ArrangeBirdInPattern function. In &quot;Solution 1&quot;, you have to modify the function every time a new flightless bird class is created.

Like most software development advice, the LSP is just a rule of thumb. You can think of an LSP violation as a kind of code smell. It indicates that there may be a better way to structure the code.

As for the Java exceptions, I had a quick google (doesn&#039;t that sound dirty) and found this: 

class: java.nio.ByteBuffer
method: public ByteBuffer put(ByteBuffer src)
throws: ReadOnlyBufferException (extends UnsupportedOperationException) - If this buffer is read-only

If you have a subclass of ByteBuffer that is read-only, I would say that it does violate the LSP. If you can never call &quot;put&quot;, then it shouldn&#039;t have a &quot;put&quot; method at all. You could get around this by having two interfaces like ReadableByteBuffer and WritableByteBuffer. Then you can choose not to implement WritableByteBuffer if the class is read-only.</description>
		<content:encoded><![CDATA[<p>Yes, in that respect this Bird/FlightfulBird example isn&#8217;t great. If this were real software, I wouldn&#8217;t use subclassing for every different type of bird. I&#8217;d just have a single Bird class, with a &#8220;setCanFly&#8221; method on it. That would make the whole problem go away.</p>
<p>When you say that your code snippet looks similar to the &#8220;Solution 1&#8243; code and therefor violates the OCP, I&#8217;d say it&#8217;s actually more similar to the &#8220;Solution 2&#8243; code which doesn&#8217;t violate the OCP. This is because when you make a new flightless bird class, you don&#8217;t have to modify the ArrangeBirdInPattern function. In &#8220;Solution 1&#8243;, you have to modify the function every time a new flightless bird class is created.</p>
<p>Like most software development advice, the LSP is just a rule of thumb. You can think of an LSP violation as a kind of code smell. It indicates that there may be a better way to structure the code.</p>
<p>As for the Java exceptions, I had a quick google (doesn&#8217;t that sound dirty) and found this: </p>
<p>class: java.nio.ByteBuffer<br />
method: public ByteBuffer put(ByteBuffer src)<br />
throws: ReadOnlyBufferException (extends UnsupportedOperationException) &#8211; If this buffer is read-only</p>
<p>If you have a subclass of ByteBuffer that is read-only, I would say that it does violate the LSP. If you can never call &#8220;put&#8221;, then it shouldn&#8217;t have a &#8220;put&#8221; method at all. You could get around this by having two interfaces like ReadableByteBuffer and WritableByteBuffer. Then you can choose not to implement WritableByteBuffer if the class is read-only.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Noemail</title>
		<link>http://www.tomdalling.com/blog/software-design/solid-class-design-the-liskov-substitution-principle/comment-page-1#comment-582</link>
		<dc:creator>Noemail</dc:creator>
		<pubDate>Sat, 26 Mar 2011 23:53:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.tomdalling.com/?p=267#comment-582</guid>
		<description>Hi Tom,

from a software architecture point of view I can fully agree to you. Subclassing the Bird for the FlightfulBird does make sense to group the flying ones alltogether. It is a reasonable choice both from a software design point of view and the semantically perspective.

But in practice the downsides stay exactly the same. To implement an apropriate ArrangeBirdInPattern-method you need to approximately do the following:

void ArrangeBirdInPattern(Bird* aBird)
{
	FlightfulBird * aFlyingOne= dynamic_cast(aBird);
	if(aFlyingOne)
		ArrangeBirdInSky(aFlyingOne);
	else
		ArrangeBirdOnGround(aBird);
		
}

That looks pretty similar to the &quot;blatant violation of the Open Closed Principle&quot; earlier in this article. The difference is the use of an (abstract) superclass or interface instead of a single concrete subclass of Bird. 

Did I get something wrong or is the LSP rather an academic subtleness and a design faux pas that a practical problem?

Besides in the Java Collection Framework the use of UnsupportedOperationException is pretty common (Collections.unmodifiableList()). The interface List itself even declares that such an Exception may be throws. Is this a violation of LSP?

Best regards
Andy</description>
		<content:encoded><![CDATA[<p>Hi Tom,</p>
<p>from a software architecture point of view I can fully agree to you. Subclassing the Bird for the FlightfulBird does make sense to group the flying ones alltogether. It is a reasonable choice both from a software design point of view and the semantically perspective.</p>
<p>But in practice the downsides stay exactly the same. To implement an apropriate ArrangeBirdInPattern-method you need to approximately do the following:</p>
<p>void ArrangeBirdInPattern(Bird* aBird)<br />
{<br />
	FlightfulBird * aFlyingOne= dynamic_cast(aBird);<br />
	if(aFlyingOne)<br />
		ArrangeBirdInSky(aFlyingOne);<br />
	else<br />
		ArrangeBirdOnGround(aBird);</p>
<p>}</p>
<p>That looks pretty similar to the &#8220;blatant violation of the Open Closed Principle&#8221; earlier in this article. The difference is the use of an (abstract) superclass or interface instead of a single concrete subclass of Bird. </p>
<p>Did I get something wrong or is the LSP rather an academic subtleness and a design faux pas that a practical problem?</p>
<p>Besides in the Java Collection Framework the use of UnsupportedOperationException is pretty common (Collections.unmodifiableList()). The interface List itself even declares that such an Exception may be throws. Is this a violation of LSP?</p>
<p>Best regards<br />
Andy</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SOLID Class Design: The Dependency Inversion Principle &#171; Tom Dalling</title>
		<link>http://www.tomdalling.com/blog/software-design/solid-class-design-the-liskov-substitution-principle/comment-page-1#comment-202</link>
		<dc:creator>SOLID Class Design: The Dependency Inversion Principle &#171; Tom Dalling</dc:creator>
		<pubDate>Sat, 19 Dec 2009 07:41:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.tomdalling.com/?p=267#comment-202</guid>
		<description>[...] &#171; SOLID Class Design: The Liskov Substitution Principle [...]</description>
		<content:encoded><![CDATA[<p>[...] &laquo; SOLID Class Design: The Liskov Substitution Principle [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

