43
SOLID OOPS Converting Real world entities into programming Objects ; understanding its applications ; API designing, MVC framework Anirudh Tomer Harshith Keni Toshish Jawale

OOP, API Design and MVC

Embed Size (px)

DESCRIPTION

Presentation for a session I took with Toshish Jawale and Harshith Keni at a college from University of Pune. Covers MVC, API designing and Principles of Object oriented programming.

Citation preview

Page 1: OOP, API Design and MVC

SOLID OOPSConverting Real world entities into programming Objects ;

understanding its applications ; API designing, MVC framework

Anirudh TomerHarshith Keni

Toshish Jawale

Page 2: OOP, API Design and MVC

Introduction

We apologize for wrong session title Lets know each otherYour expectationsOur aimWhy should you join this sessionGoodies for active participants

Page 3: OOP, API Design and MVC

Real World Entity…

What is “Real”?Everything outside of your program is

Real.Even your own program is a Real World

Entity for some other program or even for your own program!

In short

“EVERYTHING IS REAL”

Page 4: OOP, API Design and MVC

Why do we bother about this?

Because these are all the “things” on which we want to work upon.

Yes, and by work we do not mean, just writing the code!

Work?

Still, how are they related to programming?

Page 5: OOP, API Design and MVC

Correlating to programming objects

Objects? Why?How are they introduced in the

programming world?Where are they used often?State and behaviourWho should use it?

Page 6: OOP, API Design and MVC

What is this class?

We separate our real world entities in different groups

Object is an instance of a class

Ball

instances of

Page 7: OOP, API Design and MVC

Organization

How classes are organized?Why classes need to be organized?Is there any standard way to do it?If yes, whats it?

Page 8: OOP, API Design and MVC

What class contains?

How these different attributes of a class are defined?

fields/properties - statemethods/functions - behaviorconstructors/initializers destructors

Page 9: OOP, API Design and MVC

Relations

What is the object oriented way of getting rich ?

— Inheritance Relations between the classesBy the way, are you good at relations?

Page 10: OOP, API Design and MVC

Interface

Have you watched Television?

Page 11: OOP, API Design and MVC

Package

Something you use to keep your stuff from mixing together

FoldersDrivesA customized, more sophisticated packageExamples familiar to you are,

◦ZIP file◦EXE file◦DLL file etc.

Point is, keep your stuff organized.

Page 12: OOP, API Design and MVC

What is Abstraction?

• Abstraction - a concept or idea not associated with any specific instance

• It is all about perspective

• Everything

• Something

• Exactly the thing

Page 13: OOP, API Design and MVC

Where does abstraction exist?

• Control abstractiono Abstraction of actions

• Data abstractiono Abstraction of information

Page 14: OOP, API Design and MVC

How does this relate to programming?

• Writing functions/subroutines is control abstraction.

• Datatypes is data abstraction.

Page 15: OOP, API Design and MVC

Data Abstraction

• Generalization

• Specialization

Page 16: OOP, API Design and MVC

Examplepublic class Animal extends

LivingThing{ private Location loc; private double energyReserves; public boolean isHungry() { return energyReserves < 2.5; } public void eat(Food f) { // Consume food energyReserves += f.getCalories(); } public void moveTo(Location l) { // Move to new location loc = l; }}

theChicken = new Animal();theCat = new Animal();if (theChicken.isHungry()) { theChicken.eat(grains);}if (theCat.isHungry()) { theCat.eat(mouse);}theCat.moveTo(theSofa);

Page 17: OOP, API Design and MVC

It's relevance with OOP

• Object is an attempt to combine data and control abstraction

• Polymorphism

• Inheritence

Page 18: OOP, API Design and MVC

Why do this?

• Separate the business logic from the underlying complexity

• Make it easier to parallelize tasks

• Meaningful amount of details are exposed

• Representation in a similar form in semantics (meaning) while hiding implementation details.

Page 19: OOP, API Design and MVC

Why do we write programs?

Page 20: OOP, API Design and MVC

We have the right objects in place, now what?

• Let's get them talking to each other

• Let's actually tell them what to talk about

Page 21: OOP, API Design and MVC

What sort of messages?

• Creation

• Invocation

• Destruction

Page 22: OOP, API Design and MVC

Who generates these messages?

• Aliens?

• The Government?• Platform

• User Interaction

• Events

Page 23: OOP, API Design and MVC

API Designing

Page 24: OOP, API Design and MVC

String operations

I need it to be menu drivenHehehe! The

whole 2 hours I will just write a

fancy menu

Programming Lab I

Page 25: OOP, API Design and MVC

Set operations

Again! *$%#@Menu will be “Enter 1 to

proceed 0 to exit”

Programming Lab I

I need it to be menu driven

Page 26: OOP, API Design and MVC

Don’ts we do Monolithic programs Single file containing all functions Code repetition Thinking less about function signature Reinventing the wheel

Page 27: OOP, API Design and MVC

What we think about API

API

Page 28: OOP, API Design and MVC

What it actually is

printf(), scanf(), strcmp()

Easy to use and hard to misuse

Can be programming language unspecific!!

Not necessarily a code library, can be just a spec

You can create your own API too

Spec of DS, functions, behavior

etc.

Page 29: OOP, API Design and MVC

Lets design an API

Page 30: OOP, API Design and MVC

Designing Process (based on suggestions by Joshua Bloch)

Write 2 programs and not one Write API first

◦ Understand the use case◦ Foresee future extensions (don’t change API often)

◦ A general purpose API vs multiple small API’s◦ If you can’t name it, its doing either extra or doing less◦ Don’t surprise the API user, don’t do extra◦ Remember! You can always add but you can’t remove◦ Document the API religiously!◦ Use consistent parameter ordering.◦ Extra params, use a struct

Code more! API is living thing Expect to make mistakes. Refactor API! Encourage others to use it

Page 31: OOP, API Design and MVC

MVC Architecture

Page 32: OOP, API Design and MVC

Programming without MVCLinked Listo add a nodeo remove a nodeo reverse a linked list

Linked list with GUI ______ ______ ______|__1__| |__5__| |__7__| …

I need it with GUI

Page 33: OOP, API Design and MVC

Challenges

Data Structures• struct ?• Separate

variables

Functions• Function

signature• Flow control

GUI• Creating boxes• Moving boxes

Page 34: OOP, API Design and MVC

Nightmare beginstypedef struct node{

struct node* nextNode;…}Node;…Node *linkedlist;

addNewNode(){while (linkedlist->nextNode!=null){

drawSquare(); //you may have internal D.S. for each squaredrawArrow();

}

Node *newNode = (Node*) malloc(sizeof(struct node));

scanf(); // for new valuesnewNode->val1 = …;newNode->val2 = …;linkedlist->nextNode = newNode;

while (linkedlist->nextNode!=null){drawSquare();drawArrow();

}}

Page 35: OOP, API Design and MVC

New requirements

Keep different colors for each nodeI want oval nodes not rectangular nodesI want linked list nodes to be shown in

hierarchy not in straight line.

Make Changes

Page 36: OOP, API Design and MVC

FB Timeline: Full of GUI Components

Page 37: OOP, API Design and MVC

MVC Framework: Coding vs. Architecting

Its nothing but how you design your code.

Page 38: OOP, API Design and MVC

Roles: Model, View and Controller

Model• Handling data• Save them on

file or in internal D.S

View• Display data

Controller• Read data from

view• Control user

interaction• Send new data

to model

Page 39: OOP, API Design and MVC

M-V-C not MVCKeep them separateYou may create different files

◦ A separate “model.h”◦ A separate “view.c” (includes model.h)◦ A separate “controller.c” (includes model.h)◦ Give deep thought to data structures

Principle of Agnosticism: each component is unaware of others presence

Page 40: OOP, API Design and MVC

Modeltypedef struct node{

struct node* nextNode;int id;…

}Node;int globalId = 0;

typedef struct linkedList{Node * headNode;int id;int listLength;

}LinkedList;LinkedList *linkedListPool[];

void addNode(int linkedListId, int index, Node *newNode){…

}void removeNode(int linkedListId, Node *node){

…}

LinkedList* createNewLinkedList(){ LinkedList* newll = (LinkedList*) malloc(sizeof(struct linkedList));newll->id = globalId++;newll->headNode = (Node*) malloc(sizeof(struct node));newll->length = 0;linkedListPool[globalId] = newll;return newll;

}

C++ or other OOP languages make

finding the context easy

Page 41: OOP, API Design and MVC

Viewtypedef struct viewNode{

struct viewNode* nextViewNode;int xLocation, int yLocation;String color; //#FF3e2A (in hex)

…}ViewNode;

//Similar to Model create another structtypedef struct display{

ViewNode* head;int id;int length;

}Display;Display *displayNodeList[];void addDisplayNode(int viewNodeId, int index, Node *newNode){

//create new ViewNode//Render UI

…}

//create new display node like Model

Page 42: OOP, API Design and MVC

Controllertypedef enum input{ADD,REMOVE,GET_LENGTH…

}Input;

LinkedList* linkedList = createNewLinkedList(); Display* display = createNewDisplay();

//Show a fancy menuInput input = read input from user;switch(input){

case ADD:Node* newNode = //malloc new node;addNode(linkedList->id,newNode);addDisplayNode(display->id,newNode);break;

}

Page 43: OOP, API Design and MVC

Questions ?