3

这是经典的对象模型:

 class ViewBase
 {
  void DoSomethingForView() { } //May be virtual
 }

 class View1 : ViewBase //(derived class from ViewBase)
 {
    void DoSomethingForView() { }
    void DoSomethingForView1Special() { }
 }

 class View2: ViewBase //(another derived class from ViewBase)
 {
    void DoSomethingForView2Special() { }
 }

 class Application
 {
    void Print() { }
    void DoSomething() { }

     //Do some magic to create a view object (View1 or View2) and return

    //Something which I don't know to describe. Its like dynamically 
    //returning object of View1 or View2 at runtime
  }

我想将其转换为 Perl Moose 类模型。

以便,

我将调用视图方法,例如

void Main()
{

  App = new Application();

  App->View1->DoSomethingForView(); 
  App->View1->DoSomethingForView1Special();
  App->View2->DoSomethingForView(); 
  App->View2->DoSomethingForView2Special();

}

我不知道要调用哪个视图。但在运行时,必须创建 View1/View2 实例并调用 DoSomethingForView()。

上面的代码并不完全是 Perl。如何在 Perl 中翻译和实现这一点。

Application 对象应该有 View 对象,但我们在编译时不知道视图的类型。我们有一个测试应用程序,在 Perl 中开发。

您可以想象 Application 是一个 GUI 应用程序,而 View 是您在应用程序窗口中看到的内容。用户可以选择任何视图。

我对我的英语感到抱歉。如果我需要提供更多文字,请告诉我。

4

2 回答 2

3

因此,这为您提供了 Perl 中大致等效的语法。但是,它并不能帮助您解决一些相互矛盾的标准。

请注意以下事项:

  • class ABC {...} 被 perl 语法包 ABC 替代;
  • 大写的驼峰式方法名称被转换为正确的“蛇形”perl。
  • App 已转为 sigil-ed $App

这已经针对 Moose v62、Perl 5.10 进行了测试。最后一行的第二个将失败,因为do_something_for_view不是由View2类实现的。正如您所调用的view1view2具体而言,我没有看到您似乎表示的应用程序是多态性。

package ViewBase;

sub do_something_for_view { 
    Carp::croak "ViewBase::do_something_for_view is ABSTRACT!"; 
}

package View1;
use Moose;
extends 'ViewBase';

sub do_something_for_view { print "Doing something for View1.\n"; }
sub do_something_for_view1_special { print "Doing something SPECIAL for View1.\n"; }

package View2;
use Moose;
extends 'ViewBase';

sub do_something_for_view2_special() { print "Doing something SPECIAL for View2.\n"; }

package Application;
use Moose;

has view1 => ( 
      is      => 'rw'       # read/write
    , isa     => 'View1'
    , lazy    => 1
    , default => sub { View1->new(); } 
    );

has view2 => ( 
      is      => 'rw'       # read/write
    , isa     => 'View2'
    , lazy    => 1
    , default => sub { View2->new(); } 
    );

sub print {}
sub do_something {}

#void Main()
#{

package main;

  #App = new Application();
my $App = Application->new();

  #App->View1->DoSomethingForView(); 
$App->view1->do_something_for_view();
  #App->View1->DoSomethingForView1Special();
$App->view1->do_something_for_view1_special();
  #App->View2->DoSomethingForView(); 
$App->view2->do_something_for_view();
  #App->View2->DoSomethingForView2Special();
$App->view2->do_something_for_view2_special();

#}
于 2008-12-17T22:20:38.593 回答
2

如果您使用MooseX::Declare,您可能可以将语法简化为与您最初编写的内容非常相似的内容,例如,您的声明将变成:

use MooseX::Declare;
 class ViewBase {
  method DoSomethingForView() { } #May be virtual
 }

 class View1 extends ViewBase {
    method DoSomethingForView() { }
    method DoSomethingForView1Special() { }
 }

 class View2 extends ViewBase {
    method DoSomethingForView2Special() { }
 }

 class Application {
    method Print() { }
    method DoSomething() { }

     #Do some magic to create a view object (View1 or View2) and return

    #Something which I don't know to describe. Its like dynamically 
    #returning object of View1 or View2 at runtime
  }
于 2010-04-13T13:27:50.833 回答