Skip to content

Commit

Permalink
added thermostat and builder examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimweirich committed Aug 6, 2009
1 parent c86dc96 commit f14064c
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/builder/builder.rb
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
7 changes: 7 additions & 0 deletions src/thermostat/App.java
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();
}
}
11 changes: 11 additions & 0 deletions src/thermostat/Furnace.java
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");
}
}
19 changes: 19 additions & 0 deletions src/thermostat/Rakefile
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
15 changes: 15 additions & 0 deletions src/thermostat/Thermostat.java
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; }
}
7 changes: 7 additions & 0 deletions src/thermostat_dip/App.java
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();
}
}
11 changes: 11 additions & 0 deletions src/thermostat_dip/Furnace.java
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");
}
}
4 changes: 4 additions & 0 deletions src/thermostat_dip/OnOffDevice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface OnOffDevice {
public void on();
public void off();
}
19 changes: 19 additions & 0 deletions src/thermostat_dip/Rakefile
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
15 changes: 15 additions & 0 deletions src/thermostat_dip/Thermostat.java
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; }
}
5 changes: 5 additions & 0 deletions src/thermostat_ruby/app.rb
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
12 changes: 12 additions & 0 deletions src/thermostat_ruby/furnace.rb
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

15 changes: 15 additions & 0 deletions src/thermostat_ruby/thermostat.rb
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

0 comments on commit f14064c

Please sign in to comment.