OOP PHP Flashcards

1
Q

What methods should be private in a singleton class and why?

A

__clone and __construct

This prevents the object from being copied, and multiple instances being created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Should the singleton class in PHP be marked final?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are some of the issues with the singleton class?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is the singleton pattern useful in web development?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The builder pattern is useful for…

A

more complex objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The builder pattern allows creation in a …

A

step by step process

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is one of the major differences between the Abstract Factory pattern and the Builder pattern?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What kind of elements might a HTTP request object contain?

A

Headers / status codes, page templates, HTML layout etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What design pattern would be a good fit for creating a HTTP request object?

A

Potentially the builder pattern, since it can be a complex object requiring building up in stages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why is the abstract factory not suitable for something like a HTTP request object (complex object)?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What step is critical, when using the builder pattern, to ensure the client and the builder are loosely coupled?

A

Use interfaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The client should always know about, and expect ‘what’ kind of builder?

A

An abstract builder (via an interface)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Before creating the concrete builders - what is the most important thing to create?

A

The Abstract Builder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s a good substitute from using the traditional decorator pattern in PHP?

A

Use traits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is one draw back to the decorator pattern?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of the decorator pattern?

A

To add additional functionality or formatting to an existing interface. Typically by taking a reference to an object, providing a new object with the same interface that adds functionality around, or in, the result of the first object.

(i.e. takes a string object, adds formatting before and after to make it bold, before printing it out)

17
Q

What is another, looser term for a decorator pattern?

A

Wrapper.

18
Q

Is using a switch statement in a factory method and appropriate way to decide which class to instantiate?

A

Yes - it does work, but in a system where the number of objects may grow ridicuously large, you would be better using something dynamic:

public static function build($product_type, $sku, $name)
{
$product = “Product_” . ucwords($product_type);

  if(class_exists($product))
  {
    return new $product($sku, $name);
  }
  else {
    throw new Exception("Invalid product type given.");
  }
}
19
Q

What is a major difference between the bridge and the adapter patterns?

A

The bridge pattern is used typically when you are designing the systems (i.e you are implementing the bridge pattern, because you understand the potential for growth in this system). The adapter on the other hand is implemented when you are trying to get two systems to talk that you do not have control over (i.e. didn’t write).

20
Q

What is the use case for both the adapter and bridge patterns?

A

To Abstract the implementation of a system from the components making use of the implementation through the common interface

21
Q

What is the purpose of the repository pattern?

A

It provides a single point of entry for an application to request data from persistence (db etc). This is very useful in large products where there may be a requirement at a later date to change databases. By using a repository, rather than sprinkling access to the DB through the code, you can easily update the requests in one place - and switch out the original db.

22
Q

When would you use a bridge pattern?

A

When you as a programmer have control of both the application and the subsystem you are producing.

23
Q

When would you use an adapter pattern?

A

When you as a programmer do not have control over the subsystem api that you are adapting. (i.e. an outside library).

24
Q

What pattern is frequently confused with the adapter pattern?

A

The facade pattern

25
Q

What is the primary deciding factors between whether a facade or adapter pattern is used?

A

If the interfaces are similar - then it is likely an adapter pattern can be used to create a standardised interface for your application. A facade on the other hand wires together two very different interfaces - while trying to bring them together in a unified fashion.

26
Q

In an adapter, there are commonly four distinct parts - what are they?

A
The client - object that talks to the adapter
The target (the interface the client understands)
The Adapter (implements target interface, and holds reference to the adaptee)
The Adaptee (the object we are adapting for)
27
Q

What is the basic difference between a Factory and an Abstract Factory pattern?

A

A factory pattern creates implementations of a particularly interface (i.e. Apple, Banana), say IFruit.

With an abstract pattern, you create implementations of a particular factory interface - IFruitFactory, which knows how to create each of those different kind of fruit.

28
Q

In an adapter, the information flows in which direction?

A

Both directions, although control is usually from one direction.

29
Q

What is the purpose of the facade pattern?

A

It has a couple of purposes, namely - to simplify the interface to a 3rd party library or application, make it easier to understand, and reduce the dependencies between an application, and another applications/libraries interfaces.

The facade wraps the knowledge of how to obtain specific information from the 3rd party, and then the client application delegates to that facade.

This reduces coupling between the applications, and allows for easier changes to the applications interfaces.

30
Q

How is the facade pattern related to the gateway pattern?

A

The gateway pattern is simply a facade that connects an application to a persistance layer.

31
Q

When would be a good time to use the facade pattern?

A

For instance - if an application has a complex business logic module, that must be accessed from multiple parts in the code (perhaps calling many methods in that module, and combining the output in some way) - by using a facade you can create a simpler interface and have all clients that need that business logic use the facade. This makes later changes very easy (as it’s only in one place).