OOP Principles Flashcards
A design pattern consists of how many parts, and what are they?
Four parts: The name The problem The solution The consequences
Why would a design pattern like “Front Controller” possibly be unsuitable for PHP?
PHP scripts run every time a request is made of them - Front Controller can require some heavy startup time (unlike say Java) NOTE: It can be used, if PHP’s limitations or operating environment is considered in the design process
Favour ‘what’ over inheritance?
Composition
What is the Strategy pattern used for?
Strategy is used to move a set of algorithms into it’s own type. That way the implementation of an algorithm is separate from the implementation of a specific set of objects, reducing duplication of code.
What is delegation?
It is where an object assigns a task to another object (the delegate).
What is composition?
It is combining simple objects or data types into more complex ones.
Why could composition make code more flexible?
Objects can be combined to handle tasks dynamically and in many more ways than can be anticipated in the class hierarchy alone.
Is tight coupling good or bad?
Bad - it describes when there is a lot of interdependency between objects, which makes changes difficult later
Code to an ….. not an ……?
Code to an INTERFACE not an IMPLEMENTATION.
In OOP design, to what does the phrase “Encapsulate that which varies” refer to?
If there is a varying concept (for example, a costing algorithm is selected based on conditions) - this is a candidate for polymorphism. Each alternative in a suspect conditional may be extracted to form a class extending a common abstract parent.
What is YAGNI?
“You Aren’t Going to Need It” - referring to application features, but also used when describing patterns. Based on XP principles, i.e. don’t overuse patterns!
Name some patterns involved in object creation?
Singleton Pattern
Factory Method Pattern
Abstract Factory Pattern
The Prototype Pattern
What is meant by “delegate object instantiation”?
In good OOP design, it’s not appropriate to create specific objects within another class (tight coupling). It’s better if the object can be passed a concrete class, preferably using polymorphism (method with an abstract base class as an argument).
Why are global variables bad in OOP?
They tie objects into their context, and break the principle of encapsulation. Also - because PHP wont warn you when globals collide, an important value may be overridden unknowingly.
What is a DSN and what does it do?
Data Source Name - holds information about a database and it’s tables.
What is a consequence of the Singleton pattern?
It’s better than using global variables, as the value can never be written unwittingly by another variable - BUT it does have to be used sparingly. It can create dependancies, and because it bypasses the method interface, it’s use can be hidden inside functions - making relationships hard to spot.
Conditionals can be a sign that code is ‘not right’ - when is it usually fine however?
Object creation in factory methods often requires a lot of conditionals.
If I have a factory method in an imaginary ‘CommsManager’ class - and I notice I’m starting to use conditionals in code to provide services as CommsManager starts to support new protocols - what should I do?
Look into inheritance. You can use polymorphism to encapsulate the creation of concrete objects. I would subclass CommsManager for each protocol, and it should implement it’s own factory method.
What is one of the negatives of factory method patterns?
In can encourage unnecessary subclassing.
Factory pattern is often used in conjunction with what other pattern?
Abstract Factory Pattern
When is an Abstract Factory Pattern useful?
Factory patterns are excellent for horizontal extension (i.e. adding more encoding types) - but fail when it comes to dealing with a related set of classes (i.e. vertical growth).
i.e. I have appointments, and I want to encode for text, and xml. Perfect for Factory pattern - but what If I want to add to do lists, contact lists, but still want to handle text and xml?
In C++ and Java, you generally have to have a factory function for each return type, because they enforce return types - how can PHP’s flexibility be used to benefit here?
You can use a single make function that makes use of constants (similar to enums) to choose which object to create, and return that type. Even though you can do this - it’s probably not desirable, as you are now ensuring you have to implement this switch statement in every concrete creators. Client classes also, cannot be certain that all concrete generators all the same products, as the interals of the make() function are a matter of choice.
Does the prototype pattern primarily make use of inheritance or composition?
Composition (although there is inheritance to allow for polymorphism)
What is the defining feature of the Prototype pattern?
It’s a type of factory pattern, that makes use of PHP’s clone feature. Essentially, you create a ‘factory’ class that takes as a parameter a group of objects that are linked in concept, but different types (i.e. sea, plains, forest terrain). Then you cache an instance of these. When a user requests one, you clone the original object.
If you implement a prototype pattern, and your objects that are passed into a factory also reference objects, what must you do?
Implement the __clone method on those objects, so it does a deep copy.
What is one other benefit of using the prototype pattern in relation to the data stored in each of the cached objects?
This is copied and handed down to the cloned objects, which means you can initialise them with information that is important to their operation once - and all objects created by the factory get that data.
By providing implementations for addUnit and removeUnit in the abstract base class for a Composite pattern, what is one of the risks you run in a large project?
There is no compile time enforcement to ensure someone implementing a Composite implements the addUnit and removeUnit methods.
Why are composite patterns useful?
Flexible (because everything in the composite pattern shares a common supertype, it’s very easy to add new composite or leafe objects to the design without changing the wider context.
It’s simple, the client using the composite structure has a straightforward interface.
Objects in the composite pattern are organised in a tree, so manipulating one can have a large affect on many objects
If a subclass extends an abstract class, and does not implement all of the abstract methods, what must the subclass be declared as?
Abstract
What is the decorator pattern useful for?
It can be used to extend the functionality of an object at run time (decorate it).
What is the main characteristic of the decorator pattern?
It uses inheritance, composition and delegation. Decorator objects are typically derived from the same parent as the objects you are trying to decorate.
What is the facade pattern useful for?
Providing a simple, clear interface to complex systems.
What is the facade pattern?
It’s about simplifying the interface. Creating a single point of entry for a tier or subsystem.
In PHP a facade pattern can also be used to…
encapsulate blocks of procedural code.
What pattern is useful if we want the client to be as divorced as possible from a class, but still want to know when events occur so we can react to it?
The observer pattern
What is the key element of the observer pattern?
The observer (client) registers itself to a central class that we wish to observe (the subject). To this end, the subject will implement an interface “Observable” that exposes the methods, attach, detach, notify.
Which object is responsible for providing the methods that an observer can use to query the state?
The subject object.
What is a registry?
An object / class that provides access to data, either via static methods, or a singleton object
The registry pattern may also be known as…
whiteboard or blackboard (because it’s like someone leaving a sticky note on a whiteboard for the next person to come along and get the information)
What is dependency injection?
In poorly designed OO code, a client may have a hard coded dependency to a service - this creates tight coupling. By making the client receive the service via a parameter (i.e. through the constructor) and ideally through an interface, the dependency is now reduced - you have injected the dependency into the client, rather than tightly coupling it.
What are the three types of dependency injection?
Constructor injection
Setter injection
Interface injection
What is one test to see if you have tight coupling?
If you can examine only the client code and determine what framework is being used, the client has a hard coded dependency on the framework
What is the most common way to write the prototype pattern (modern Javascript)?
To overwrite the prototype of a function using an object literal.
function Person() { } Person.prototype = { name: "Foobar", age: 23 };
What is one caveat of overwriting a functions prototype using an object literal (and how do you get around it)?
You cannot use the functions constructor to determine the type - you should use instanceof.
friend instanceof Person; // true (do it this way)
friend.constructor == Person; // false
If, when using the object literal to overwrite the default prototype - you need the constructor to be available - how would make this happen?
function Person() { } Person.prototype = { constructor: Person, // Other stuff };
If you create an object at runtime, then add a new member to the prototype - will this be reflected on the already created object?
Yes