OOP Principles

Helpfulness: 0
Set Details Share
created 5 months ago by schembot
3 views
Basics of Object Oriented Programming
updated 5 months ago by schembot
show moreless
Page to share:
Embed this setcancel
COPY
code changes based on your size selection
Size:
X
Show:
1

What are the 4 principles of OOP

Encapsulation => Identifying Classes;

Abstraction => Separating Responsibilities;

Inheritance => Establishing Relationships;

Polymorphism => Leveraging Reuse;

2

When Identifying Classes what key things do you need to keep track of

Nouns and Verbs

3

What are the benefits of Separating Responsibilities?

Minimizes Coupling

Maximizes Cohesion

Simplifies Maintenance

Improves Testability

4

What is Coupling

Coupling is the dependency that one class has upon another. (my definition)

Coupling is the degree to which classes are dependent on other classes or external resources.

You want to minimize coupling as much as possible. The fewer dependencies the easier it is to maintain a class.

5

What is a good example of minimizing coupling

Every class needs Retrieve/Save functionality. So to minimize coupling you would split the 2 functions into a Data Access Layer.

6

What is Cohesion

A measure of how related everything in a class is to the purpose of the class

Example:

There is a lot of work required to manage an address. There are quite a few properties. Both a Customer class and and Order class require it, does that make sense to make the properties in both classes? No! Make a new class called Address.

7

What is Establishing Relationships

Defines how objects work together to perform the operations of the application

8

What are the types of relationships

  • Collaboration ("Uses A")
  • Composition ("Has A" )
  • Inheritance ("Is a")
9

Example of Collaboration relationship

All the repository (Data Access) classes use this.

10

What is Composition broken down into:

  • Aggregation
  • Composition
    • 1:1
    • 1:n
11

What does Levaraging Reuse boil down to?

Building Base Classes to help decouple and to make reusable items

12

Name the different types of Base Classes

  • Abstract Classes
  • Concrete Classes
  • Sealed Classes
13

What are some properties of an abstract class

  • It's an incomplete class, with at least one property or method not implemented
  • CANNOT be instatiated
  • Intended use is to be used as a base class

public abstract class EntityBase { //Abstract Class definition }

14

Concrete Class

Normal class that can be instantiated, can also be used as a base class.

15

Sealed Class

Cannot be extended through inheritance, marked with sealed keyword.

16

What are the 2 ways to override base class members

Abstract

Virtual

17

When making an abstract method what should you know

  • The Method Signature is a place holder, will not have any implementation
  • The abstract method can only be used in abstract classes
  • MUST be overridden by the derived class

Example:

public abstract bool Validate();

18

When making a virtual method what should you know

  • Method is made with default implementation, meaning you can define it.
  • Can be used in abstract or concrete classes
  • Optional to be overridden by the derived class.
19

What is polymorphism

Means literally "many shaped". How it is implemented is very easy.

Just overriding methods is polymorphism.

Let's take Object as our base class:

object.ToString() => 'System.Object'

Every class created has this Object.ToString() method, so you can use Polymorphism to override it for your created classes. Say Order.ToString()=>"4/14/14 (1)"

20

What is DRY

Don't Repeat Yourself

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system

21

What is SOLID

  • Single Responsibility
    • Every Class is responsible for exactly one thing
  • Open-Closed Principle
    • The classes you use should be open for extension but closed for modification
  • Liskov Substitution Principle
    • Derived Classes must be substitutable for their base class
  • Interface Segregation Principle
    • Class should be as specialized as possible
  • Dependency Inversion Principle