<?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>Sat, 04 Feb 2012 03:08:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.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-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>

