Object Oriented Programming


Published on

My presentation to the Code Foo 2012 interns. http://code.ign.com/foo

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Object Oriented Programming

  1. 1. Object Oriented Principles and Design Manish Pandit
  2. 2. About me• Joined IGN in Aug 2009 to build the Social API Platform• Worked at E*Trade, Accenture, WaMu, BEA• Started working with Java ecosystem in ’96• @lobster1234 on Twitter, Slideshare, Github and StackOverflow
  3. 3. Programming• What constitutes a ‘program’? – Data – Behavior
  4. 4. Procedural Code#include<stdio.h>int main(){ int what, result; printf("nFactorial of what, fellow code-foo’er? t"); scanf("%d",&what); result = function(what); printf("nThe number you are looking for is %d n", result);}int function(int number){ int result; if(number==1) return 1; else result = number*function(number -1); return result;}
  5. 5. Lets analyze..• Simple to write, as it is linear. Think machine.• Can kick any OO language’s ass in performance BUT• No separation of data and behavior• No concept of visibility of variables• No encapsulation• Hard to map real world problems as humans – great for algorithms though.• Global, shared data.• Reusability achieved via copy paste, header files (.h) or shared object files (.so files in Unix)
  6. 6. Objects – What?• Abstraction over linear programming• Keywords – reduce, reuse, recycle• Modeling a problem around data and behavior vs. a big block of code• Identify patterns to model – Classes – Methods• HUMAN!
  7. 7. Confused?• Let me confuse you more with the cliché, textbook examples of – Shapes (Circle, Triangle, Rectangle, Square) – Animals (Dog, Cat, Pig, Tiger) – Vehicles (Car, Truck, Bike)
  8. 8. We can do better!• Lets model IGN the OO-way – We have games – We have properties of games – We have users (who have properties too) – Users follow games – Users follow other users – Users rate games – Users update status
  9. 9. Real world enough for you?• Actors – Nouns – Users, Games, Activities, Status• Actions – Verbs – Rate, Follow, Update• Properties – User – nickname, age, gender – Game – publisher, title, description
  10. 10. Connect the dots• Actors, Nouns – are Classes• Actions, Verbs – are functions or methods• A function operates on data encapsulated within a Class• Every actor has certain properties• A caller of this Class doesn’t give a shit to the implementation details (abstraction, data hiding)• Collectively, the properties and methods in a class are called its members.
  11. 11. Going back to C#include<stdio.h>int main(){ char* title; char* publisher;// save??// describe??}
  12. 12. Javapublic class Game { private String title; private String publisher; public String getTitle(){ return "Title is " + title; } public void setTitle(String title){ if(title!=null) this.title = title; else this.title = ""; } public String getPublisher(){ return "Publisher is " + publisher; } public void setPublisher(String publisher){ this.publisher = publisher; } public void describe(){ System.out.println(“Title is “ + title + “ Publisher is “ +publisher); }}
  13. 13. Principles so far..• Objects are instances of classes and are created by invoking the constructor which is defined in a class.• Objects have a state and identity, classes don’t.• Some classes cannot be instantiated as objects – Abstract classes – Factories (getting a little ahead of ourselves here!)• Properties are defined by “has-a” clause• Classes are defined by “is-a” clause (more on this later)
  14. 14. Inheritance• Classes can extend other classes and interfaces – is-a relationship • A Game is a MediaObject, so is a DVD • A User is a Person, so is an Editor• Java does not support multiple inheritance per- se, but you can mimic that via interfaces.• A cluster of parent-child nodes is called a hierarchy• CS Students – Generalization vs. Specialization
  15. 15. Inheritance and Behavior• Child classes can – override behavior – overload behavior Provided the Parent has provided visibility to its members
  16. 16. Association: Aggregation• A game has a title• A game has a publisher – A Publisher can live without this game. He can have other games. This is aggregation.
  17. 17. Association: Composition• A game has a story – A story cannot live outside of the game. This is composition
  18. 18. Behavior : Interfaces• An interface defines a behavior of the classes that inherit it – as seen by the consumers• Interfaces are actions on verbs – like Observable, Serializable, Mutable, Comparable. They add behavior.• Interfaces do not define implementation (that’s OO Power!). They just define behavior. The implementation is encapsulated within the class. This is polymorphism.
  19. 19. Behavior of saving data• Interface: public interface Persistable{ public void save(Object o); }• Implementations: public class MySQLStore implements Persistable{ public void save(Object o){ //save in MySQL } } public class MongoStore implements Persistable{ public void save(Object o){ //save in MongoDB } }
  20. 20. Packages• Packages for logical organization
  21. 21. Visibility and Modifiers• public/protected/package/private – Scala has explicit package-private – Accessors and Mutators• Advanced for this session – final – static – constructors (default, noargs..) – super
  22. 22. Recap : Object Modeling• Nouns, Verbs• is-a, has-a• Inheritance (is-a)• Association (has-a) – Composition – Aggregation• Data vs. Behavior
  23. 23. Design Patterns• Singleton• Adapter• Decorator• Proxy• Iterator• Template• Strategy• Factory Method
  24. 24. Advanced OO• Polymorphism – Dynamic/runtime binding• Mix-ins – Traits in Scala• Frameworks – Frameworks like Scalatra, Struts, Grails.. – Spring, Shindig
  25. 25. Case Study : IGNSocial• Interfaces• Implementation• Packages• Dependencies
  26. 26. Java Collections API• Go over the javadoc of collections API and identify – Packages – Inheritance • Classes • Interfaces
  27. 27. Collections Type Hierarchy
  28. 28. Factorial in Scala def fact(x:Int):Int=if(x==1) x else x*fact(x-1)
  29. 29. Further Reading• Apache Commons Library – Connection Pool – String and Calendar Utils – JDBC• Java or C++ Language Spec• Wikipedia