PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Jun 24, 2019 5:31 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Feb 18, 2014 12:23 am 
Offline
The Ninja Space Mod
User avatar

Joined: Fri Aug 05, 2005 1:53 pm
Posts: 6424
Location: Paradise, CA
I have a series of classes that make up a composite structure. I'm going to simplify the interface just to make it easier to understand. Let's say I have components and properties. Components can have sub-components and properties. Sometimes certain components require certain sub-components. Sometimes certain components require certain properties. Sometimes components have required properties depending on whether certain other properties are set or have certain values. So, basically I have to define rules that specify when properties are required and when they aren't. Here is an example structure of components/properties:

Code:
/MyLibrary
    /Component
        Alarm.php
        Calendar.php
        Event.php
        Todo.php
        Entry.php
    /Property
        Method.php
        Version.php
        ProdId.php
        Start.php
        End.php
        Description.php
        Summary.php
        Title.php


Calendar components ALWAYS require Version and ProdId properties. Event components can optionally have Summary, Description, and Title properties. They are REQUIRED to have a Start property if they are the sub-component of a Calendar component that DOESN'T have a Method property. Entries ALWAYS require a Title and Description property. Alarm components can only be sub-components of Event and Todo properties, but if an Event or Todo component has an Alarm sub-component, it MUST also have a Start and End property.

So you can see how sometimes the specifications for required properties/components can be either very straightforward or somewhat complex. At first I was just going to add a $requiredProperties property on the component class, but after finding out that the specifications for when properties are required can be much more complex than I first thought, it became clear that I needed something a little more complex to define required properties. I have been looking at my Gang of Four design patterns book and the only design pattern I have found that seems even somewhat close to what I need is the decorator pattern. Can anybody think of a good way to define which properties are required and when? I am really struggling with this...


Top
 Profile  
 
PostPosted: Tue Feb 18, 2014 6:47 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13590
Location: New York, NY, US
Sounds more like the Strategy pattern where you would base what properties and/or sub-components are needed base on the Component type and other factors.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Wed Feb 19, 2014 12:32 am 
Offline
The Ninja Space Mod
User avatar

Joined: Fri Aug 05, 2005 1:53 pm
Posts: 6424
Location: Paradise, CA
Yeah I had considered Strategy briefly but it didn't seem to quite fit. I'll take a look again and see what I think. Thanks, man :)


Top
 Profile  
 
PostPosted: Tue Jun 24, 2014 6:36 am 
Offline
Forum Newbie

Joined: Tue Jun 24, 2014 6:28 am
Posts: 3
Thanks for sharing this information .


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group