# Design pattern question

**URL:** <https://rubytalk.org/t/design-pattern-question/65206>\
**Category:** ruby-talk\
**Created:** [5 April 2012 12:58 UTC](https://rubytalk.org/t/design-pattern-question/65206 "2012-04-05T12:58:32Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_French](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@James\_French](https://rubytalk.org/u/James_French)\
**Post date:** [5 April 2012 12:58 UTC](https://rubytalk.org/t/design-pattern-question/65206/1 "2012-04-05T12:58:32Z")

</div>

Hi group,

I wonder whether you could give me any advice on how to structure some code?

Its pretty simple really. I want to be able to call an API like this:

SourceControl.checkout  
SourceControl.update

Etc

I want to be able to plugin different source control systems that conform to the API.

My initial thought was just to go with a simple C++ style wrapper/delegate approach:

module SourceControl

def self.checkout(repository, dest, options={})  
&nbsp;&nbsp;@sourceControl.checkout(repository, dest, options)  
end

def self.update(path, options={})  
&nbsp;&nbsp;@sourceControl.update(parth, options)  
end

class SourceControlAPI

&nbsp;&nbsp;def checkout(repository, dest, options={})  
&nbsp;&nbsp;end

&nbsp;&nbsp;def update(path, options={})  
&nbsp;&nbsp;end

end

end

But I bet there is a nicer way, perhaps without having to duplicate the API?

Any pointers appreciated.

Cheers,  
James

---

<div class="post-metadata">

**Author:** ![Saji\_Hameed](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/saji_hameed/32/2120_2.png) [@Saji\_Hameed](https://rubytalk.org/u/Saji_Hameed)\
**Post date:** [5 April 2012 13:17 UTC](https://rubytalk.org/t/design-pattern-question/65206/2 "2012-04-05T13:17:05Z")

</div>

Hi,

> I wonder whether you could give me any advice on how to structure some code?  
> Its pretty simple really. I want to be able to call an API like this:

> SourceControl.checkout
> 
> SourceControl.update

Maybe this one gives some good pointers..

> **[Ruby: Replace method\_missing with dynamic method definitions](http://blog.jayfields.com/2008/02/ruby-replace-methodmissing-with-dynamic.html)**
>
> You have methods you want to handle dynamically without the pain of debugging method\_missing. class Decorator def initialize ( subject )...

hope it helps,  
saji

> **···**
>
> --
> 
> Saji N Hameed,  
> ARC-ENV, Center for Advanced Information Science and Technology,  
> University of Aizu, Tsuruga, Ikki-machi,  
> Aizuwakamatsu-shi, Fukushima 965-8580,  
> Japan
> 
> Tel: +81242 37-2736  
> Fax:+81242 37-2760  
> email: saji@u-aizu.ac.jp  
> url: [http://www.u-aizu.ac.jp](http://www.u-aizu.ac.jp)  
> bib: [Web of Science](http://www.researcherid.com/rid/B-9188-2009)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [5 April 2012 13:19 UTC](https://rubytalk.org/t/design-pattern-question/65206/3 "2012-04-05T13:19:50Z")

</div>

James French wrote in post #1055131:

> Its pretty simple really. I want to be able to call an API like this:
> 
> SourceControl.checkout  
> SourceControl.update

You better do

sc.checkout  
sc.update

i.e. not use a constant but an instance variable.

> I want to be able to plugin different source control systems that  
> conform to the API.

Well, in Ruby there are no interfaces so you can, but do not need to use  
inheritance.

> My initial thought was just to go with a simple C++ style  
> wrapper/delegate approach:
> 
> module SourceControl
> 
> def self.checkout(repository, dest, options={})  
> &nbsp;&nbsp;@sourceControl.checkout(repository, dest, options)  
> end
> 
> def self.update(path, options={})  
> &nbsp;&nbsp;@sourceControl.update(parth, options)  
> end
> 
> class SourceControlAPI
> 
> &nbsp;&nbsp;def checkout(repository, dest, options={})  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def update(path, options={})  
> &nbsp;&nbsp;end
> 
> end
> 
> end
> 
> But I bet there is a nicer way, perhaps without having to duplicate the  
> API?

Especially, what advantages does it have?

I'd probably simply do

class CVS  
&nbsp;&nbsp;def checkout(dest, options={})  
&nbsp;&nbsp;end

&nbsp;&nbsp;def update(path, options={})  
&nbsp;&nbsp;end  
end

class SVN  
&nbsp;&nbsp;def checkout(dest, options={})  
&nbsp;&nbsp;end

&nbsp;&nbsp;def update(path, options={})  
&nbsp;&nbsp;end  
end

class Perforce  
&nbsp;&nbsp;def checkout(dest, options={})  
&nbsp;&nbsp;end

&nbsp;&nbsp;def update(path, options={})  
&nbsp;&nbsp;end  
end

and then

# connection  
sc = SVN.new("svn-repo.my.network.com", "James", "p@ssword")

# repository  
sc.checkout("repo", "/tmp/work") do |repo|  
&nbsp;&nbsp;repo.update("foo")  
&nbsp;&nbsp;File.open(repo.path + "foo", "a") {|io| io.write("a change)}  
&nbsp;&nbsp;repo.submit("foo")  
end # auto commit

sc.close # needed?

You could even make SVN.new accept a block which receives "sc" and  
cleans up in ensure regardless how the block terminates.

[http://blog.rubybestpractices.com/posts/rklemme/002\_Writing\_Block\_Methods.html](http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html)

Kind regards

robert

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_French](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@James\_French](https://rubytalk.org/u/James_French)\
**Post date:** [5 April 2012 14:03 UTC](https://rubytalk.org/t/design-pattern-question/65206/4 "2012-04-05T14:03:51Z")

</div>

Hi,

> I wonder whether you could give me any advice on how to structure some code?  
> Its pretty simple really. I want to be able to call an API like this:

> SourceControl.checkout
> 
> SourceControl.update

Maybe this one gives some good pointers..

> **[Ruby: Replace method\_missing with dynamic method definitions](http://blog.jayfields.com/2008/02/ruby-replace-methodmissing-with-dynamic.html)**
>
> You have methods you want to handle dynamically without the pain of debugging method\_missing. class Decorator def initialize ( subject )...

hope it helps,  
saji

Thanks for the pointer - I'll take a look at that.

> **···**
>
> -----Original Message-----  
> From: saji.uaizu@gmail.com [mailto:saji.uaizu@gmail.com] On Behalf Of Saji Hameed  
> Sent: 05 April 2012 14:17  
> To: ruby-talk ML  
> Subject: Re: Design pattern question

---

<div class="post-metadata">

**Author:** ![James\_French](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@James\_French](https://rubytalk.org/u/James_French)\
**Post date:** [5 April 2012 14:25 UTC](https://rubytalk.org/t/design-pattern-question/65206/5 "2012-04-05T14:25:18Z")

</div>

James French wrote in post #1055131:

> Its pretty simple really. I want to be able to call an API like this:
> 
> SourceControl.checkout  
> SourceControl.update

You better do

sc.checkout  
sc.update

i.e. not use a constant but an instance variable.

> I want to be able to plugin different source control systems that  
> conform to the API.

Well, in Ruby there are no interfaces so you can, but do not need to use inheritance.

> My initial thought was just to go with a simple C++ style  
> wrapper/delegate approach:
> 
> module SourceControl
> 
> def self.checkout(repository, dest, options={})  
> &nbsp;&nbsp;@sourceControl.checkout(repository, dest, options) end
> 
> def self.update(path, options={})  
> &nbsp;&nbsp;@sourceControl.update(parth, options) end
> 
> class SourceControlAPI
> 
> &nbsp;&nbsp;def checkout(repository, dest, options={})  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def update(path, options={})  
> &nbsp;&nbsp;end
> 
> end
> 
> end
> 
> But I bet there is a nicer way, perhaps without having to duplicate  
> the API?

Especially, what advantages does it have?

I'd probably simply do

class CVS  
&nbsp;&nbsp;def checkout(dest, options={})  
&nbsp;&nbsp;end

&nbsp;&nbsp;def update(path, options={})  
&nbsp;&nbsp;end  
end

class SVN  
&nbsp;&nbsp;def checkout(dest, options={})  
&nbsp;&nbsp;end

&nbsp;&nbsp;def update(path, options={})  
&nbsp;&nbsp;end  
end

class Perforce  
&nbsp;&nbsp;def checkout(dest, options={})  
&nbsp;&nbsp;end

&nbsp;&nbsp;def update(path, options={})  
&nbsp;&nbsp;end  
end

and then

# connection  
sc = SVN.new("svn-repo.my.network.com", "James", "p@ssword")

# repository  
sc.checkout("repo", "/tmp/work") do |repo|  
&nbsp;&nbsp;repo.update("foo")  
&nbsp;&nbsp;File.open(repo.path + "foo", "a") {|io| io.write("a change)}  
&nbsp;&nbsp;repo.submit("foo")  
end # auto commit

sc.close # needed?

You could even make SVN.new accept a block which receives "sc" and cleans up in ensure regardless how the block terminates.

[http://blog.rubybestpractices.com/posts/rklemme/002\_Writing\_Block\_Methods.html](http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html)

Kind regards

robert

Using the Jay Fields link and slightly modifying I've got something I'm happy with. Thanks for the help Robert, as always 🙂

module SourceControl  
&nbsp;&nbsp;def self.init(subject)  
&nbsp;&nbsp;&nbsp;&nbsp;subject.public\_methods(false).each do |meth|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(class \<\< self; self; end).class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method meth do |\*args|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subject.send meth, \*args  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

class SourceControlAPI  
&nbsp;&nbsp;  
&nbsp;&nbsp;def checkout(repository, dest, options={})  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;def update(path, options={})  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
end

class SVN \< SourceControlAPI  
&nbsp;&nbsp;  
&nbsp;&nbsp;def checkout(repository, dest, options={})  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Checking out from SVN"  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;def update(path, options={})  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
end

SourceControl.init(SVN.new)  
SourceControl.checkout('foo', 'bar')

> **···**
>
> -----Original Message-----  
> From: Robert Klemme [mailto:lists@ruby-forum.com]  
> Sent: 05 April 2012 14:20  
> To: ruby-talk ML  
> Subject: Re: Design pattern question

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [5 April 2012 15:08 UTC](https://rubytalk.org/t/design-pattern-question/65206/6 "2012-04-05T15:08:06Z")

</div>

> Using the Jay Fields link and slightly modifying I've got something I'm happy with. Thanks for the help Robert, as always 🙂
> 
> module SourceControl  
> def self.init(subject)  
> subject.public\_methods(false).each do |meth|  
> (class \<\< self; self; end).class\_eval do  
> define\_method meth do |\*args|  
> subject.send meth, \*args  
> end  
> end  
> end  
> end  
> end

That's just a mechanism to generate delegation. And you redefine  
methods all the time. I don't think meta programming is necessary at  
all here.

> class SourceControlAPI
> 
> def checkout(repository, dest, options={})  
> end
> 
> def update(path, options={})  
> end

Those are not really needed. And if you want to have them then I'd  
make them either contain a default implementation if there exists one  
or throw an exception which explains that this method must be  
implemented for the class.

> end
> 
> class SVN \< SourceControlAPI
> 
> def checkout(repository, dest, options={})  
> puts "Checking out from SVN"  
> end
> 
> def update(path, options={})  
> end
> 
> end
> 
> SourceControl.init(SVN.new)  
> SourceControl.checkout('foo', 'bar')

You are hiding specific state in a constant. I don't like that  
because it will prevent accessing multiple different source control  
systems in the same program or from multiple threads. I find that  
requirement to be able to invoke all the methods through the constant  
leads to a situation where you severely cripple modularity and  
reusability without really gaining something.

Kind regards

robert

> **···**
>
> On Thu, Apr 5, 2012 at 4:25 PM, James French \<James.French@naturalmotion.com\> wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![James\_French](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@James\_French](https://rubytalk.org/u/James_French)\
**Post date:** [5 April 2012 16:16 UTC](https://rubytalk.org/t/design-pattern-question/65206/7 "2012-04-05T16:16:09Z")

</div>

> Using the Jay Fields link and slightly modifying I've got something  
> I'm happy with. Thanks for the help Robert, as always 🙂
> 
> module SourceControl  
> def self.init(subject)  
> subject.public\_methods(false).each do |meth|  
> (class \<\< self; self; end).class\_eval do  
> define\_method meth do |\*args|  
> subject.send meth, \*args  
> end  
> end  
> end  
> end  
> end

That's just a mechanism to generate delegation. And you redefine methods all the time. I don't think meta programming is necessary at all here.

> class SourceControlAPI
> 
> def checkout(repository, dest, options={})  
> end
> 
> def update(path, options={})  
> end

Those are not really needed. And if you want to have them then I'd make them either contain a default implementation if there exists one or throw an exception which explains that this method must be implemented for the class.

True, but I want to document the API somewhere central. I raise exceptions saying the method must be implement.

> end
> 
> class SVN \< SourceControlAPI
> 
> def checkout(repository, dest, options={})  
> puts "Checking out from SVN"  
> end
> 
> def update(path, options={})  
> end
> 
> end
> 
> SourceControl.init(SVN.new)  
> SourceControl.checkout('foo', 'bar')

You are hiding specific state in a constant. I don't like that because it will prevent accessing multiple different source control systems in the same program or from multiple threads. I find that requirement to be able to invoke all the methods through the constant leads to a situation where you severely cripple modularity and reusability without really gaining something.

Also true, if I wanted to do that, but I don't. I just need to initialise it at startup and use the source control system that was automatically determined from then on, without knowing which one it is. I think this API is working really well for me... I also just like the API of SourceControl.checkout etc.

Kind regards

robert

> **···**
>
> -----Original Message-----  
> From: Robert Klemme [mailto:shortcutter@googlemail.com]  
> Sent: 05 April 2012 16:08  
> To: ruby-talk ML  
> Subject: Re: Design pattern question  
> On Thu, Apr 5, 2012 at 4:25 PM, James French \<James.French@naturalmotion.com\> wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Steve\_Klabnik](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@Steve\_Klabnik](https://rubytalk.org/u/Steve_Klabnik)\
**Post date:** [5 April 2012 16:19 UTC](https://rubytalk.org/t/design-pattern-question/65206/8 "2012-04-05T16:19:20Z")

</div>

> True, but I want to document the API somewhere central. I raise exceptions saying the method must be implement.

That's not very ruby-like at all.

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [5 April 2012 19:16 UTC](https://rubytalk.org/t/design-pattern-question/65206/9 "2012-04-05T19:16:27Z")

</div>

Yes it is, in that it is good design. Otherwise, why would we have NotImplementedError? We use it all the time in our projects that implement an abstract interface:

graph/dev/lib/dep\_analyzer.rb: raise NotImplementedError, "subclass responsibility"  
graph/dev/lib/dep\_analyzer.rb: raise NotImplementedError, "subclass responsibility"  
graph/dev/lib/dep\_analyzer.rb: raise NotImplementedError, "subclass responsibility"

> **···**
>
> On Apr 5, 2012, at 09:19 , Steve Klabnik wrote:
> 
> > > True, but I want to document the API somewhere central. I raise exceptions saying the method must be implement.
> > 
> > That's not very ruby-like at all.

---

<div class="post-metadata">

**Author:** ![Steve\_Klabnik](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@Steve\_Klabnik](https://rubytalk.org/u/Steve_Klabnik)\
**Post date:** [5 April 2012 19:27 UTC](https://rubytalk.org/t/design-pattern-question/65206/10 "2012-04-05T19:27:58Z")

</div>

We'll have to agree to disagree on this one, Ryan. Abstract classes  
are pretty silly in Ruby. Then again, I find subclassing distasteful  
in general, so this might just be a style/preference thing.

---

<div class="post-metadata">

**Author:** ![11142](https://avatars.discourse-cdn.com/v4/letter/1/e0b2c6/32.png) [@11142](https://rubytalk.org/u/11142)\
**Post date:** [5 April 2012 21:19 UTC](https://rubytalk.org/t/design-pattern-question/65206/11 "2012-04-05T21:19:51Z")

</div>

W dniu 5 kwietnia 2012 21:27 użytkownik Steve Klabnik  
\<steve@steveklabnik.com\> napisał:

> We'll have to agree to disagree on this one, Ryan. Abstract classes

\> are pretty silly in Ruby. Then again, I find subclassing distasteful  
\> in general, so this might just be a style/preference thing.

Abstract classes are sometimes useful (although rarely). For example,  
how else could you do the kind of database adapter handling Sequel[1]  
does?

[1] [sequel | RubyGems.org | your community gem host](http://rubygems.org/gems/sequel)

-- Matma Rex

---

<div class="post-metadata">

**Author:** ![Steve\_Klabnik](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@Steve\_Klabnik](https://rubytalk.org/u/Steve_Klabnik)\
**Post date:** [5 April 2012 21:26 UTC](https://rubytalk.org/t/design-pattern-question/65206/12 "2012-04-05T21:26:10Z")

</div>

> Abstract classes are sometimes useful (although rarely). For example,  
> how else could you do the kind of database adapter handling Sequel[1]  
> does?

Use duck typing.

This:

class Abstract  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;throw NotImplementedException  
&nbsp;&nbsp;end  
end

class Foo \< Abstract  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts "in foo!"  
&nbsp;&nbsp;end  
end

class Bar \< Abstract  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts "in bar's foo"  
&nbsp;&nbsp;end  
end

Is the exact same thing as

class Foo  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts "in foo!"  
&nbsp;&nbsp;end  
end

class Bar  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts "in bar's foo"  
&nbsp;&nbsp;end  
end

in fact...

class Baz  
end

Baz.new.foo # =\> NoMethodError

class Baz \< Abstract  
end

Baz.new.foo # =\> NotImplementedError

Almost the exact same thing. Just a different exception.

---

<div class="post-metadata">

**Author:** ![11142](https://avatars.discourse-cdn.com/v4/letter/1/e0b2c6/32.png) [@11142](https://rubytalk.org/u/11142)\
**Post date:** [5 April 2012 21:37 UTC](https://rubytalk.org/t/design-pattern-question/65206/13 "2012-04-05T21:37:54Z")

</div>

Sequel provides a lot of helper methods and aliases in the abstract  
class. Just look thru the docs of Sequel::Database (this is the  
abstract class) and it's list of methods - you only need to implement  
a handful, the rest are helpers.

-- Matma Rex

---

<div class="post-metadata">

**Author:** ![Steve\_Klabnik](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@Steve\_Klabnik](https://rubytalk.org/u/Steve_Klabnik)\
**Post date:** [5 April 2012 22:05 UTC](https://rubytalk.org/t/design-pattern-question/65206/14 "2012-04-05T22:05:55Z")

</div>

Insert a 'def bar; puts "bar"; end' into my Abstract class, the end  
point is still the same. Adding an empty method that throws a slightly  
different error doesn't really do anything worthwhile in my opinion.  
If it makes you happy, then do it.

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [6 April 2012 10:50 UTC](https://rubytalk.org/t/design-pattern-question/65206/15 "2012-04-06T10:50:21Z")

</div>

> Adding an empty method that throws a slightly  
> different error doesn't really do anything worthwhile in my opinion.

Adding an empty method that throws a slightly  
different error doesn't really do anything worthwhile in my opinion.

Because being explicit about the interactions across classes isn't worthwhile? Having an explicit place to document the contract isn't worthwhile? Sorry... I'm not buying it. From your example [with my edits]:

> class Abstract  
> [# contract doco]  
> def foo  
> &nbsp;&nbsp;&nbsp;[raise] NotImplemented[Error][, "helpful explanation--much more so than NoMethodError"]  
> end  
> end

Now I know where I can document the foo contract and suggest proper ways to implement it. You lose that when you throw that out the window and go with duck typing.

That reminds me... How do you suggest a beginner know the difference between a typo and an unimplemented ducktyped method interface?

> **···**
>
> On Apr 5, 2012, at 15:05 , Steve Klabnik wrote:  
> On Apr 5, 2012, at 15:05 , Steve Klabnik wrote:
