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.