Skip to content

Instantly share code, notes, and snippets.

@bellthoven
Created March 29, 2009 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bellthoven/87484 to your computer and use it in GitHub Desktop.
Save bellthoven/87484 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
{
package CineFacade;
sub new {
my $this = {};
my $class = shift;
$class = ref($class) || $class;
$this->{amplifier} = shift || undef;
$this->{dvd} = shift || undef;
$this->{projector} = shift || undef;
$this->{lights} = shift || undef;
$this->{popcornMachine} = shift || undef;
bless ($this, $class);
return $this;
}
sub watchMovie {
my $this = shift;
$movie = shift;
# Popcorn machine
$this->{popcornMachine}->on();
$this->{popcornMachine}->pop();
# Home lights
$this->{lights}->off();
# Projector
$this->{projector}->on();
$this->{projector}->videoChannel();
# Amplifier
$this->{amplifier}->on();
$this->{amplifier}->setVolume(100);
# DVD
$this->{dvd}->on();
$this->{dvd}->play($movie);
}
sub endMovie {
my $this = shift;
$this->{popcornMachine}->off();
$this->{lights}->on();
$this->{projector}->off();
$this->{amplifier}->off();
$this->{dvd}->stop();
$this->{dvd}->eject();
$this->{dvd}->off();
}
}
{
package Amplifier;
sub new {
my $class = shift;
$class = ref($class) || $class;
my $this = {};
bless($this, $class);
return $this;
}
sub on {
my $this = shift;
print "Turning the Amplifier [ON]\n";
}
sub off {
my $this = shift;
print "Turning the Amplifier [OFF]\n";
}
sub setVolume {
my $this = shift;
$volume = shift;
print printf "Turning the Amplifiers volume to %d%%\n", $volume
}
}
{
package Dvd;
sub new {
my $class = shift;
$class = ref($class) || $class;
my $this = {};
$this->{state} = 'stop';
$this->{movie} = undef;
bless($this, $class);
return $this;
}
sub on {
my $this = shift;
print "Turning the DVD Player [ON]\n";
}
sub off {
my $this = shift;
print "Turning the DVD Player [OFF]\n";
}
sub play {
my $this = shift;
$movie = shift;
if ($this->{state} eq 'play') {
$this->{stop}();
$this->{play}($movie);
} else {
$this->{movie} = $movie;
$this->{state} = 'play';
print printf "Playing the movie %s\n", $movie
}
}
sub stop {
my $this = shift;
if ($this->{state} eq 'play') {
$this->{state} = 'stop';
print printf "Stoping the movie %s\n", $this->{movie};
$this->{movie} = undef;
}
}
sub eject {
my $this = shift;
if ($this->{state} eq 'play') {
$this->{stop}();
}
print "Ejecting the DVD\n";
}
}
{
package Projector;
sub new {
my $class = shift;
$class = ref($class) || $class;
my $this = {};
bless($this, $class);
return $this;
}
sub videoChannel {
my $this = shift;
print "Setting Video Channel\n";
}
sub on {
my $this = shift;
print "Turning the Project [ON]\n";
}
sub off {
my $this = shift;
print "Turning the Project [OFF]\n";
}
}
{
package Lights;
sub new {
my $class = shift;
$class = ref($class) || $class;
my $this = {};
$this->{state} = 'off';
bless($this, $class);
return $this;
}
sub on {
my $this = shift;
if ($this->{state} eq 'off') {
print "Turning the Lights [ON]\n";
$this->{state} = 'on';
}
}
sub off {
my $this = shift;
if ($this->{state} eq 'on') {
$this->{state} = 'off';
print "Turning the Lights [OFF]\n";
}
}
}
{
package PopcornMachine;
sub new {
my $this = {};
my $class = shift;
$class = ref($class) || $class;
$this->{state} = 'off';
bless($this, $class);
return $this;
}
sub on {
my $this = shift;
if ($this->{state} eq 'off') {
$this->{state} = 'on';
print "Turning the Popcorn Machine [ON]\n";
}
}
sub pop {
my $this = shift;
if ($this->{state} eq 'on') {
print "Poping the corns ;P\n";
}
}
sub off {
my $this = shift;
if ($this->{state} eq 'on') {
$this->{state} = 'off';
print "Turning the Popcorn Machine [OFF]\n";
}
}
}
{
$cine = CineFacade->new(
Amplifier->new(),
Dvd->new(),
Projector->new(),
Lights->new(),
PopcornMachine->new()
);
print "Movie Session started: \n\n";
$cine->watchMovie('Titanic');
print "Movie Session ended: \n\n";
$cine->endMovie();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment