Skip to content

Instantly share code, notes, and snippets.

@samnang
Created October 25, 2010 15:34
Show Gist options
  • Save samnang/645139 to your computer and use it in GitHub Desktop.
Save samnang/645139 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment