forked from jimweirich/presentation_solid_ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added thermostat and builder examples.
- Loading branch information
1 parent
c86dc96
commit f14064c
Showing
13 changed files
with
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'rubygems' | ||
require 'builder/xmlmarkup' | ||
|
||
sb = Builder::XmlMarkup.new(:target => "") | ||
sb.person { sb.name("Jim") } | ||
puts sb.target! | ||
|
||
|
||
fb = Builder::XmlMarkup.new(:target => STDOUT) | ||
fb.person { fb.name("Bob") } | ||
puts |
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,7 @@ | ||
class App { | ||
public static void main(String[] args) { | ||
Furnace f = new Furnace(); | ||
Thermostat t = new Thermostat(f); | ||
t.run(); | ||
} | ||
} |
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,11 @@ | ||
class Furnace { | ||
public Furnace() { | ||
off(); | ||
} | ||
public void on() { | ||
System.out.println("Furnace is on"); | ||
} | ||
public void off() { | ||
System.out.println("Furnace is off"); | ||
} | ||
} |
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,19 @@ | ||
require 'rake/clean' | ||
|
||
PROG = 'App' | ||
JAVA_FILES = FileList['*.java'] | ||
CLASS_FILES = JAVA_FILES.ext(".class") | ||
|
||
CLEAN.include("*.class") | ||
|
||
task :default => :prog | ||
|
||
task :prog => CLASS_FILES | ||
|
||
task :run => :prog do | ||
sh "java #{PROG}" | ||
end | ||
|
||
rule '.class' => '.java' do |r| | ||
sh "javac #{r.source}" | ||
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,15 @@ | ||
class Thermostat { | ||
public Thermostat(Furnace f) { | ||
this.furnace = f; | ||
} | ||
public void run() { | ||
if (should_be_on()) { | ||
this.furnace.on(); | ||
} else { | ||
this.furnace.off(); | ||
} | ||
} | ||
|
||
private Furnace furnace; | ||
private boolean should_be_on() { return true; } | ||
} |
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,7 @@ | ||
class App { | ||
public static void main(String[] args) { | ||
Furnace f = new Furnace(); | ||
Thermostat t = new Thermostat(f); | ||
t.run(); | ||
} | ||
} |
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,11 @@ | ||
class Furnace implements OnOffDevice { | ||
public Furnace() { | ||
off(); | ||
} | ||
public void on() { | ||
System.out.println("Furnace is on"); | ||
} | ||
public void off() { | ||
System.out.println("Furnace is off"); | ||
} | ||
} |
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,4 @@ | ||
interface OnOffDevice { | ||
public void on(); | ||
public void off(); | ||
} |
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,19 @@ | ||
require 'rake/clean' | ||
|
||
PROG = 'App' | ||
JAVA_FILES = FileList['*.java'] | ||
CLASS_FILES = JAVA_FILES.ext(".class") | ||
|
||
CLEAN.include("*.class") | ||
|
||
task :default => :prog | ||
|
||
task :prog => CLASS_FILES | ||
|
||
task :run => :prog do | ||
sh "java #{PROG}" | ||
end | ||
|
||
rule '.class' => '.java' do |r| | ||
sh "javac #{r.source}" | ||
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,15 @@ | ||
class Thermostat { | ||
public Thermostat(OnOffDevice f) { | ||
this.furnace = f; | ||
} | ||
public void run() { | ||
if (should_be_on()) { | ||
this.furnace.on(); | ||
} else { | ||
this.furnace.off(); | ||
} | ||
} | ||
|
||
private OnOffDevice furnace; | ||
private boolean should_be_on() { return true; } | ||
} |
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,5 @@ | ||
require 'furnace' | ||
require 'thermostat' | ||
|
||
t = Thermostat.new(Furnace.new) | ||
t.run |
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,12 @@ | ||
class Furnace | ||
def initialize | ||
off | ||
end | ||
def on | ||
puts "Furnace is on" | ||
end | ||
def off | ||
puts "Furnace is off" | ||
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,15 @@ | ||
class Thermostat | ||
def initialize(furnace) | ||
@furnace = furnace | ||
end | ||
def run | ||
if should_be_on? | ||
@furnace.on | ||
else | ||
@furnace.off | ||
end | ||
end | ||
def should_be_on? | ||
true | ||
end | ||
end |