Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Object Oriented Principle’s

940 views

Published on

Object oriented design

Published in: Technology, Education
  • Be the first to comment

Object Oriented Principle’s

  1. 1. Learning Session’s Object Oriented Life VIVEK.P.S
  2. 2. Welcome and Introduction The subject of the session. The overall goals of the session.
  3. 3. Overview Overview of the OOP’S. Why this is important for you. Class design principles C# and OOP’s
  4. 4. Vocabulary SRP: Single Responsibility Principle. OCP: Open/Closed Principle. LSP: Liskov Substitution Principle. ISP: Interface Segregation Principle. DIP: Dependency Inversion Principle.
  5. 5. Single Responsibility Principle
  6. 6. Single Responsibility Principle A responsibility is considered to be one reason to change a class. if we have 2 reasons to change for a class, we have to split the functionality in two classes. A class should have only one reason to change.
  7. 7. Single Responsibility Principle bad example-Protocol,Content change interface IEmail { public void setSender(String sender); public void setReceiver(String receiver); public void setContent(String content); } class Email implements IEmail { public void setSender(String sender) {// set sender; } public void setReceiver(String receiver) {// set receiver; } public void setContent(String content) {// set content; } }
  8. 8. Single Responsibility Principle good example interface IEmail { public void setSender(String sender); public void setReceiver(String receiver); public void setContent(IContent content); } interface Content { public String getAsString(); // used for serialization } class Email implements IEmail { public void setSender(String sender) {// set sender; } public void setReceiver(String receiver) {// set receiver; } public void setContent(IContent content) {// set content; } }
  9. 9. Single Responsibility Principle A new interface and class called IContent and Content to split the responsibilities. Adding a new protocol causes changes only in the Email class. Adding a new type of content supported causes changes only in Content class.
  10. 10. Open Close Principle
  11. 11. Open Close Principle Software entities like classes, modules and functions should be open for extension but closed for modifications. that the behavior of the module can be extended.we can make the module behave in new and different ways as the requirements of the application.
  12. 12. Open Close Principle They are “Closed for Modification”. The source code of such a module is inviolate. No one is allowed to make source Abstraction is the key principle.
  13. 13. - Bad example class GraphicEditor { public void drawShape(Shape s) { if (s.m_type==1) drawRectangle(s); else if (s.m_type==2) drawCircle(s); } public void drawCircle(Circle r) {....} public void drawRectangle(Rectangle r) {....} } class Shape { int m_type; } class Rectangle extends Shape { Rectangle() { super.m_type=1; } } class Circle extends Shape { Circle() { super.m_type=2; } }
  14. 14. Open Close Principle
  15. 15. Open Close Principle Good example class GraphicEditor { public void drawShape(Shape s) { s.draw(); } } class Shape { abstract void draw(); } class Rectangle extends Shape { public void draw() { // draw the rectangle } }
  16. 16. Open Close Principle
  17. 17. Liskovs Substitution Principle
  18. 18. Liskovs Substitution Principle Derived types must be completely substitutable for their base types. if a program module is using a Base class, then the reference to the Base class can be replaced with a Derived class without affecting the functionality of the program module.
  19. 19. // Violation of Likovs Substitution Principleclass Rectangle { protected int m_width; protected int m_height; public void setWidth(int width) { m_width = width; } public void setHeight(int height) { m_height = height; } public int getArea() { return m_width * m_height; } } class Square extends Rectangle { public void setWidth(int width) { m_width = width; m_height = width; } public void setHeight(int height) { m_width = height; m_height = height; }}
  20. 20. class LspTest { private static Rectangle getNewRectangle() { // it can be an object returned by some factory ... return new Square(); } public static void main (String args[]) { Rectangle r = LspTest.getNewRectangle(); r.setWidth(5); r.setHeight(10); // user knows that r its a rectangle. // It assumes that hes able to set the width and height as for the base class System.out.println(r.getArea()); // now hes surprised to see that the area is 100 instead of 50. }}
  21. 21. Liskovs Substitution Principle it means that we must make sure that new derived classes are extending the base classes without changing their behavior.
  22. 22. Interface Segregation Principle
  23. 23. Interface Segregation Principle clients should not be forced to implement interfaces they dont use.
  24. 24. // interface segregation principle - bad exampleinterface IWorker { public void work(); public void eat();}class Worker implements IWorker{ public void work() {} public void eat() {}}class Robot implements IWorker{ public void work() {} public void eat() {}}class Manager { IWorker worker; public void setWorker(IWorker w) { } public void manage() { worker.work(); } }
  25. 25. // interface segregation principle - good exampleinterface IWorkable { public void work();}interface IFeedable{ public void eat();}class Worker implements IWorkable, IFeedable{ public void work() {} public void eat() {}}class Robot implements IWorkable{ public void work() {}}
  26. 26. Dependency Inversion Principle
  27. 27. Dependency Inversion Principle High-level modules should not depend on low-level modules. Both should depend on abstractions Give an example. Abstractions should not depend on details. Details should depend on abstractions
  28. 28. Dependency Inversion Principle 3 modules, Copy module call other 2 modules.2 sub modules are re usable
  29. 29. Dependency Inversion Principlevoid Copy(){int c;while ((c = ReadKeyboard()) != EOF)WritePrinter(c);}
  30. 30. Dependency Inversion Principleenum OutputDevice {printer, disk};void Copy(outputDevice dev){int c;while ((c = ReadKeyboard()) != EOF)if (dev == printer)WritePrinter(c);elseWriteDisk(c);} New device disk
  31. 31. Dependency Inversion Principle Make Copy module re usable.
  32. 32. class Reader{public:virtual int Read() = 0;};class Writer{public:virtual void Write(char) = 0;};void Copy(Reader& r, Writer& w){int c;while((c=r.Read()) != EOF)w.Write(c);}
  33. 33. Summary SRP: One reason to change. OCP: Extend function with out editing code. LSP: Derived types can substitute base. ISP: Don’t implement Interface they don’t use DIP: Depend on abstraction instead of details.
  34. 34. C# oop’s Refreshing of what you know..
  35. 35. More information http://vivekcek.wordpress.com.

×