SOLID Principles Flashcards

1
Q

What does the S stand for in SOLID?

A

Single Responsibility Principle

Classes and objects should only have one job

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

What does the O stand for in SOLID?

A

The Open/Closed Principle

Objects should be open to extension, but closed to modification

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

What does the L stand for in SOLID?

A

Liskov Substitution principle

Objects of the same type should be interchangeable

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

What does the I stand for in SOLID?

A

Interface Segregation

Small compact interfaces are preferable to ‘God Objects’.

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

What does the D stand for in SOLID?

A

Dependency Inversion

Applications should depend on Abstractions not concrete instances of an object.

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

Would connecting to a database in order to post a tweet be considered two responsibilities in a PostTweet object?

A

Yes - the database connection task should be abstracted.

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

Would the following code be a correct way to remove the responsibility of connecting to a database?

protected function getConnection()
{
return DatabaseLayer::getConnection();
}

A

Yes - but it could be improved even further by using dependency injection. This at least abstracts away the act of connecting - and allows for mocks and testing.

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

What is one of the most important tools in the PHP object model for implementing the ‘O’ part of solid (Open / Closed principle)?

A

Interfaces - these allow the creation of solidified and defined interfaces for creating objects when those objects have similar behaviours but different internals.

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

What does the Liskov substitution principle state?

A

That we should be able to swap objects of the same type without changing the correctness of the program.

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

Interfaces allow us to enforce inputs in PHP, what can we use to enforce outputs?

A

There are no features built into PHP that enforce outputs - this is why the Liskov principle is important (i.e. we must enforce this ourselves in the design).

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

What PHP features should we use to help enforce the Liskov principle?

A

Effective comments and type hinting.

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

If an object has more than 5-7 methods, what could this indicate?

A

It has too many responsibilities, and therefore could do with a refactor.

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

If you find yourself building an interface that not all objects can honor (i.e. you have to implement a stub for that method, but it is empty) what does this indicate?

A

That you should break your interfaces down into multiple interfaces.

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

How do you implement multiple interfaces?

A
class MyClass implements IMyInterface1, IMyInterface2
{
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

If it doesn’t make sense to use multiple interfaces (to deal with objects that may have an additional method that others don’t), what is another way of implementing this without violating the Interface Segregation principle?

A

Use traits - this allows those objects that need to use an extra method to have the method - but doesn’t require the interface to expose a method that not all objects will use.

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

Why can we simply use a method of an injected object without being concerned whether it exists?

A

Because if we have followed the O and the I principle, we should be using type hinting and interfaces to inject a dependency. This means the interface is consistent and guaranteed, and we can rely on the abstraction.

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

What is one of the most important benefits of dependency injection?

A

Testability

18
Q

What should you do with extra responsibilities that don’t fit with the ‘Single Reponsibility’ paradigm?

A

Delegate to other objects

19
Q

What does Meyers’ Open / Closed principle state?

A

That once an object has been completed, it may only be modified to fix bugs, and cannot be modified to add new features.

20
Q

In Meyers’ description of the Open / Closed principle, how would new features be added to an object?

A

Through inheritance (Open for extension).

21
Q

What does the Polymorphic interpretation of Open / Closed state?

A

Once solidified, the API of a class is set, but classes inheriting from that class may implement that API in anyway they choose.

22
Q

Which is the more common interpretation of Open / Closed principle, Meyer or Polymorphic.

A

The polymorphic interpretation - i.e. that once the API is set, classes that inherit from that class may implement that API in anyway they choose.

23
Q

Objects B and C inherit from A. Swapping B and C with A does not affect the way the program runs in anyway - what principle of Solid does this conform to?

A

The Liskov principle - that any object of the same type should be interchangeable without affecting the correctness of the program.

24
Q

Given the need to develop a database class, why would the Interface Segregation principle suggest that it be pertinent to add transactions to a second interface?

A

Because not all databases are transaction based, and if it were added to the main base class - then databases that don’t support transactions would have to implement that interface.

25
Q

Does dependency injection satisfy the requirements of Dependency Inversion?

A

No - only partially. Because of the way PHP typehinting works - type hinting on an interface or concrete object (class name) will both work if the object implements the interface. But only typehinting on the interface is correct.

26
Q

When considering the ‘Single Responsibility’ concept - is creating an object it’s own responsibility (and therefore deserving of its own class?)?

A

Yes it is.

27
Q

What design patterns could be used to create objects?

A

Abstract Factory, Factory Methods or Singleton.

28
Q

If you see objects created inside other objects, this indicates you are violating what principle?

A

Dependency Inversion.

29
Q

What is a Factory Method?

A

A method that is a part of an object, used to create new objects.

30
Q

A factory method is usually a part of which design pattern?

A

Abstract Factory.

31
Q

Why is the following method frowned up in creating factory methods?

Object::factoryMethod();

A

Using a static factory method creates tight coupling between the method and the class name (exactly what you are trying to avoid by using factory methods). It also makes mocking very difficult.

32
Q

If you have multiple similar types of objects (for example a caching system, but you want to support multiple engines) - what pattern is the best for this?

A

Abstract Factory

33
Q

In an Abstract Factory, the object that receives the generated object is called…

A

The client

34
Q

What is the name of the object that is generated from the Abstract Factory?

A

The product

35
Q

Why does the Factory Method exist at all, given that the Abstract Factory pattern is probably more ‘correct’ in terms of OO programming?

A

Because it can be a unnecessarily complicated for creating simple objects.

36
Q

What situations are Abstract Factory patterns not suitable for?

A

Creating large / complicated objects that require multiple steps for configurations.

37
Q

What is ‘Duck Typing’?

A

In polymorphism, passing an object of a specific type into a function will work, as long as the object is of the same type as expected by the parameter.

In PHP however, you do not have to specify the type (through type hinting) - so how does PHP handle a user passing an object that may not be of the intended type? As long as it has the correct method (i.e. called later) - it will work. It’s called duck typing because PHP says, if the object looks like a duck, walks like a duck - it must be a duck (so call the method).

38
Q

When would you use the term ‘Duck Typing’?

A

When you see non-inheritance based polymorphism.

39
Q

What is the Null object pattern?

A

Given a class that requires another object, it is often best to provide this object through dependency injection. If you were to do this through a setter - it is possible the object is created without that dependency. The null object pattern allows the object to continue working - without doing any conditional statements to see if the object exists or not.

class NullObject {
  public function log($message) { }
}
class ClassNeedsLogging
{
   public function usesLogger() 
   {
      $this->logger->log('If logger hasn't been set - this wont cause an error');
   }
   public function setLogger($logger)
   {
      $this->logger = $logger;
   }
}
40
Q

What are some of the pitfalls of using the Null Object pattern?

A

It can make errors or bugs appear as though things are executing normally.