1. Perl
  2. Object Oriented
  3. Behavior according to the environment

Create an object according to the environment

Let's modify the class a little and change the behavior according to the OS.

1. Polymorphism that changes its behavior according to the environment

If the environment is Windows, I created an object of FilePath::Windows class, and if the environment is not, I wrote a class that creates an object of FilaPath::Unix class.

use strict;
use warnings;

my $file_path = FilePath->new;

my $result = $file_path->cat_path('a', 'b');

print "Result: $result\n";

# Class used by user
package FilePath;

sub new {
  my $self = shift;
  if ($^O eq 'MSWin32') {
    return FilePath::Windows->new;
  }
  else {
    return FilePath::Unix->new;
  }
}

# Base class for handling file paths
package FilePath::Base;

sub new {
  my $proto = shift;
  my $class = ref $proto || $proto;
  my $self = {};
  bless $self, $class;
  return $self;
}

sub cat_path {
  my ($self, $str1, $str2) = @_;
  return $str1. $Self->delimiter. $Str2;
}

# For Unix
package FilePath::Unix;
use base 'FilePath::Base';

# Unix delimiter
sub delimiter {return'/'}


# For Windows
package FilePath::Windows;
use base 'FilePath::Base';

# Delimiter for Windows
sub delimiter {return'\\'}

The execution result is when the environment is Windows.

Result: a\b

Otherwise,

Result: a/b

It will be. If the environment is Windows, an object of the FilePath::Windows class is created, otherwise an object of the FilePath::Unix class is created.

print ref $file_path;

Then you can see which class of object is created.

2. Change the behavior with the constructor

We have prepared a class called FilePath as a class for users to use. The constructor of this class will create an object according to the environment. (Generally, it's called a factory class.)

package FilePath;

sub new {
  my $self = shift;
  if ($^O eq 'MSWin32') {
    return FilePath::Windows->new;
  }
  else {
    return FilePath::Unix->new;
  }
}

I'm looking at $^O to find out what type of OS I'm using and changing the objects I create. The constructor in this example takes no arguments, but since constructors generally take arguments.

sub new {
  my $self = shift;
  if ($^O eq 'MSWin32') {
    return FilePath::Windows->new(@_);
  }
  else {
    return FilePath::Unix->new(@_);
  }
}

It is more often described as. Writing using the ternary operator makes it even cleaner.

sub new {
  my $self = shift;
  return $^O eq 'MSWin32'? FilePath::Windows->new(@_):
                            FilePath::Unix->new(@_);
}

3. Convenient part of inheritance

If you use inheritance well, you will find that you can clearly write a description that makes it behave according to the environment.

Also, if you want to support other environments, just create a class called "FilePath::Honyara" and modify the constructor of the FilePath class a little.

In other words, when it comes to situations where inheritance is effective, there are many patterns and there is a high possibility of expansion. Twice

4. If you find it difficult to inherit

Follow this example with a debugger. You can understand it by observing how it moves around between classes.

You can emulate Windows and Unix by changing the value of $^O. Also, use the ref function to see the class to which the created object belongs.

Related Informatrion