Object Oriented Programming Flashcards
Learn OOP with C# .NET
OOP = 3 PILLARS
- Encapsulation 2. Inheritance 3. Polymorphism
what is gang of 4
In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book (Design Patterns: Elements of Reusable Object-Oriented Software) for explaining the concept of Design Pattern in Software development. These four authors are collectively known as Gang of Four (GOF).
Are patterns always good
gang-of-4 software design patterns not covered - using patterns tend to make the code more complicated and thus harder to maintain.
goal should be simplicity - not“how can i apply a pattern to this problem”
WHAT IS SOLID
SOLID is an acronym that deals with the following five object oriented design principles:
S: Single responsibility principle - a class should be responsible for doing one thing and doing that one thing quite well.
O: Open / closed principle - a software entity like an interface should be open for extension, but closed for modification.
L: Liskov substitution principle - a method must be able to accept a derived object in place of a base class object when specified as a method parameter.
I: Interface segregation principle - better to define many client-specific interfaces than one general-purpose interface.
D: Dependency inversion principle - decouples high and low level modules by using an abstraction like an interface.
* also of note: use composition over inheritance
WHAT IS OOP
- model representing concepts as objects that have data fields and behaviors (methods)
- objects are an instance of classes and structs - used to interact with one another
- OOP supports abstraction, encapsulation, inheritance, and polymorphism.
- kind of also includes generics, containers and OO principles / patterns
WHAT IS SEPARATION OF CONCERNS
- P-BL-D: presentation, business logic, data access layers
- splitting functionality so change in may has little to no impact on other layer.
- MVC is an example
WHAT DOES DONT REPEAT YOURSELF mean…
consolidating functionality into a single system, assembly, class or method
WHAT IS AN OBJECT
- an INSTANCE if the class or struct
- state, behavior, identity
- class methods, properties, fields, events can be accessed.
- NEW keyword needed
WHAT IS OOP MESSAGE PASSING?
- objects passing messages to each other to perform high level functions
- how the method performs the function and the class variables it uses are all encapsulated withing the method and the class
- think private
This concept is in sharp contrast to the concept of exposing the inner details of a class to outside user exposing functionality. Having methods or fields exposed makes maintenance easier for updates/changes to code base and prevents duplication but still a concern.
WHAT IS ENCAPSULATION
group of RELATED properties, methods or other members are treated as SINGLE OBJECT.
hiding unimportant details from the user of a class - to better focus on core capability of that class
fields and methods hidden by marking them as private - if marked public they are considered the PUBLIC INTERFACE for how other developers will use that class. Usually one or more constructors used to create and instantiate the class INTO an object.
encapsulation supported through use of classes and structs
WHAT IS POLYMORPHISM
use derived class in place of a base class.
- define a virtual method in base class
- override that method in a derived class
at runtime if the object is a derived class then the DERIVED class’s method is called instead of the base class method.
WHAT IS INHERITANCE?
Base class fields, methods, properties and events are available to derived class IF declared as public, protected, or internal protected
private fields still present in derived class but NOT ACCESSIBLE
only single inheritance supported.
interfaces may also be inherited
WHAT IS COMPOSITION?
class contains another class, not inheriting from it.
person class composed of 2 properties with their own class - when the instance of Person is destroyed the the composed objects die along with it
class Person
{
string firstName { get; set; }
string lastName {get; set; }
}
WHAT DOES THE TERM HAS-A RELATIONSHIP MEAN WHEN DESIGNING A CLASS
When dealing on how to design a class, if another object HAS-A relationship with that class then composition should be used instead of inheritance.
for example a car has-a wheel -> composition should be used here.
WHAT DOES IS-A MEAN WHEN DESIGNING A CLASS
when deciding on how to design a class, if another object is-a specialized type of that class, then inheritance should be used instead of composition.
a house is a building, inheritance should be used
is-a has closer ties to its parent object so will be inheritance
Difference between LOOSE and TIGHT coupling
coupling is speaking to the dependency of one class on another
tight = you asked for banana and you got a jungle that has a gorilla holding a banana.
- when one class changes it affects changes on other classes
- one class with 2 many responsibilities
- one concern spread over many classes rather then consolidated into its own class
- concrete class used - preferable to use interfaces
interfaces make testing better and more maintainable
WHAT IS ASSOCIATION
- relationship between objects where each object has its own life cycle - none of the objects own any other object.
- contractor has 20 jobs going and each job has 10 employees on it. If 1 employee dead, i job is dead = contractor still has independent lifecycle.
Association is implemented via dependency injection. Usually by constructor but can be done via set property or method.
WHAT IS AGGREGATION
- a specialized form of association
- all objects have own life-cycle BUT one object uniquely owns another object.
- an object can only be owned by one parent at a time
WHAT IS DEPENDENCY INJECTION?
- design principle - injecting dependency into a class vs. the class creating it.
- usually injected in the constructor but a set property could be used if the dependency is not yet available at creation time.
works hand in hand with inversion of control principle for resolving dependencies.