Basic Sequence objects - object oriented programming concepts

In Bioinformatics, nucleotide or protein sequence parsing and processing is a common initial step performed in most of the analysis. In this tutorial, I will explain
  1. How a simple Sequence Class can be created and used in programming language like Java, Perl and Python ?
  2. How sequence objects can be easily extended to include more functionalities ?
  3. How you can use already available sequence objects in BioJava, BioPerl and BioPython instead of creating your own classes.
Thus this tutorial could help in understanding the basic concepts of programming using sequence objects as example.

Perl

A. Using Data structure and procedural programming approach :

Let's call the program 'seq_hash.pl'

#!/usr/bin/env perl
use strict;
use warnings;

# Anonymous Hash reference in Perl used for storing the Sequence content
my $sequence_hash = {
  "name"       => "cytochrome",
  "type"       => "DNA",
  "seq_string" => "ADSFASDFASDF"
};

# A function/subroutine to get the sequence hash reference contents as FASTA string
sub seq_hash_to_fasta {
   my ($seq_hash) = @_;
   my $fasta_out  = ">$seq_hash->{'name'}\n$seq_hash->{'seq_string'}\n";
   return $fasta_out;
}

my $fasta_string = &seq_hash_to_fasta($sequence_hash);

print "FASTA string of Sequence:\n";
print $fasta_string;


Running the program from command-prompt
Prompt$$ perl seq_hash.pl
FASTA string of Sequence:
>cytochrome
ADSFASDFASF


B. Using Object-Oriented Concept - Perl Module:

use strict;
use warnings;

package SimpleSequence;


# Function: Constructor for the class and return the SimpleSequence Object
# Usage   : $seq = new SimpleSequence('seq Name')
# Returns : A SimpleSequence object. A quick synopsis:
#           $seq_object->name() - name of the sequence
#           $seq_object->sequence() - sequence as a string
#
sub new {
        my ($class,$name) = @_;
        my $data = {
                "name"     => "$name",
                "sequence" => "",
        };
        bless $data, $class;
        return $data;
}

# Get or Set Sequence name
# Usage   : $seq_obj->name('gi:145443344') - sets the name of sequence
#         : $name= $seq_obj->name() - gets the name of sequence
sub name {
        my ($self, $arg) = @_;
        if (defined $arg) {
                $self->{"name"} = $arg;
        }else {
                return $self->{"name"};
        }
}

# Get or Set sequence string
# Usage   : $seq_obj->sequence('ATAGAFAF') - sets the sequence
#         : $seq_str = $seq_obj->sequence() - gets the sequence as string
sub sequence {
        my ($self, $arg) = @_;
        if (defined $arg) {
                $self->{"sequence"} = $arg;
        }else {
                return $self->{"sequence"};
        }
}

# Get FASTA string
# Usage   : $fasta_str = $seq_obj->to_fasta()
sub to_fasta {
     my ($self, $arg) = @_;
     my $name = $self->{"name"};
     my $seq_str = $self->{"sequence"};
     my $fasta_out  = ">$name\n$seq_str\n";
     return $fasta_out;

}