forked from design-patterns-in-ruby/design-patterns-in-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added notes and code for the Singleton design pattern.
- Loading branch information
1 parent
e111e73
commit 05a0858
Showing
10 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Logger | ||
attr_reader :log | ||
|
||
def initialize | ||
@log = File.open('log.txt', 'a') | ||
end | ||
|
||
@@instance = self.new | ||
|
||
def self.instance | ||
return @@instance | ||
end | ||
|
||
# def log(msg) | ||
# @log.puts(msg) | ||
# end | ||
|
||
private_class_method :new | ||
end | ||
|
||
Logger.instance.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'singleton' | ||
|
||
class SimpleLogger | ||
include Singleton | ||
|
||
attr_accessor :level | ||
|
||
ERROR = 1 | ||
WARNING = 2 | ||
INFO = 3 | ||
|
||
def initialize | ||
@log = File.open('log.txt', 'w') | ||
@level = WARNING | ||
end | ||
|
||
## the Singleton module overrides the following code | ||
# @@instance = self.new | ||
# | ||
# def self.instance | ||
# @@instance | ||
# end | ||
# | ||
# private_class_method :new | ||
|
||
def error(msg) | ||
@log.puts(msg) | ||
@log.flush | ||
end | ||
|
||
def warning(msg) | ||
@log.puts(msg) if @level >= WARNING | ||
@log.flush | ||
end | ||
|
||
def info(msg) | ||
@log.puts(msg) if @level >= INFO | ||
@log.flush | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class ClassBasedLogger | ||
ERROR = 1 | ||
WARNING = 2 | ||
INFO = 3 | ||
|
||
@@level = WARNING | ||
|
||
# Lazily instantiate @@log | ||
def self.log | ||
@@log ||= File.open('log.txt', 'w') | ||
end | ||
|
||
def self.error(msg) | ||
log.puts("Error: #{msg}") | ||
log.flush | ||
end | ||
|
||
def self.warning(msg) | ||
log.puts("Warning: #{msg}") if @@level >= WARNING | ||
log.flush | ||
end | ||
|
||
def self.info(msg) | ||
log.puts("Info: #{msg}") if @@level >= INFO | ||
log.flush | ||
end | ||
|
||
def self.level=(new_level) | ||
@@level = new_level | ||
end | ||
|
||
def self.level | ||
@@level | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Info: Computer wins chess game. | ||
Warning: AE-35 hardware failure predicted. | ||
Error: HAL-9000 malfunction, take emergency action! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require './ClassBasedLogger' | ||
|
||
ClassBasedLogger.level = ClassBasedLogger::INFO | ||
ClassBasedLogger.info('Computer wins chess game.') | ||
ClassBasedLogger.warning('AE-35 hardware failure predicted.') | ||
ClassBasedLogger.error('HAL-9000 malfunction, take emergency action!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require './SimpleLogger' | ||
|
||
logger = SimpleLogger.instance | ||
puts logger.level | ||
|
||
SimpleLogger.instance.info('Computer wins chess game.') | ||
SimpleLogger.instance.warning('AE-35 hardware failure predicted.') | ||
SimpleLogger.instance.error('HAL-9000 malfunction, take emergency action!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module ModuleBasedLogger | ||
ERROR = 1 | ||
WARNING = 2 | ||
INFO = 3 | ||
|
||
@@level = WARNING | ||
|
||
# Lazily instantiate @@log | ||
def self.log | ||
@@log ||= File.open('log.txt', 'w') | ||
end | ||
|
||
def self.error(msg) | ||
log.puts("Error: #{msg}") | ||
log.flush | ||
end | ||
|
||
def self.warning(msg) | ||
log.puts("Warning: #{msg}") if @@level >= WARNING | ||
log.flush | ||
end | ||
|
||
def self.info(msg) | ||
log.puts("Info: #{msg}") if @@level >= INFO | ||
log.flush | ||
end | ||
|
||
def self.level=(new_level) | ||
@@level = new_level | ||
end | ||
|
||
def self.level | ||
@@level | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Info: Computer wins chess game. | ||
Warning: AE-35 hardware failure predicted. | ||
Error: HAL-9000 malfunction, take emergency action! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require './ModuleBasedLogger' | ||
|
||
ModuleBasedLogger.level = ModuleBasedLogger::INFO | ||
ModuleBasedLogger.info('Computer wins chess game.') | ||
ModuleBasedLogger.warning('AE-35 hardware failure predicted.') | ||
ModuleBasedLogger.error('HAL-9000 malfunction, take emergency action!') |