Encapsulation Examples
The scenario for these examples is the same as that in the encapsulation article. We have a room to clean and we want to know when it is finished so we can inspect it.
Although I will be giving examples in several different languages I am not planning to get into the syntax of the languages themselves. I am hoping that the examples will be simple enough that even a novice will be able to understand what is going on. I will include explanations in the form of comments which I hope will be sufficient.
As before, here is an example with out encapsulation.
C#
using System; public class Program { public void Main() { // Console is used to write to and read from the Command or Dos prompt // WriteLine is used to print text to the command prompt Console.WriteLine("Take all toys off your bed"); Console.WriteLine("Straighten your bed sheets"); Console.WriteLine("Put all your toys in your toy box"); Console.WriteLine("Pick up all the trash"); Console.WriteLine("Put the trash in your trashcan"); // ReadKey simply pauses the program long enough for us to view the result Console.ReadKey(); } }
VB.Net
Module Module1 Sub Main() ' Console is used to write to and read from the Command or Dos prompt ' WriteLine is used to print text to the command prompt Console.WriteLine("Take all toys off your bed") Console.WriteLine("Straighten your bed sheets") Console.WriteLine("Put all your toys in your toy box") Console.WriteLine("Pick up all the trash") Console.WriteLine("Put the trash in your trashcan") ' ReadKey simply pauses the program long enough for us to view the result Console.ReadKey() End Sub End Module
PHP
<?php # Print or echo is used to print text to the webpage # \n is used to put a new line in the text echo("Take all toys off your bed"); echo("Straighten your bed sheets"); echo("Put all your toys in your toy box"); echo("Pick up all the trash"); echo("Put the trash in your trashcan"); ?>
Ruby
# Print or echo is used to print text to the webpage # \n is used to put a new line in the text print "Take all toys off your bed\n" print "Straighten your bed sheets\n" print "Put all your toys in your toy box\n" print "Pick up all the trash\n" print "Put the trash in your trashcan\n"
And now with encapsulation
C#
using System; public class Program { public void Main() { CleanYourRoom(); } public void CleanYourRoom() { // Console is used to write to and read from the Command or Dos prompt // WriteLine is used to print text to the command prompt Console.WriteLine("Take all toys off your bed"); Console.WriteLine("Straighten your bed sheets"); Console.WriteLine("Put all your toys in your toy box"); Console.WriteLine("Pick up all the trash"); Console.WriteLine("Put the trash in your trashcan"); // ReadKey simply pauses the program long enough for us to view the result Console.ReadKey(); } }
VB.Net
Module Module1 Sub Main() CleanYourRoom() End Sub Sub CleanYourRoom() ' Console is used to write to and read from the Command or Dos prompt ' WriteLine is used to print text to the command prompt Console.WriteLine("Take all toys off your bed") Console.WriteLine("Straighten your bed sheets") Console.WriteLine("Put all your toys in your toy box") Console.WriteLine("Pick up all the trash") Console.WriteLine("Put the trash in your trashcan") ' ReadKey simply pauses the program long enough for us to view the result Console.ReadKey() End Sub End Module
PHP
<?php CleanYourRoom(); function CleanYourRoom() { # Print or echo is used to print text to the webpage # \n is used to put a new line in the text Print("Take all toys off your bed\n"); Print("Straighten your bed sheets\n"); echo("Put all your toys in your toy box\n"); echo("Pick up all the trash\n"); echo("Put the trash in your trashcan\n"); } ?>
Ruby
def cleanYourRoom() do # Print is used to print text to the webpage # \n is used to put a new line in the text print "Take all toys off your bed\n" print "Straighten your bed sheets\n" print "Put all your toys in your toy box\n" print "Pick up all the trash\n" print "Put the trash in your trashcan\n" end cleanYourRoom
Just as in the main encapsulation article the details of cleaning your room have been encapsulated and moved to somewhere else. In this example the details are just below but they do not have to be. The details could be in another file or even on the internet somewhere. The important thing here is that we can now call the CleanYourRoom method again if we need to and we will get the same results. Also, we could change the order of things in the CleanYourRoom method and it doesn’t really matter as long as the work is getting done. In the case of my children, I often tell them to clean their room and then see that as an encapsulation of play some more and throw your toys everywhere.