Upload
anirudh-tomer
View
981
Download
0
Tags:
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
SOLID OOPSConverting Real world entities into programming Objects ;
understanding its applications ; API designing, MVC framework
Anirudh TomerHarshith Keni
Toshish Jawale
Introduction
We apologize for wrong session title Lets know each otherYour expectationsOur aimWhy should you join this sessionGoodies for active participants
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”
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?
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?
What is this class?
We separate our real world entities in different groups
Object is an instance of a class
Ball
instances of
Organization
How classes are organized?Why classes need to be organized?Is there any standard way to do it?If yes, whats it?
What class contains?
How these different attributes of a class are defined?
fields/properties - statemethods/functions - behaviorconstructors/initializers destructors
Relations
What is the object oriented way of getting rich ?
— Inheritance Relations between the classesBy the way, are you good at relations?
Interface
Have you watched Television?
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.
What is Abstraction?
• Abstraction - a concept or idea not associated with any specific instance
• It is all about perspective
• Everything
• Something
• Exactly the thing
Where does abstraction exist?
• Control abstractiono Abstraction of actions
• Data abstractiono Abstraction of information
How does this relate to programming?
• Writing functions/subroutines is control abstraction.
• Datatypes is data abstraction.
Data Abstraction
• Generalization
• Specialization
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);
It's relevance with OOP
• Object is an attempt to combine data and control abstraction
• Polymorphism
• Inheritence
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.
Why do we write programs?
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
What sort of messages?
• Creation
• Invocation
• Destruction
Who generates these messages?
• Aliens?
• The Government?• Platform
• User Interaction
• Events
API Designing
String operations
I need it to be menu drivenHehehe! The
whole 2 hours I will just write a
fancy menu
Programming Lab I
Set operations
Again! *$%#@Menu will be “Enter 1 to
proceed 0 to exit”
Programming Lab I
I need it to be menu driven
Don’ts we do Monolithic programs Single file containing all functions Code repetition Thinking less about function signature Reinventing the wheel
What we think about API
API
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.
Lets design an API
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
MVC Architecture
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
Challenges
Data Structures• struct ?• Separate
variables
Functions• Function
signature• Flow control
GUI• Creating boxes• Moving boxes
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();
}}
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
FB Timeline: Full of GUI Components
MVC Framework: Coding vs. Architecting
Its nothing but how you design your code.
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
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
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
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
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;
}
Questions ?