samnang
10/25/2010 - 3:34 PM

Implement Abstract Factory

Implement Abstract Factory

class MySqlDatabaseFactory
  class << self
    def create_connection
      MySqlConnection.new
    end
    
    def create_generator
      MySqlGenerator.new
    end
  end
end

class MySqlConnection
  def initialize
    puts "Setup mysql connection"
  end
  
  def open_connection
    puts "Open mysql connection"
  end
end

class MySqlGenerator  
  def create_table
    puts "Create mysql table"
  end
  
  #...
end
def load_adapter_from_config_file
  MySqlDatabaseFactory  #returning stub
end

adapter = load_adapter_from_config_file
adapter.create_connection.open_connection
adapter.create_generator.create_table
class SqliteDatabaseFactory
  class << self
    def create_connection
      SqliteConnection.new
    end
    
    def create_generator
      SqliteGenerator.new
    end
  end
end

class SqliteConnection
  def initialize
    puts "Setup sqlite connection"
  end
  
  def open_connection
    puts "Open sqlite connection"
  end
end

class SqliteGenerator  
  def create_table
    puts "Create sqlite table"
  end
  
  #...
end