Object Oriented Programming Principles Flashcards
What are the 4 Basics of OOP?
Encapsulation
Abstraction
Inheritance
Polymorphism
What is Encapsulation?
Controlling access to the state.
Encapsulation is when an object, containing properties and methods, encapsulates the how the properties/data/state is accessed via an interface - specific methods that have permission to access and or mutate properties.
EG1: An instance of a bank account class would hold within it the data of the current balance for that account. However it would be very dangerous if the balance could be accessed directly, so an interface provided might specify ‘withdraw’ & ‘deposit’ methods for specific processes.
EG2: Getters & Setters
What is Abstraction?
Hiding unnecessary details from the outside world.
Abstraction is when an object hides methods and properties not required by the outside worlds, only giving the bare minimum required by the consumer for successful use.
What is Inheritance?
Using a new class to extend the definition/functionality of an existing class.
EG1: If there were several types of bank accounts, a basic Account class could be implemented, and several other classes for specific accounts can inherit from the account class. E.G. Saving Account inherits from Saving Account.
What is Polymorphism?
Changing or adding to the functionality of a class by overriding and/or overloading methods.
What are the SOLID principles?
Single Responsibility Open-Closed Liskov Substitution Interface Segregation Dependency Inversion
What is the Single Responsibility Principle?
A class should hold a single responsibility to an actor. There should be never more than one reason for a class to change. If there is, a new class is required.
What is the Open-Closed Principle?
Software entities should be open for extension, but closed for modification.
What is the Liskov Substitution Principle
Subtypes must be substitutable for their base types.
What is the Inferface Segregation Principle?
Classes that implement interfaces, should not be forced to implement methods they do not use.
What is the Dependency Inversion Principle?
High-level modules should not depend on low level modules, rather both should depend on abstraction. Abstraction should not depend on details; rather detail should depend on abstraction.
How is Object Oriented code made DRY (not-repeating)?
- Never create classes or interfaces that share the same functionality as another. SOLUTION: Use the existing class, or extend with inheritance if extending functionality.
- Minimise extensive if statements, especially for type checking. Use polymorphism to create simpler functions, or simplify as much as possible.
- Strictly adhere to Liskov Substitution principle & by extension the Open-Closed principle.
- If a class performs an action that is already covered by another class, it is acting as a middle man, which is repetition
- If a class does not have a responsibility, and uses another class, integrate the two classes.
What is Loose Coupling and why is it good?
When a class is used inside another class, it is considered a dependency - the class depends on the existence of another class to work. Loose coupling means that the state of the class instance (object) is never dependent on another class.
What traits indicate (unwanted) Tight Coupling?
- Feature Envy: A method accesses the data of another object more than its own data.
- Inappropriate Intimacy: One class uses the internal fields and methods of another class. Good classes should know as little about each other as possible. Such classes are easier to maintain and reuse.
- Message chains: When methods beget methods beget methods.
What are good practices for method composition?
- Any section of code that does something can be encapsulated in another (private) method. This makes the code MUCH more readable to another developer.
- Where simple code can be obvious, let it be obvious. Don’t encapsulate simple operations for the sake of readability. Developers aren’t stupid.
- Minimise temporary variables that are not “used” more than once.