Object::Simple

简介

Object::Simple 是一个非常简单的从Mojo::Base移植的类构建器,使用它可以很容易的在Perl语言中使用面向对象的方式进行编程。同时它还具有以下特点:

  1. 使用方便,简单易学。使用Object::Simple模块,你只需要学习has函数的使用就可以了。
  2. 与 Mojo::Base 兼容。因为它是从Mojo::Base模块移植过来的,如果你是喜欢Mojolicious的面向对象的编码方式,使用Object::Simple是一个不错的选择。
  3. 速度快,占用内存少。可以快速的new,并且是基于hash的对象属性创建,所以在节约内存方面更优。
package SomeClass;
use Object::Simple -base;

# Create accessor
has 'foo';

# Create accessor with default value
has foo => 1;
has foo => sub { [] };
has foo => sub { {} };
has foo => sub { OtherClass->new };

# Create accessors at once
has ['foo', 'bar', 'baz'];
has ['foo', 'bar', 'baz'] => 0;

创建对象的代码
# Create a new object
my $obj = SomeClass->new;
my $obj = SomeClass->new(foo => 1, bar => 2);
my $obj = SomeClass->new({foo => 1, bar => 2});

# Set and get value
my $foo = $obj->foo;
$obj->foo(1);

# Setter can be chained
$obj->foo(1)->bar(2);

继承
package Foo;
use Object::Simple -base;

# Bar inherit Foo
package Bar;
use Object::Simple 'Foo';

使用说明

创建类和访问器

下面是创建一个类SomeClass的代码。

package SomeClass;
use Object::Simple -base;

通过使用 -base 选项,SomeClass继承了 Object::Simple并且向SomeClass中导入了has方法。
Object::Simple模块有一个名为new的构造函数。这个构造函数可以接收散列或散列引用,它会用接收到的参数创建一个基本hash结构的对象。

my $obj = SomeClass->new;
my $obj = SomeClass->new(foo => 1, bar => 2);
my $obj = SomeClass->new({foo => 1, bar => 2});

下面一个段创建访问器的代码,它创建了一个名为 foo 的访问器。

has 'foo';

可以在对象上使用访问器获取或设置对象属性的值,如下面的代码段所示:

# Set value
$obj->foo(1);

# Get value
my $foo = $obj->foo;

并且 setter 操作可以链式进行:

$obj->foo(1)->bar(2);

在创建访问器时还可以指定默认值,如果在访问器被调用时还没有对其设置值,则会返回默认值。

has foo=>1;

my $foo_default = $obj->foo;

如果要使用引用或对象作为默认值,则默认值必须由一个函数返回。返回值成为默认值。

has foo => sub { [] };
has foo => sub { {} };
has foo => sub { SomeClass->new };

您还可以一次创建多个访问器:

has ['foo', 'bar', 'baz'];
has ['foo', 'bar', 'baz'] => 0;

覆盖方法

在使用Object::Simple时,构造方法new是可以被覆盖的。

sub new {
  my $self = shift->SUPER::new(@_);

  # Initialization

  return $self;
}

下面是一个 更改 构造函数参数类型的例子:

sub new {
  my $self = shift;

  $self->SUPER::new(x => $_[0], y => $_[1]);

  return $self;
}

进行上面的new构造参数覆盖之后,就可以像下面这样使用new函数了。

my $point = Point->new(4, 5);

示例

Point类有两个访问器x、y;它还有一个方法clear用来清空x、y的值(都设置为0)。

package Point;
use Object::Simple -base;

has x => 0;
has y => 0;

sub clear {
  my $self = shift;

  $self->x(0);
  $self->y(0);
}

使用Point类的方式如下:

use Point;
my $point = Point->new(x => 3, y => 5);
print $point->x;
$point->y(9);
$point->clear;

Point3D类继承了Point类,但它除了有x、y访问器外还有一个z访问器。且clear方法可以清除x、y、z的值(设置为0)。

package Point3D;
use Object::Simple 'Point';

has z => 0;

sub clear {
  my $self = shift;

  $self->SUPER::clear;

  $self->z(0);
}

使用Point3D类的代码如下:

use Point3D;
my $point = Point->new(x => 3, y => 5, z => 8);
print $point->z;
$point->z(9);
$point->clear;

函数

Object::Simple会向所有引入它的类中导入 has 函数。has函数是一个用于创建访问器的函数。

has 'foo';
has ['foo', 'bar', 'baz'];
has foo => 1;
has foo => sub { {} };

has ['foo', 'bar', 'baz'];
has ['foo', 'bar', 'baz'] => 0;

has函数接收访问者名称和默认值。默认值是可选的。如果要同时创建多个访问器,请在第一个参数处使用数组引用指定访问器的名称。如果要将引用或对象指定为默认值,则必须使用代码引用,代码的返回值作为访问器的默认值。

在指定了默认值时,如果在获取访问器的值之前没有对访问器的值进行设置,则返回默认值。在使用访问器设置值时返回的是调用访问器的对象,所以可以使用链式操作。

方法

Object::Simple对象中有两个方法,它的子类也会继承这两个方法。下面分别进行介绍。

new

my $obj = Object::Simple->new;
my $obj = Object::Simple->new(foo => 1, bar => 2);
my $obj = Object::Simple->new({foo => 1, bar => 2});

创建一个新对象,new接收散列或散列作为参数,并使用参数创建一个基于hashref的对象。

attr

__PACKAGE__->attr('foo');
__PACKAGE__->attr(['foo', 'bar', 'baz']);
__PACKAGE__->attr(foo => 1);
__PACKAGE__->attr(foo => sub { {} });

__PACKAGE__->attr(['foo', 'bar', 'baz']);
__PACKAGE__->attr(['foo', 'bar', 'baz'] => 0);

用于创建访问器的方法,attr方法与has的功能完全一样,只是has工作在类中是一个函数,而attr工作在对象中是一个方法。

引入Object::Simple时的可用选项

通过使用 -base 选项,目标类将继承Object::Simple类,并向目标类中导入has方法。

package Foo;
use Object::Simple -base;

has x => 1;
has y => 2;

如果目标类需要继承一个其他的类,可以使用下在的写法:

# Bar inherit Foo
package Bar;
use Object::Simple 'Foo';

你还可以使用如下语法,这与上面的代码段的功能没什么不同。

# Same as above
package Bar;
use Object::Simple -base => 'Foo';

你还可以在Object::Simple的子类中使用下面的语法。这与上面的代码段的功能没什么不同。

# Same as above
package Bar;
use Foo -base;

无论使用什么样的方法引入Object::Simple模块, strict 和 warning 模式都将自动被启用。

a

你可能感兴趣的:(Object::Simple)