OOP PHP Flashcards
What methods should be private in a singleton class and why?
__clone and __construct
This prevents the object from being copied, and multiple instances being created.
Should the singleton class in PHP be marked final?
No, not necessarily. There are legitimate reasons (such as adding functionality) to leave the class as ‘not final’. However, you have to be careful to not violate the principles of the design pattern as a result of the PHP limitations.
What are some of the issues with the singleton class?
It is impossible to extend (if implemented correctly)
Being global, it’s difficult to test because changes made to the single are reflected across all tests, not just the area under test
PHP allows the developer to redeclare the __construct and __clone methods as public in a child class (which breaks the whole concept of a singleton
Violates many of the SOLID principles - creates itself, so can’t abstract that away (Single Responsibility Principle). It can’t be swapped with a subclass of itself (Liskov Substitution principle).
Is the singleton pattern useful in web development?
Generally - no. It seems to have few real, legitimate uses.
However, it could be part of the Registry pattern (to make sure you only have one Registry), or you could use it for configuration / or preferences object. But there are probably better uses.
The builder pattern is useful for…
more complex objects
The builder pattern allows creation in a …
step by step process
What is one of the major differences between the Abstract Factory pattern and the Builder pattern?
The Builder Pattern only returns the object to the client when the client asks for it (i.e. it may be sometime AFTER the object has begun creation).
What kind of elements might a HTTP request object contain?
Headers / status codes, page templates, HTML layout etc.
What design pattern would be a good fit for creating a HTTP request object?
Potentially the builder pattern, since it can be a complex object requiring building up in stages.
Why is the abstract factory not suitable for something like a HTTP request object (complex object)?
Abstract Factory expects the configuration and settings to be injected when the client commands the factory to create the object. This is usually not possible in more complex objects such as a HTTP request object.
What step is critical, when using the builder pattern, to ensure the client and the builder are loosely coupled?
Use interfaces
The client should always know about, and expect ‘what’ kind of builder?
An abstract builder (via an interface)
Before creating the concrete builders - what is the most important thing to create?
The Abstract Builder
What’s a good substitute from using the traditional decorator pattern in PHP?
Use traits
What is one draw back to the decorator pattern?
The decorator pattern creates objects that use a common interface, however, depending on whether the object is the concrete object or the decorator, the objects behave differently. I.e. a user needs to know that passing a concrete decorator object to another decorator object will be syntactically correct (due to shared interfaces) but will certainly not give the expected results.