From: Robert Klemme Date: 2011-04-05T22:39:33+09:00 Subject: Re: Seeking advice On Tue, Apr 5, 2011 at 3:22 PM, Alex Rothbard wrote: > Hi. I am writing a library that will have the ability to access a server > by many different "transports" (JSON over HTTP, Thrift, etc). Depending > on which transport the user chooses, different files and classes will > need to be loaded. > > A complete implementation of a transport requires that three classes > (such as the 'Grid' class below) be implemented from their respective > base class. Additionally, if the user wants to use JSON over HTTP, they > shouldn't need to depend on the thrift gem and vice versa. These > transport classes are used by other classes within the library, and > should never be used directly by the user. > > What is the best way to allow the user of the library to specify a > transport at runtime? Could I wrap all my implemented transport classes > in a module and do something like this?: > > def initialize(transport) >  @t = transport >  @grid = @t::Grid.new > end > > where "transport" could be "MyProject::Transport::Thrift". > > Is this a good idea? Is there a better way? I'd go for factory pattern. def initialize(transport) @t = transport @grid = @t.grid @grid = @t.grid.new # alternative end All transports must implement the particular set of methods of course. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/