Perl的OO设计模式 [英] OO Design Patterns with Perl

查看:90
本文介绍了Perl的OO设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在计划新系统的设计,我需要编写与后端API交互的代码.我当时正在考虑对象的组合和继承,并决定在我的情况下,最正确的过程是将组合放在继承之上,因为我的对象彼此之间是具有a"关系,而不是是".

I am currently planning the design for a new system I will need to code that interacts with a back-end API. I was contemplating object composition and inheritance and decided that the most correct procedure in my situation would be to go with composition over inheritance as my objects have a "has a" relationship to one another and not an "is a".

我现在发现,尽管由于某些对象依赖于其他对象,所以可能存在对象A"具有属性对象B"和属性对象C"的情况-但是对象B"也具有属性对象C".

I find now though that because some objects are reliant on other, there may be cases were "object A" has an attribute which is "object B" and an attribute "object C" - however "object B" also has an attribute "object C".

希望这种比喻更有意义:

In hope that this analogy would make more sense :

让我们说我有一家公司出售的盒子里装有猫和放射性物质,它们可能会或可能永远不会反应:

Lets say I have a company that sells boxes which contains cats and radioactive substance inside of them which may or may never react :

我将产品出售给组织.用户通过指定他们所属的组织向我注册.一个组织可能有很多用户,也可能没有.用户必须具有其所属的组织.我会跟踪我的产品(盒子作为一个实体,猫作为一个实体)及其所属的组织.我还会跟踪这些猫以及它们所在的盒子.一个组织可能有很多盒子,其中的任何一个都有很多猫.框可能是空的.某些用户可以购买新盒子,而其他用户则可以看看它们.

I sell my product to organizations. Users register with me by specifying an organization that they belong to. An organization may have many users or none. A user must have an organization that it belongs to. I keep track of my product (the box as an entity, and the cat(s) as an entity) and which organization they belong to. I also keep track of the cats and which boxes they are in. An organization may have many boxes with many cats in any of them. Boxes may be empty. Some users are allowed to buy new boxes whilst other are just allowed to look at them.

身份验证和授权全部由我与之交互的API管理.

Authentication & Authorization is all managed by the API I interact with.

就对象关系而言:

$user has a => $organization that it belongs to

$user has a => $role that dictates what it may or may not do.

$box has a => $organization that it belongs to

现在:

$cat has a => $box that it belongs to 

AND

$cat has a => $organization that it belongs to ?

OR

$cat has a => $box that it belongs to WHICH has a => $organization that it belongs to

这里正确的设计决定是什么?我是否还在考虑其他方面,哪些可能使一个选项比另一个更可行?

What would be the correct design decision here? Are there other aspects I'm not considering which may make one option more viable than the other?

我将使用Perl CatalystMoose在此系统中实现MVC设计模式.

I will be implementing an MVC design pattern in this system using Perl Catalyst and Moose.

非常感谢大家的贡献.

推荐答案

您必须问自己一个问题.对猫来说,猫或盒子属于哪个组织重要吗?

You gotta ask yourself one question. Does it matter to a cat which organization a cat or a box belongs to?

例如,当您有一个猫咪对象时,您是否还需要知道猫的主人?是否有从猫开始的功能,是否有特定于所有者的功能-甚至在您不知道猫对象之前就不知道所有者?

E.g., when you have a cat object, do you even need to know its owner? Is there a functionality that starts with a cat, and does something owner specific - WITHOUT knowing the owner before you even know the cat object?

例如典型的功能总是始于用户:

E.g. a typical functionality would always start with a user:

my $org = $user->org();

继续寻找它的猫

my @cats = $org->listOwnedCats();

然后对其中一只猫做一些事情:

And then do something with one of the cats:

$cats[0]->CheckHealth();

请注意一个重要的事实:当您到达特定的猫时-您已经知道组织,因为这是您最初获得猫对象的方式.无需将$org存储在$cat对象中.

Note the important fact: by the time you get to the specific cat - you already KNOW the organization since that was how you got the cat object in the first place. There's zero need to store $org inside the $cat object.

盒子里的猫也一样.除了知道还没有装盒猫以外,您是否还需要找到猫的盒子?

Same is true for the cats in boxes. Do you EVER need to find a cat's object's box aside from knowing that some cat isn't boxed yet?

如果该功能模式成立(几乎总是如此),那么您将拥有非常严格的海峡前移对象模型:

If that functionality pattern holds (as it nearly always does), you have a VERY strait-forward object model:

  • 用户:属性是"org"和其他一些东西
  • 组织:属性为"UnboxedCatList"和"BoxList"-一个是尚未分配给箱子的猫的数组;一个是盒子对象的数组
    • 其中一种方法是PlaceUnboxedCatIntoBox()
    • User: Attributes are "org" and some other stuff
    • Org: Attributes are "UnboxedCatList" and "BoxList" - one is an array of cats that were not yet * assigned to boxes; one is an array of box objects
      • One of the methods is PlaceUnboxedCatIntoBox()

      这篇关于Perl的OO设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆