<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://www.eqqon.com/skins/common/feed.css?270"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://www.eqqon.com/index.php?action=history&amp;feed=atom&amp;title=Design_For_Reuse</id>
		<title>Design For Reuse - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://www.eqqon.com/index.php?action=history&amp;feed=atom&amp;title=Design_For_Reuse"/>
		<link rel="alternate" type="text/html" href="http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;action=history"/>
		<updated>2026-04-26T16:07:48Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.16.0</generator>

	<entry>
		<id>http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=567&amp;oldid=prev</id>
		<title>Henon: /* The Value of Single-Purpose-Classes */</title>
		<link rel="alternate" type="text/html" href="http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=567&amp;oldid=prev"/>
				<updated>2008-02-19T23:06:52Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;The Value of Single-Purpose-Classes&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
		&lt;tr valign='top'&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 23:06, 19 February 2008&lt;/td&gt;
		&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;= The Value of Single-Purpose-Classes =&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;=&lt;/ins&gt;= The Value of Single-Purpose-Classes &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;=&lt;/ins&gt;=&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Most of the classes that get written are used once and never again. The same special problem the class is intended to solve will never appear again. The effort of creating and testing them is practically wasted because it's value is gained only once for a specific part of a software. Often, similar problems arise but adopting the existing classes is too expensive so we need to write new classes again from scratch. If we were able to write classes that are highly reusable we had the opportunity to build a huge resource of diversely applicable modules which could be reused over and over again without any overhead. The commercial potential of a highly reusable class is enormous for a software company.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Most of the classes that get written are used once and never again. The same special problem the class is intended to solve will never appear again. The effort of creating and testing them is practically wasted because it's value is gained only once for a specific part of a software. Often, similar problems arise but adopting the existing classes is too expensive so we need to write new classes again from scratch. If we were able to write classes that are highly reusable we had the opportunity to build a huge resource of diversely applicable modules which could be reused over and over again without any overhead. The commercial potential of a highly reusable class is enormous for a software company.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wikidb_eqqon_pub:diff:version:1.11a:oldid:472:newid:567 --&gt;
&lt;/table&gt;</summary>
		<author><name>Henon</name></author>	</entry>

	<entry>
		<id>http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=472&amp;oldid=prev</id>
		<title>Henon: /* Selection of Objects in a GUI */</title>
		<link rel="alternate" type="text/html" href="http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=472&amp;oldid=prev"/>
				<updated>2007-12-09T15:05:38Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Selection of Objects in a GUI&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
		&lt;tr valign='top'&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 15:05, 9 December 2007&lt;/td&gt;
		&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 42:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 42:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;I will try to explain the design guidelines on a small, easy to understand example where I started with a single not very reusable class and after some refactoring ended up with a quite flexible result consisting of six small highly reusable classes.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;I will try to explain the design guidelines on a small, easy to understand example where I started with a single not very reusable class and after some refactoring ended up with a quite flexible result consisting of six small highly reusable classes.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;=== &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;The selection &lt;/del&gt;of &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;graphical objects &lt;/del&gt;===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;=== &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Selection &lt;/ins&gt;of &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Objects in a GUI &lt;/ins&gt;===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Back then, in my inexperienced days, I was writing a graphics engine. It had a class named '''Canvas''' which could draw graphic objects and supported all sorts of user interaction like picking, moving, scrolling ... etc. Canvas also maintained a list of currently selected objects. As you can see, in my naive first try, I mixed a lot of functionalities in the Canvas class which was a huge monolithic unmaintainable chaos-class.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Back then, in my inexperienced days, I was writing a graphics engine. It had a class named '''Canvas''' which could draw graphic objects and supported all sorts of user interaction like picking, moving, scrolling ... etc. Canvas also maintained a list of currently selected objects. As you can see, in my naive first try, I mixed a lot of functionalities in the Canvas class which was a huge monolithic unmaintainable chaos-class.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wikidb_eqqon_pub:diff:version:1.11a:oldid:471:newid:472 --&gt;
&lt;/table&gt;</summary>
		<author><name>Henon</name></author>	</entry>

	<entry>
		<id>http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=471&amp;oldid=prev</id>
		<title>Henon at 15:04, 9 December 2007</title>
		<link rel="alternate" type="text/html" href="http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=471&amp;oldid=prev"/>
				<updated>2007-12-09T15:04:06Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
		&lt;tr valign='top'&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 15:04, 9 December 2007&lt;/td&gt;
		&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 59:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 59:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;One last step had to be done yet. Realizing that SelectionController was not application specific I wanted to place it into a library. The problem was that the selection strategies needed to call two methods '''Select()''' and '''Unselect()''' on the graphic objects stored in the selection hashtable which are used to advise them to change their representation accordingly. To be highly reusable SelectionController and its strategies should work with all kinds of objects. These objects should not need inherit from a specific base class just to be clients of SelectionController. So I created the '''interface ISelectable''' which exposes the Select() and Unselect() methods. The incorporation of this interface eventually removed the last bit of application specific code and made SelectionController ubiquitously applicable and flexible.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;One last step had to be done yet. Realizing that SelectionController was not application specific I wanted to place it into a library. The problem was that the selection strategies needed to call two methods '''Select()''' and '''Unselect()''' on the graphic objects stored in the selection hashtable which are used to advise them to change their representation accordingly. To be highly reusable SelectionController and its strategies should work with all kinds of objects. These objects should not need inherit from a specific base class just to be clients of SelectionController. So I created the '''interface ISelectable''' which exposes the Select() and Unselect() methods. The incorporation of this interface eventually removed the last bit of application specific code and made SelectionController ubiquitously applicable and flexible.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;--[[User:Henon|Henon]] 16:04, 9 December 2007 (CET)&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wikidb_eqqon_pub:diff:version:1.11a:oldid:470:newid:471 --&gt;
&lt;/table&gt;</summary>
		<author><name>Henon</name></author>	</entry>

	<entry>
		<id>http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=470&amp;oldid=prev</id>
		<title>Henon: /* The selection of graphical objects */</title>
		<link rel="alternate" type="text/html" href="http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=470&amp;oldid=prev"/>
				<updated>2007-12-09T15:03:49Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;The selection of graphical objects&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
		&lt;tr valign='top'&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 15:03, 9 December 2007&lt;/td&gt;
		&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 58:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 58:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Now we already have six very simple classes that were initially mixed up in a single class. The reuse potential of SelectionController is much better now, because one has fine grained control over it's behavior and it is amazingly small and simple.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Now we already have six very simple classes that were initially mixed up in a single class. The reuse potential of SelectionController is much better now, because one has fine grained control over it's behavior and it is amazingly small and simple.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;One last step had to be done yet. Realizing that SelectionController was not application specific I wanted to place it into a library. The problem was that the selection strategies needed to call two methods '''Select()''' and '''Unselect()''' on the graphic objects stored in the selection hashtable which are used to advise them to change their representation accordingly. To be highly reusable SelectionController and its strategies should work with all kinds of objects. These objects should not need inherit from a specific base class just to be clients of SelectionController. So I created the '''interface ISelectable''' which exposes the Select() and Unselect() methods. The incorporation of this interface eventually removed the last bit of application specific code and made SelectionController ubiquitously applicable.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;One last step had to be done yet. Realizing that SelectionController was not application specific I wanted to place it into a library. The problem was that the selection strategies needed to call two methods '''Select()''' and '''Unselect()''' on the graphic objects stored in the selection hashtable which are used to advise them to change their representation accordingly. To be highly reusable SelectionController and its strategies should work with all kinds of objects. These objects should not need inherit from a specific base class just to be clients of SelectionController. So I created the '''interface ISelectable''' which exposes the Select() and Unselect() methods. The incorporation of this interface eventually removed the last bit of application specific code and made SelectionController ubiquitously applicable &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;and flexible&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wikidb_eqqon_pub:diff:version:1.11a:oldid:469:newid:470 --&gt;
&lt;/table&gt;</summary>
		<author><name>Henon</name></author>	</entry>

	<entry>
		<id>http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=469&amp;oldid=prev</id>
		<title>Henon: /* Single Purpose Classes */</title>
		<link rel="alternate" type="text/html" href="http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=469&amp;oldid=prev"/>
				<updated>2007-12-09T15:02:12Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Single Purpose Classes&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
		&lt;tr valign='top'&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 15:02, 9 December 2007&lt;/td&gt;
		&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 8:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 8:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;=== Single Purpose Classes ===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;=== Single Purpose Classes ===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;The most important thing is to keep a class small and simple. The rule of thumb here is to analytically dissect the functionality and divide everything into different classes what can be divided. A functionality immediately looses its reuse potential if it is mixed with other functionalities. A class which is made only for a single purpose and holds only a single simple &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;functionality &lt;/del&gt;is what you should be going for. &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;The &lt;/del&gt;class is easily reused because its implementation is slim and can be easily understood. The documentation of the class is &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;slim &lt;/del&gt;because &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;it has &lt;/del&gt;only one simple functionality. The simple class can be combined with other classes to solve complicated problems. &amp;nbsp;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;The most important thing is to keep a class small and simple. The rule of thumb here is to analytically dissect the functionality and divide everything into different classes what can be divided. A functionality immediately looses its reuse potential if it is mixed with other functionalities &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;in a hard coded way&lt;/ins&gt;. A class which is made only for a single purpose and holds only a single simple &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;responsibility &lt;/ins&gt;is what you should &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;always &lt;/ins&gt;be going for. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Such a &lt;/ins&gt;class is easily reused because its implementation is slim and &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;thus &lt;/ins&gt;can be easily understood. The documentation of the class is &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;small &lt;/ins&gt;because only one simple functionality &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;has to be described&lt;/ins&gt;. The simple class can be combined with other classes &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;dynamically &lt;/ins&gt;to solve complicated problems.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;A badly factored class &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;holding &lt;/del&gt;multiple &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;functionalities &lt;/del&gt;when reused in a different context always bears the risk of unforeseeable bugs resulting from a functional part of the class that does not fit in the new context. When attempting to reuse a multi-purpose-class it might be necessary to work around some aspects which are not needed in the reuse context effectively reducing the value of reusing it. The risk that the reusing engineer is misunderstanding how the multiple functionalities work together in a fat class is high. The result are bugs which are hard to spot which cut the value gained from reusing, or even worse, force you to write it all new.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;A badly factored class &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;having &lt;/ins&gt;multiple &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;responsibilities, &lt;/ins&gt;when reused in a different context&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;, &lt;/ins&gt;always bears the risk of unforeseeable bugs resulting from a functional part of the class that does not fit in the new context. When attempting to reuse a multi-purpose-class it might be necessary to work around some aspects which are not needed in the reuse context effectively reducing the value of reusing it. The risk that the reusing engineer is misunderstanding how the multiple functionalities work together in a fat class is high. The result are bugs which are hard to spot which cut the value gained from reusing, or even worse, force you to write it all new.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;=== Side-Effect Free Methods ===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;=== Side-Effect Free Methods ===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wikidb_eqqon_pub:diff:version:1.11a:oldid:468:newid:469 --&gt;
&lt;/table&gt;</summary>
		<author><name>Henon</name></author>	</entry>

	<entry>
		<id>http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=468&amp;oldid=prev</id>
		<title>Henon: /* Kill any Application-Specific Code */</title>
		<link rel="alternate" type="text/html" href="http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=468&amp;oldid=prev"/>
				<updated>2007-12-09T14:58:41Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Kill any Application-Specific Code&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
		&lt;tr valign='top'&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 14:58, 9 December 2007&lt;/td&gt;
		&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 34:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 34:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Quite often the same problem where one could apply Strategy is solved by using inheritance. The badly factored class where the strategy pattern should have been applied is sub-classed and the specific parts are overridden. Despite the fact, that it is a commonly accepted way of solving the reuse problem, I think most of the time it were better to use Strategy instead of dubious inheritance. Inheritance is most useful if you are able to make use of polymorphism and late binding. If inheritance is used just to reuse some functionality in another unrelated class and nothing else then it surely is better to use composition instead. With composition, the other class that has a functionality your class needs is just used as is. It is owned or referenced by the user class instead of inherited. This leaves your user class free to inherit from a class that makes more sense to inherit from and brings you the freedom of replacing the used functionality without changing class hierarchies.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Quite often the same problem where one could apply Strategy is solved by using inheritance. The badly factored class where the strategy pattern should have been applied is sub-classed and the specific parts are overridden. Despite the fact, that it is a commonly accepted way of solving the reuse problem, I think most of the time it were better to use Strategy instead of dubious inheritance. Inheritance is most useful if you are able to make use of polymorphism and late binding. If inheritance is used just to reuse some functionality in another unrelated class and nothing else then it surely is better to use composition instead. With composition, the other class that has a functionality your class needs is just used as is. It is owned or referenced by the user class instead of inherited. This leaves your user class free to inherit from a class that makes more sense to inherit from and brings you the freedom of replacing the used functionality without changing class hierarchies.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;=== Kill any Application-Specific Code ===&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;Last but not least, it must be said, that application specific parts in a class are a great show stopper for reuse. Encapsulate any application specific things into application classes. To make sure, that it really works move your reusable class in a library project that is not part of the application. This will bring up any hidden dependencies which have to be eliminated.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;== A Practical Example ==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;== A Practical Example ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wikidb_eqqon_pub:diff:version:1.11a:oldid:467:newid:468 --&gt;
&lt;/table&gt;</summary>
		<author><name>Henon</name></author>	</entry>

	<entry>
		<id>http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=467&amp;oldid=prev</id>
		<title>Henon: /* A Practical Example ... continued */</title>
		<link rel="alternate" type="text/html" href="http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=467&amp;oldid=prev"/>
				<updated>2007-12-09T14:48:18Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;A Practical Example ... continued&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
			&lt;col class='diff-marker' /&gt;
			&lt;col class='diff-content' /&gt;
		&lt;tr valign='top'&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 14:48, 9 December 2007&lt;/td&gt;
		&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 40:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 40:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;I will try to explain the design guidelines on a small, easy to understand example where I started with a single not very reusable class and after some refactoring ended up with a quite flexible result consisting of six small highly reusable classes.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;I will try to explain the design guidelines on a small, easy to understand example where I started with a single not very reusable class and after some refactoring ended up with a quite flexible result consisting of six small highly reusable classes.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;... to be &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;continued &lt;/del&gt;...&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;=== The selection of graphical objects ===&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Back then, in my inexperienced days, I was writing a graphics engine&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;It had a class named '''Canvas''' which could draw graphic objects and supported all sorts of user interaction like picking, moving, scrolling &lt;/ins&gt;..&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;. etc. Canvas also maintained a list of currently selected objects. As you can see, in my naive first try, I mixed a lot of functionalities in the Canvas class which was a huge monolithic unmaintainable chaos-class.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;These days I had to extend an existing engine (Piccolo.NET) with object-selection interaction and, of course, I did not repeat this mistake. I created a class called '''SelectionController''' which should maintain the selection and implement the select/deselect behavior on top of the events OnClick ... etc. &lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;;The intended purpose of SelectionController was:&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;:Maintain a list of selected objects (for efficiency it holds them as keys in a Hashtable) and implement exclusive selection behavior. (Exclusive selection behavior means an object is deselected automatically if another object is selected, except if the CTRL key is pressed)&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Soon I hit some problems. Piccolo's Click event was also fired if the up-location was far away from the down-location. In other words, dragging also fired a click event. I decided that the SelectionController should maintain a last_down_location of the mouse too. Wait a minute, I would need that in other locations of my program too. The class '''ClickController''' was born. '''SelectionController''' could be notified by the '''ClickController''' about click events and had one less unnecessary responsibility. The ClickControllers functionality could be easily cut-and-pasted into a different class without any problems. I was going with the design rule '''Single-Purpose-Classes'''. &lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Doing this I also found some other functionality that was unrelated to the original purpose of SelectionController. I broke it out into the class '''KeyboardController''' having the responsibility to maintain keyboard states and deliver them via a convenient interface (i.e IsCtrlPressed). Some days later I earned the fruits of doing so, because I found out, that I needed multiple SelectionControllers which could now share the same KeyboardController effectively not wasting memory and processor time by maintaining their own keyboard states each.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;The next clue illustrates the '''Strategy Pattern'''-design guide. Of course It were possible that different selection mechanisms are needed. I immediately refactored '''SelectionController''' and changed so that it used a class named '''SelectionStrategy.Exclusive''' which implemented the behaviour described above. Later alternative behaviors like '''SelectionStrategy.Simple''' and '''SelectionStrategy.Radio''' followed because they were needed.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Now we already have six very simple classes that were initially mixed up in a single class. The reuse potential of SelectionController is much better now, because one has fine grained control over it's behavior and it is amazingly small and simple.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;nbsp;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;One last step had to be done yet. Realizing that SelectionController was not application specific I wanted to place it into a library. The problem was that the selection strategies needed to call two methods '''Select()''' and '''Unselect()''' on the graphic objects stored in the selection hashtable which are used to advise them to change their representation accordingly. To be highly reusable SelectionController and its strategies should work with all kinds of objects. These objects should not need inherit from a specific base class just &lt;/ins&gt;to be &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;clients of SelectionController&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;So I created the '''interface ISelectable''' which exposes the Select() and Unselect() methods&lt;/ins&gt;. &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;The incorporation of this interface eventually removed the last bit of application specific code and made SelectionController ubiquitously applicable&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wikidb_eqqon_pub:diff:version:1.11a:oldid:461:newid:467 --&gt;
&lt;/table&gt;</summary>
		<author><name>Henon</name></author>	</entry>

	<entry>
		<id>http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=461&amp;oldid=prev</id>
		<title>Henon: New page: = The Value of Single-Purpose-Classes =  Most of the classes that get written are used once and never again. The same special problem the class is intended to solve will never appear again...</title>
		<link rel="alternate" type="text/html" href="http://www.eqqon.com/index.php?title=Design_For_Reuse&amp;diff=461&amp;oldid=prev"/>
				<updated>2007-12-07T11:45:27Z</updated>
		
		<summary type="html">&lt;p&gt;New page: = The Value of Single-Purpose-Classes =  Most of the classes that get written are used once and never again. The same special problem the class is intended to solve will never appear again...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= The Value of Single-Purpose-Classes =&lt;br /&gt;
&lt;br /&gt;
Most of the classes that get written are used once and never again. The same special problem the class is intended to solve will never appear again. The effort of creating and testing them is practically wasted because it's value is gained only once for a specific part of a software. Often, similar problems arise but adopting the existing classes is too expensive so we need to write new classes again from scratch. If we were able to write classes that are highly reusable we had the opportunity to build a huge resource of diversely applicable modules which could be reused over and over again without any overhead. The commercial potential of a highly reusable class is enormous for a software company.&lt;br /&gt;
&lt;br /&gt;
== A Design Guide to Highly Reusable Classes ==&lt;br /&gt;
&lt;br /&gt;
To rip off these benefits it is essential to understand what makes a class highly reusable and what design guidelines to follow. Here they are:&lt;br /&gt;
&lt;br /&gt;
=== Single Purpose Classes ===&lt;br /&gt;
The most important thing is to keep a class small and simple. The rule of thumb here is to analytically dissect the functionality and divide everything into different classes what can be divided. A functionality immediately looses its reuse potential if it is mixed with other functionalities. A class which is made only for a single purpose and holds only a single simple functionality is what you should be going for. The class is easily reused because its implementation is slim and can be easily understood. The documentation of the class is slim because it has only one simple functionality. The simple class can be combined with other classes to solve complicated problems. &lt;br /&gt;
&lt;br /&gt;
A badly factored class holding multiple functionalities when reused in a different context always bears the risk of unforeseeable bugs resulting from a functional part of the class that does not fit in the new context. When attempting to reuse a multi-purpose-class it might be necessary to work around some aspects which are not needed in the reuse context effectively reducing the value of reusing it. The risk that the reusing engineer is misunderstanding how the multiple functionalities work together in a fat class is high. The result are bugs which are hard to spot which cut the value gained from reusing, or even worse, force you to write it all new.&lt;br /&gt;
&lt;br /&gt;
=== Side-Effect Free Methods ===&lt;br /&gt;
The methods of a class must not have any side-effects. A side-effect is something that might not be expected by the caller of the method. Side-effects inherently poison any attempts to reuse. Side-effect eliminating workarounds nullify any value gained from reusing. The resulting solution is prone to errors and hard to understand.&lt;br /&gt;
&lt;br /&gt;
If there are any mandatory side-effects which cannot be avoided at all convert them into regular expected functionality by asserting on pre- and post-conditions that enforce the side-effect behavior.&lt;br /&gt;
&lt;br /&gt;
=== Naming ===&lt;br /&gt;
Names are not just hollow words. It is extremely important to name classes and methods (and all other identifiers) such that the name describes the purpose of the class/method as exactly as possible. Put some effort in finding a good name and the users of your class (who might be you yourself after some time) will find it tremendously easier to understand and reuse it. &lt;br /&gt;
&lt;br /&gt;
Badly chosen names lead to bugs or even prevent possible reuse which is, from our point of view, the worst thing that could ever happen.&lt;br /&gt;
&lt;br /&gt;
=== Decoupling through Events ===&lt;br /&gt;
Putting different functionality in different classes is often not enough to provide a reusable solution. If those classes were to notify each other by maintaining hard coded references to each other they would be too highly coupled to be reused singularly. Events resolve such reuse-stopping dependencies and render related classes replaceable by any other class. The interchangeability of a piece of functionality is most essential for achieving reusable code.&lt;br /&gt;
&lt;br /&gt;
=== Use Interfaces to Decouple Unrelated Functionalities ===&lt;br /&gt;
Classes which implement services often need to call methods on their clients or even store them in a data structure. Using events for decoupling sometimes is not possible or efficient enough. The service class could enforce all their clients to inherit from an abstract client class or something like that but these unnecessary restrictions would decrease the usability and prevent reuse. The solution is to declare a client interface which is required for the client classes to request the services of the class in question. &lt;br /&gt;
&lt;br /&gt;
=== Use the Strategy Pattern to Abstract from Specific Needs ===&lt;br /&gt;
Quite often when analytically dissecting the functionality of a design I come to a point where there might be multiple ways of doing something. Those who reuse your class will surely need something different than you need right now. This is when you should think about applying the Strategy Pattern [Gamma et al 95]. The functionality or algorithm that might be replaced by a similar but different one needs to be factored out and placed in its own class. The user may supply its own algorithm and replace yours.&lt;br /&gt;
&lt;br /&gt;
=== Use Composition in Favor of Dubious Inheritance ===&lt;br /&gt;
Quite often the same problem where one could apply Strategy is solved by using inheritance. The badly factored class where the strategy pattern should have been applied is sub-classed and the specific parts are overridden. Despite the fact, that it is a commonly accepted way of solving the reuse problem, I think most of the time it were better to use Strategy instead of dubious inheritance. Inheritance is most useful if you are able to make use of polymorphism and late binding. If inheritance is used just to reuse some functionality in another unrelated class and nothing else then it surely is better to use composition instead. With composition, the other class that has a functionality your class needs is just used as is. It is owned or referenced by the user class instead of inherited. This leaves your user class free to inherit from a class that makes more sense to inherit from and brings you the freedom of replacing the used functionality without changing class hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== A Practical Example ==&lt;br /&gt;
Once you get used to it, it is not very hard to follow the guide. Of course, this approach has a little &amp;quot;thinking and refactoring&amp;quot; overhead over a naive approach but in the end you quickly get a large set of small grained highly reusable classes which can be combined to flexible solutions of complicated problems with little effort.&lt;br /&gt;
&lt;br /&gt;
I will try to explain the design guidelines on a small, easy to understand example where I started with a single not very reusable class and after some refactoring ended up with a quite flexible result consisting of six small highly reusable classes.&lt;br /&gt;
&lt;br /&gt;
... to be continued ...&lt;/div&gt;</summary>
		<author><name>Henon</name></author>	</entry>

	</feed>