OO Design Patterns with Perl

ⅰ亾dé卋堺 提交于 2019-12-05 12:44:00

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();

Proceed to find its cats

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

And then do something with one of the cats:

$cats[0]->CheckHealth();

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:

  • 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()
  • Box: Attributes are "CatList" (array of cat objects)
  • Cat: Attributes are cat specific - cat's don't own boxes or orgs.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!