Single Responsibility |
Class should have a single, well-defined responsibility and should not be responsible for multiple things. |
Open-Closed |
Software enteties (classes, modules, functions) should be open for extension but closed for modification. You should be able to add new functionality to class without changing its existing code. |
Liskov Substitution |
Objects of a subclass should be able to be used in the same way as objects of parent class without issues. |
Interface Segregation |
Classes shouldn’t have to implement interfaces that they don't need. |
Dependency Inversion |
High-level modules (i.e., classes depending on other classes) should not depend on low-level modules. Both should depend on abstractions. So, it's easier to change implementation of low-level module without affecting high-level module. |
OOP Cheat Sheet
A handy guide to the basic principles of Object Oriented Programming and Design. Covers standard and accepted principles, design patterns, and more! This is not intended to teach OOP, but as a reminder for those who already learned.
OOP General
SOLID principles
Not only Inheritance
Composition “has-a” (strong connection) |
Building has a room. Containing object owns it. Objects' lifecycles are tied (if we destroy the owner object, its members also will be destroyed with it) |
Aggregation “has-a” (medium connection) |
Сar and its wheels. We can take off the wheels, and they'll still exist. Doesn't involve owning. Lifecycles of the objects aren't tied: every one of them can exist independently of each other. |
Association objects “know” each other (weak connection) |
Mother and child. The difference with aggregation is only logical: whether one of the objects is part of other or not. |
Abstraction
We exposing only an object's relevant details to the outside world. And hiding the implementation details. Reduces the code's complexity and makes it easier to use. |
Polymorphism
Subclasses can define their own behaviors and yet share some of the same functionality of the parent class. |
Compile time polymorphism -- Method overloading
Class can have more than one method with the same name, but with different parameters
Run time polymorphism - Method overriding
An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.
Class can have more than one method with the same name, but with different parameters
Run time polymorphism - Method overriding
An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.
Encapsulation (What happens in Vegas...)
By encapsulating a class's variables, only the methods of the class can access them. It protects the data from external access or modification. |
Inheritance ( “is-a” relationship)
We can create a new class based on existing class. The new class can reuse the code and behavior of the ancestor class, and it can also add new features or modify the existing behavior. Types: • Single (one parent, one child) • Multi-level (child is created from another child) • Multiple (many parents, one child) • Hierarchical (one parent, many children) • Hybrid (child extend several parents, where one or more of them is a combination of different types of inheritance) |
Abstract Class | Interface
Abstract Class |
Interface |
|
Describes |
Attributes, methods |
Methods |
For classes |
With close connections (inheritance) |
That could have nothing in common |
Multiple Inheritance |
|
|
Key words |
Implements interface Extends class |
Extends interface |
Attributes |
|
|
Methods without realisation |
( keyword) |
|
Methods with realisation |
|
( keyword) |
Constructor |
|
|
Access modifiers |
any |
(default), for methods with realisation |
Basic terms
Class |
Data type acting like blueprint for individual objects, attributes, and methods. And rules for interacting with this entity |
Objects |
Instances of class created with specifically defined data. Object has a state (fields) and behavior (methods). |
Methods |
Functions describing behaviors of object. |
Attributes |
Defined in class template and represent state of object. Object fields. |
OOP
Object-oriented programming is a model that organizes software design around objects which interact with each other. |
Properties of static class members
Static attributes are initialized as follows: ints, floats -> 0, bools -> false, references -> null |
static methods have access only to static methods and attributes of a class (obviously).They can't have use the "this" reference since it doesn't make any sense. |
|
Garbage Collection
|
I/O
INPUT |
in the header of the class file :
in the method:
|
OUTPUT |
for single variables:
for multiple variables:
the idea is to convert variables to strings and concatenate them |
Strings
Declaration:
Some string methods: returns 1 is s=s2 and 0 otherwise <=> to concatenate strings |
Array declaration
In java you must allocate the memory for array frst by or
Then if the type is not primitive you need to instantiate each field of the array by
Also, we can declare arrays like this
|
JAVA main function (entry point)
|
JAVA general infos
JAVA platform = JVM + API JAVA |
JVM : execution environment for JAVA apps. Allows code to be machine independant as it executes inside of a virtual machine which abstracts the specefics of input\output, hardware configuration, and different OSs. JVM has its own native language : byte code (juts like a real computer has its own instruction set) The JVM interprets byte code and manages memeory for the programs automatically by its Garbage collector |
JAVA API : libraries that abstracts diverse functionalities. |
OOP Paradigms
Abstraction |
The selection of only useful information about an object for a particular application. |
Encapsulation |
Is the ability to control the access to the object's properties and methods, render them either visible (public) or hidden (visible only to internal functions). |
Polymorphisme |
The definition of different executions of a methode for different inputs and for different objects |
Heritage |
A description of a general\specific relationship between classes. In shorts a subclass has all the properties/ actions of its super class + its own ones + its redefined ones |
Composition, Agregation |
The creation of a class/object as a collection of other objects |
OOP building blocks
Class |
A blueprint of an object: its properties, behavior and how it interacts with the exterior world. |
Attributes & methods |
The attributes are the properties of an object while the methods represents its dynamic behavior inside of your program. These attributes and methods define how the object should be accessed, its internal behavior and how it interacts other objects |
Objects |
An instance of a class. (the class is the type definition and the object is the variable) => all instances of a class share the same fields but have distinct data inside. In addition to its state (properties) and behavior (methods) an object has an identity which distincts it from other objects (alias/ memory address ...) |
OOP goals
Simple modeling |
An accurate representation of the real world by grouping objects with their properties and actions |
Robustness |
Easy maintenance and bug detection. Strong typing which results in more robust code (predictable behavior of your code) |
Scalability |
Adding functionality comes down to establishing new connections with other objects and methods. |
Reusability |
Available features allowing code reusability. (inheritance) |
Design Patterns
Creational |
|
Creational |
|
Creational |
|
Creational |
|
Creational |
|
Structural |
|
Structural |
|
Structural |
|
Structural |
|
Structural |
|
Structural |
|
Structural |
|
Behavioral |
|
Behavioral |
|
Behavioral |
|
Behavioral |
|
Behavioral |
|
Behavioral |
|
Behavioral |
|
Behavioral |
|
Behavioral |
|
Behavioral |
|
Behavioral |
References
Wood, David, Bert Bates, Kathy Sierra, Brett D. McLaughlin, Gary Pollice, and David West. Head First Object-Oriented Analysis and Design: A Brain Friendly Guide to Object Oriented Programming. Cambridge: O'Reilly Media, Incorporated, 2006. Print. "Abstraction (computer Science)." Wikipedia. Wikimedia Foundation, 18 Apr. 2014. Web. 25 Apr. 2014. <http://en.wikipedia.org/wiki/Abstraction_(computer_science)> "Polymorphism (computer Science)." Wikipedia. Wikimedia Foundation, 25 Apr. 2014. Web. 25 Apr. 2014. <http://en.wikipedia.org/wiki/Polymorphism_(computer_science)> "Inheritance (object-oriented Programming)." Wikipedia. Wikimedia Foundation, 21 Apr. 2014. Web. 25 Apr. 2014. <http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)>. "Encapsulation (object-oriented Programming)." Wikipedia. Wikimedia Foundation, 21 Apr. 2014. Web. 25 Apr. 2014. <http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)> |
Dependency Inversion Principle (DIP)
a. High level modules should not depend upon low level modules. Both should depend upon abstractions. b. Abstractions should not depend upon details. Details should depend upon abstractions. |
Interface Segregation Principle (ISP)
Clients should not be forced to depend upon interfaces that they don't use. |
Liskov Substitution Principle (LSP)
Subtypes must be substitutable for their base types. |
Open-Closed Principle
Classes should be open for extension, and closed for modification. |
Single Responsibility Principle
Every object in your system should have a single responsibility, and all the object's services should be focused on carrying out that single responsibility. |
Don't Repeat Yourself (DRY)
Avoid duplicate code by abstracting out things that are common and placing those things in a single location. DRY is about having each piece of information and behavior in your system in a single, sensible place. |
Favor the following over inheritance
Delegation |
When you hand over the responsibility for a particular task to another class or method. |
Composition |
Use behavior from a family of other classes, and change that behavior at runtime. |
Aggregation |
When one class is used as part of another class, but still exists outside of that other class. |
Basic OO Terms
Abstraction |
The process of separating ideas from specific instances of those ideas at work. |
Polymorphism |
The provision of a single interface to entities of different types. Subtyping. |
Inheritance |
When an object or class is based on another object or class, using the same implementation; it is a mechanism for code reuse. The relationships of objects or classes through inheritance give rise to a hierarchy. |
Encapsulation |
Enclosing objects in a common interface in a way that makes them interchangeable, and guards their states from invalid changes |
Basic Principles
Encapsulate what varies. |
Code to an interface rather than to an implementation. |
Each class in your application should have only one reason to change. |
Classes are about behavior and functionality. |
Design Patterns (GoF)
Abstract Factory |
Creational |
Builder |
Creational |
Factory Method |
Creational |
Prototype |
Creational |
Singleton |
Creational |
Adapter |
Structural |
Bridge |
Structural |
Composite |
Structural |
Decorator |
Structural |
Facade |
Structural |
Flyweight |
Structural |
Proxy |
Structural |
Chain of Responsibility |
Behavioral |
Command |
Behavioral |
Interpreter |
Behavioral |
Iterator |
Behavioral |
Mediator |
Behavioral |
Memento |
Behavioral |
Observer |
Behavioral |
State |
Behavioral |
Strategy |
Behavioral |
Template Method |
Behavioral |
Visitor |
Behavioral |
Access Modifiers
Private |
Only inside the same class instance |
Protected |
Inside same or derived class instances |
Public |
All other classes linking/referencing the class |
Internal |
Only other classes in the same assembly |
Protected Internal |
All classes in same assembly, or derived classes in other assembly |
Static |
Accessible on the class itself (can combine with other accessors) |
Common Refactorings
Encapsulate Field |
Generalize Type |
Type-Checking ⇒ State/Strategy |
Conditional ⇒ Polymorphism |
Extract Method |
Extract Class |
Move/Rename Method or Field |
Move to Superclass/Subclass |
Other Principles
Don't Repeat Yourself (DRY) Duplication should be abstracted
|
Law of Demeter Only talk to related classes
|
Hollywood Principle "Don't call us, we'll call you"
|
You Ain't Gonna Need It Only code what you need now
|
Keep It Simple, Stupid Favor clarity over cleverness
|
Convention Over Configuration Defaults cover 90% of uses
|
Encapsulation What happens in Vegas...
|
Design By Contract And then write tests
|
Avoid Fragile Base Class Treat Base like a public API
|
Common Closure Principle Classes that change together, stay together
|
SOLID
Single Responsibility Principle A class changes for only one reason
|
Open/Closed Principle A class should be open for extension, closed for editing
|
Liskov's Substitution Principle Derived types should cleanly and easily replace base types
|
Interface Segregation Principle Favor multiple single-purpose interfaces over composite
|
Dependency Inversion Principle Concrete classes depend on abstractions, not vice-versa
|