Coupling Basics : Concrete Class vs Abstract Messages
To learn about depending on abstract messages instead of concrete classes to create re-usable code.
while line = gets puts line end
Here is a sample run:
Hi Hi Hello Hello
Ctrl+D to quit the program. This reads from keyboard and writes to the console.
Create a readme.txt file:
This is the first line This is the second line
Now run the program as follows:
$ruby copy.rb readme.txt
This reads the readme.txt from the file and writes it to the console.
File.open('./readme.txt', 'r') do |file| while line = file.gets puts line end end
This reads the readme.txt and writes it to the console.
Create abstract.rb with:
require 'stringio' ip = StringIO.new('This is a test') op = StringIO.new('', 'w') ip.each_line do |line| op.puts line end print op.string
This uses StringIO a fake file system to read and write. This is good for testing. The dependency is on the message:
each_line and puts and not on any concrete class. If there is a name of the class in the code, it creates tight coupling in the code and makes it difficult to reuse. As long as the ip and op variables can respond to the each_line and puts messages. This program will work.
Depend on messages that capture abstractions which can be varied by having different implementations.
In this article, we saw examples for:
Standard In --> Standard Out Keyboard --> Console File --> Console Fake File --> Fake Console
You learned that depending on a concrete class creates tight coupling and depending on messages that can be implemented by different implementations in different class can lead to re-usable code.