You are here: Re: Learning patterns... Decorator « PHP Programming Language « IT news, forums, messages
Re: Learning patterns... Decorator

Posted by Tyno Gendo on 09/28/07 08:24

Rik Wasmus wrote:
> On Fri, 28 Sep 2007 10:03:05 +0200, Willem Bogaerts
> <w.bogaerts@kratz.maardanzonderditstuk.nl> wrote:
>
>>> I'm trying to learn patterns, which I hope to use in my PHP code,
>>> although I'm finding it hard to get any real impression of how patterns
>>> fit in properly, I've done the following test code for Decorator pattern
>>> and want to know:
>>>
>>> a) is it correct, this is decorator pattern?
>>> b) how would i use this in practice with a database, eg. how would i
>>> store the 'attributes' in tables, and how would the 'pattern' be used in
>>> conjunction?
>>>
>>> Any hints, tips, pointers to examples would be appreciated:
>> The GOF book ("Design Patterns") is quite clear, I think.
>>
>> <snipped example>
>>
>> A decorator is a class that adds functionality at runtime. Functionality
>> you could add to your objects in that way are things like spread
>> payment, discount conditions, etc. The point of the pattern is that you
>> can add responsibility to specific objects instead of whole classes. It
>> is an alternative to subclassing (your example was subclassing).
>
> Yup, and Matt Zandstra has a pretty handy real decorator pattern example
> in PHP in his book ('PHP 5 Objects, Patterns, and Practice' I believe).
>

Are you sure I was subclassing? I thought I would have to use
'extends' to subclass.

I'm creating a new class that implemented same interface and passing
a base class in the constructor, so that is sub-classing?

I thought subclassing would be like:

class WhiteProduct extends Product {
... blah
... blah
}

I thought maybe I could have a CLargeProduct that got it's values from
a database somehow so it was pretty 'generic' and it's values applied
at run-time based on the db content? And the cost() function wraps
around or 'decorates' the base passed through the constructor???

You're probably right, I'm just trying to get a better understanding as
OOP isn't my scene but I would like it to be.

I'm probably very confused ;)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация