Programming patterns and techniques Flashcards
Guard clauses and example
Stop a functions execution by using the return keyword when certain conditions are met, continue otherwise.
function someFunction(isLoading){
if(isLoading){
return;
}
//else continue
}
Benefits of guard clauses
Prevents if-else nesting.
Makes code more legible.
What is an early return?
When the return keyword is used before the end of a function/method.
What is the strategy pattern?
Context class and strategy classes
The context class takes an instance of the strategy and can execute methods on the strategy via methods in the context class.
The strategies implement a common abstraction (inherits from an abstract class with abstract methods and properties OR implements a common interface)
Open Closed Principle, Dependency Inversion Principle
The client communicates with the context class.
What is the factory pattern/factory method pattern?
A factory class is meant to handle object creation of sub-classes that all use the same abstraction.
The abstraction may be:
1) An interface that each sub-class is meant to implement
2) An abstract class with abstract properties and methods that each sub-class is meant to inherit from
The client interfaces with the factory class
What is the observer pattern?
A subject class adds, removes and executes all observers, which are classes (that implement same interface) that are injected into the subject.
The observers must implement a common abstraction (abstract class with abstract properties and methods should be inherited from OR common interface should be implemented)
Dependency inversion principle, open-closed principle, single responsibility principle
The client should interface with the subject class.
What is the singleton pattern?
Ensures that only one instance of a class is in the application. Create a getInstance() method that either returns a new instance of the class, or the existing one if it already exists.
What are the SOLID principles
1) Single Responsibility Principle - class/function/module should be responsible for a single thing within the application.
2) Open-closed: Open for extension, closed for modification. A class, function, module should be extensible without modification being required of the source code.
3) Liskov substitution principle: should be able to replace any parent class instance/object with a child class instance/object - child class should not overwrite parent methods/properties. PHP use final keyword
4) Interface Segregation Principle: A class should not implement an interface unless it needs to expose all the methods/properties listed in the interface to the client.
5) Dependency Inversion principle: High-level modules/classes/functions should depend on abstractions (interface or abstract class) instead of concrete implementations as in dependency injection. These dependencies are object/class instances.
Classes only:
Interface segregation principle
Liskov substitution principle
Functions, classes, modules:
Single responsibility principle
Open closed principle
Dependency Inversion principle
Explain the Fluent Interface Pattern
Allows for method chaining, when a method(s) return the keyword this.
What are abstractions?
Interfaces
Abstract classes that implement abstract methods/properties, which child classes inherit from.
What is the client and an interface
The client is meant to interface with a class that implements a given interface. The interface tells the client which methods/properties are available. In other words, all interface properties and methods should be public.
What is UML
It stands for Unified Modelling Language.
It is meant to be a graphical representation of a code base, which includes interfaces, classes and relationships, inheritance and interface implementation.
Explain dependency injection vs dependency inversion
They are similar, where a class instance/function/modules depends on an instance of a different class(es).
Dependency injection: inject concrete class instance/object
Dependency inversion: inject class instance/object which implements an abstraction
What is a class vs object
An object is an instance of a class
What is loose coupling
This is when there is minimal interdependence between modules/classes/functions in a system.
Coding to abstractions helps achieve this (dependency inversion)