<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: Tag OCP</title>
    <link>http://blog.objectmentor.com/articles/tag/ocp</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Some Rough Draft TDD Demonstration Videos</title>
      <description>&lt;p&gt;I&amp;#8217;m doing a series of videos on &lt;span class="caps"&gt;TDD&lt;/span&gt;. The ultimate result will be a much more polished version with embedded slides, and such. But as a part of the development process, I&amp;#8217;m creating scratch videos.&lt;/p&gt;


	&lt;p&gt;Much of what you see in these videos will be in the final versions, but those are far in the future relative to this work.&lt;/p&gt;


	&lt;p&gt;Hope you find them interesting.&lt;/p&gt;


	&lt;p&gt;Comments welcome.&lt;/p&gt;


Here is what is already available:
	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://vimeo.com/10569751"&gt;Getting started&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://vimeo.com/10570537"&gt;Adding Operators&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


Next up:
	&lt;ul&gt;
	&lt;li&gt;Removing violation of Open/Closed principle&lt;/li&gt;
		&lt;li&gt;Removing duplication in operations with a combination of the Strategy pattern and the Template Method pattern&lt;/li&gt;
		&lt;li&gt;Adding new operators after the removal of duplication.&lt;/li&gt;
		&lt;li&gt;Reducing coupling by using the Abstract Factory pattern, Dependency Inversion and Dependency Injection&lt;/li&gt;
		&lt;li&gt;Adding a few more operations&lt;/li&gt;
		&lt;li&gt;Allowing the creation of complex &amp;#8220;programs&amp;#8221; or &amp;#8220;macros&amp;#8221; by using the Composite pattern &amp;#8211; and avoiding Liskov Substitution Principle inherent in the GoF version of the pattern&lt;/li&gt;
		&lt;li&gt;Driving the calculator via FitNesse + Slim&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Anyway, that&amp;#8217;s the plan. I&amp;#8217;ll try to add each of these videos over the next few weeks.&lt;/p&gt;</description>
      <pubDate>Tue, 30 Mar 2010 21:01:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:f9144f21-6bbf-4e5d-b525-9e526c000fe8</guid>
      <author>Brett Schuchert</author>
      <link>http://blog.objectmentor.com/articles/2010/03/30/some-rough-draft-tdd-demonstration-videos</link>
      <category>Schuchert's Scattered Synapses </category>
      <category>TDD</category>
      <category>bdd</category>
      <category>video</category>
      <category>demonstration</category>
      <category>design</category>
      <category>patterns</category>
      <category>SRP</category>
      <category>LSP</category>
      <category>OCP</category>
      <category>DIP</category>
    </item>
    <item>
      <title>The Open-Closed Principle for Languages with Open Classes</title>
      <description>&lt;p&gt;We&amp;#8217;ve been having a discussion inside Object Mentor World Design Headquarters about the meaning of the &lt;span class="caps"&gt;OCP&lt;/span&gt; for dynamic languages, like Ruby, with open classes.&lt;/p&gt;


	&lt;p&gt;For example, in Ruby it&amp;#8217;s normal to define a class or module, &lt;em&gt;e.g.,&lt;/em&gt;&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
# foo.rb
class Foo
    def method1 *args
        ...
    end
end
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;and later re-open the class and add (or redefine) methods,&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
# foo2.rb
class Foo
    def method2 *args
        ...
    end
end
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Users of &lt;code&gt;Foo&lt;/code&gt; see all the methods, as if &lt;code&gt;Foo&lt;/code&gt; had one definition.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
foo = Foo.new
foo.method1 :arg1, :arg2
foo.method2 :arg1, :arg2
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Do open classes violate the Open-Closed Principle? Bertrand Meyer articulated &lt;span class="caps"&gt;OCP&lt;/span&gt;. Here is his definition&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Software entities (classes, modules, functions, &lt;em&gt;etc.&lt;/em&gt;) should be open for extension, but closed for modification.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;He elaborated on it &lt;a href="http://se.ethz.ch/~meyer/publications/computer/implicitness.pdf"&gt;here&lt;/a&gt;.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;... This is the open-closed principle, which in my opinion is one of the central innovations of object technology: the ability to use a software component as it is, while retaining the possibility of adding to it later through inheritance. Unlike the records or structures of other approaches, a class of object technology is both closed and open: closed because we can start using it for other components (its clients); open because we can at any time add new properties without invalidating its existing clients.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;a href="http://se.ethz.ch/~meyer/publications/computer/implicitness.pdf"&gt;Tell Less, Say More: The Power of Implicitness&lt;/a&gt;&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;So, if one client &lt;code&gt;require&lt;/code&gt;&amp;#8217;s only &lt;code&gt;foo.rb&lt;/code&gt; and only uses &lt;code&gt;method1&lt;/code&gt;, that client doesn&amp;#8217;t care what &lt;code&gt;foo2.rb&lt;/code&gt; does. However, if the client also &lt;code&gt;require&lt;/code&gt;&amp;#8217;s &lt;code&gt;foo2.rb&lt;/code&gt;, perhaps indirectly through another &lt;code&gt;require&lt;/code&gt;, problems will ensue unless the client is unaffected by what &lt;code&gt;foo2.rb&lt;/code&gt; does.  This looks a lot like the way &amp;#8220;good&amp;#8221; inheritance should behave.&lt;/p&gt;


	&lt;p&gt;So, the answer is &lt;em&gt;no&lt;/em&gt;, we aren&amp;#8217;t violating &lt;span class="caps"&gt;OCP&lt;/span&gt;, as long as we extend a re-opened class following the same rules we would use when inheriting from it.&lt;/p&gt;


	&lt;p&gt;If we use inheritance instead:&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
# foo.rb
class Foo
    def method1 *args
        ...
    end
end
...
class DerivedFoo &amp;lt; Foo
    def method2 *args
        ...
    end
end
...
foo = SubFoo.new    # Instantiate different class...
foo.method1 :arg1, :arg2
foo.method2 :arg1, :arg2
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;One notable difference is that we have to instantiate a different class. This is an important difference. While you can often just use inheritance, and maybe you should prefer it, inheritance only works if you have full control over what types get instantiated and &lt;em&gt;it&amp;#8217;s easy to change which types you use&lt;/em&gt;. Of course, inheritance is also the best approach when you need all behavioral variants &lt;em&gt;simulateneously&lt;/em&gt;, &lt;em&gt;i.e.,&lt;/em&gt; each variant in one or more objects.&lt;/p&gt;


	&lt;p&gt;Sometimes you want to affect the behavior of all instances transparently, without changing the types that are instantiated. A slightly better example, logging method calls, illustrates the point. Here we use the &amp;#8220;famous&amp;#8221; &lt;code&gt;alias_method&lt;/code&gt; in Ruby.&lt;/p&gt;


&lt;pre&gt;
    &lt;code&gt;
# foo.rb
class Foo
    def method1 *args
        ...
    end
end
# logging_foo.rb
class Foo
    alias_method :old_method1, :method1
    def method1 *args
        p "Inside method1(#{args.inspect})" 
        old_method1 *args
    end
end
...
foo = Foo.new
foo.method1 :arg1, :arg2
    &lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;&lt;code&gt;Foo.method1&lt;/code&gt; behaves like a subclass override, with extended behavior that still obeys the &lt;a href="http://www.objectmentor.com/resources/articles/lsp.pdf"&gt;Liskov-Substitution Principle&lt;/a&gt; (LSP).&lt;/p&gt;


	&lt;p&gt;So, I think the &lt;span class="caps"&gt;OCP&lt;/span&gt; can be reworded slightly.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Software entities (classes, modules, functions, &lt;em&gt;etc.&lt;/em&gt;) should be open for extension, but closed for &lt;strong&gt;source&lt;/strong&gt; modification.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;We should not re-open the original source, but adding functionality through a separate source file is okay.&lt;/p&gt;


	&lt;p&gt;Actually, I prefer a slightly different wording.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Software entities (classes, modules, functions, &lt;em&gt;etc.&lt;/em&gt;) should be open for extension, but closed for &lt;strong&gt;source and contract&lt;/strong&gt; modification.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;The extra &lt;strong&gt;and contract&lt;/strong&gt; is redundant with &lt;span class="caps"&gt;LSP&lt;/span&gt;. I don&amp;#8217;t think this kind of redundancy is necessarily bad. ;) The &lt;em&gt;contract&lt;/em&gt; is the set of &lt;em&gt;behavioral expectations&lt;/em&gt; between the &amp;#8220;entity&amp;#8221; and its client(s). Just as it is &lt;a href="http://blog.objectmentor.com/articles/2007/02/17/liskov-substitution-principle-and-the-ruby-core-libraries"&gt;bad to break the contract with inheritance&lt;/a&gt;, it is also bad to break it through open classes.&lt;/p&gt;


	&lt;p&gt;&lt;span class="caps"&gt;OCP&lt;/span&gt; and &lt;span class="caps"&gt;LSP&lt;/span&gt; together are our most important design principles for effective organization of similar &lt;em&gt;vs.&lt;/em&gt; variant behaviors. Inheritance is one way we do this. Open classes provide another way. Aspects provide a third way and are subject to the same &lt;a href="http://www.aosd.net/2007/program/industry/I6-AspectDesignPrinciples.pdf"&gt;design issues&lt;/a&gt;.&lt;/p&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; Meyer, Bertrand (1988). Object-Oriented Software Construction. Prentice Hall. &lt;span class="caps"&gt;ISBN 0136290493&lt;/span&gt;.&lt;/p&gt;</description>
      <pubDate>Thu, 04 Sep 2008 21:42:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:8045ef44-c256-4c10-9edf-7f9fde4a26bd</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2008/09/04/the-open-closed-principle-for-languages-with-open-classes</link>
      <category>Dean's Deprecations</category>
      <category>Dynamic Languages</category>
      <category>Design Principles</category>
      <category>Clean Code</category>
      <category>OCP</category>
      <category>languages</category>
      <category>Ruby</category>
    </item>
  </channel>
</rss>

