module 03 - object-oriented design principles

15
ObjectOriented Design Principles Oracle® Certified Professional, Java® SE 7 Programmer Module 3 © 20092012 JohnYeary

Upload: john-yeary

Post on 28-Oct-2014

3.407 views

Category:

Documents


2 download

DESCRIPTION

This presentation is the third module of 12 modules covering the Oracle Certified Java SE 7 Programmer exam. This module covers:Write code that declares, implements, and/or extends interfacesChoose between interface inheritance and class inheritanceDevelop code that implements "is-a" and/or "has-a" relationships.Apply object composition principlesDesign a class using the Singleton design patternWrite code to implement the DAO patternDesign and create objects using a factory, and use factories from the API

TRANSCRIPT

Page 1: Module 03 - Object-Oriented Design Principles

Object-­‐Oriented  Design  Principles  

Oracle®  Certified  Professional,  Java®  SE  7  Programmer  Module  3  

©  200

9-­‐20

12  Jo

hn  Yea

ry  

Page 2: Module 03 - Object-Oriented Design Principles

Module  3  -­‐  Objectives  

Ê  Write  code  that  declares,  implements,  and/or  extends  interfaces  

Ê  Choose  between  interface  inheritance  and  class  inheritance  

Ê  Develop  code  that  implements  "is-­‐a"  and/or  "has-­‐a"  relationships.  

Ê  Apply  object  composition  principles  

Ê  Design  a  class  using  the  Singleton  design  pattern  

Ê  Write  code  to  implement  the  DAO  pattern  

Ê  Design  and  create  objects  using  a  factory,  and  use  factories  from  the  API  

Page 3: Module 03 - Object-Oriented Design Principles

Implementing  an  interface  

Ê  To  implement  an  interface,  you  must  simply  state  that  your  class  implements  the  interface,  and  provide  overriding  methods  for  the  implementation  of  the  interface.  

Ê  An  interface  can  extend  another  interface.  It  does  not  implement  it.  

Ê  Remember  classes  implement  interfaces,  and  interfaces  extend  interfaces.  

Page 4: Module 03 - Object-Oriented Design Principles

Inheritance  

Ê  There  are  two  main  mechanisms  for  inheritance  in  Java;  class  and  interface.  

Ê  Java  supports  only  single-­‐class  inheritance.    

Ê  All  classes  extend  from  Object.  

Ê  Java  supports  multiple  interface  inheritance.  

Ê  Promotes  class  reuse  through  specialization  

Ê  Uses  polymorphism  

Page 5: Module 03 - Object-Oriented Design Principles

“is-­‐a”  relationships  

Ê  This  is  based  on  class  inheritance,  or  interface  implementation.  

Ê  An  Eagle  IS-­‐A  Bird.  

Ê  An  Eagle  IS-­‐A  Bird  of  Prey  

Ê  An  Eagle  IS-­‐A  Flyer  

Ê  A  Penguin  IS-­‐NOT  a  Flyer.  

Page 6: Module 03 - Object-Oriented Design Principles

“is-­‐a”  relationships  (cont.)  

Page 7: Module 03 - Object-Oriented Design Principles

“has-­‐a”  relationships  

Ê  This  is  based  on  usage  rather  than  inheritance,  or  interface  implementation.  

Ê  A  Car  HAS-­‐A  Radio  

Ê  An  Airplane  HAS-­‐A  Radio  

Ê  A  Boat  HAS-­‐A  Radio  

Page 8: Module 03 - Object-Oriented Design Principles

“has-­‐a”  relationships  

Page 9: Module 03 - Object-Oriented Design Principles

Object  Composition  

Ê  A  means  of  constructing  more  complex  objects  from  simple  building  blocks.  

Ê  A  composition  “has-­‐a”  relationship  between  the  main  Object  and  its  composed  parts;  e.g.  a  car  has  tires,  radio,  steering  wheel,  etc.  

Page 10: Module 03 - Object-Oriented Design Principles

Object  Composition  (cont.)  

Page 11: Module 03 - Object-Oriented Design Principles

Singleton  Design  Pattern  

Ê  The  typical  Singleton  design  uses  a  private  constructor,  and  has  a  static  method  to  get  an  instance  of  the  object.  

Ê  Since  the  constructor  is  private,  the  object  is  implicitly  final.  

Ê  Runtime.getRuntime();  is  a  singleton.  

Page 12: Module 03 - Object-Oriented Design Principles

Data  Access  Object  (DAO)  Design  Pattern  

Ê  This  is  a  means  of  abstracting  the  data  access  from  the  implementation.  

Ê  Uses  Transfer  objects  to  maintain  data.  

Ê  Traditionally  used  with  a    database,  but  is  a  more  general  design  for  use  with  any  data  store.  

Page 13: Module 03 - Object-Oriented Design Principles

Data  Access  Object  (DAO)  Design  Pattern  (cont.)  

Core  J2EE  Pattern  

Page 14: Module 03 - Object-Oriented Design Principles

Factory  Pattern  

Ê  A  GoF  Creational  Pattern.  

Ê  Designs  a  method  for  obtaining  an  instance  of  an  object  from  an  interface.  

Ê  A  concrete  implementation  is  used  to  create  the  object.  

Ê  Class.newInstance();  is  a  a  reflective  example  for  creating  an  object  in  Java.  

Page 15: Module 03 - Object-Oriented Design Principles

 Attribution-­‐NonCommercial-­‐ShareAlike  3.0  Unported  

This  work  is  licensed  under  the  Creative  Commons  Attribution-­‐NonCommercial-­‐ShareAlike  3.0  Unported  License.  To  view  a  copy  of  this  license,  visit  http://creativecommons.org/licenses/by-­‐nc-­‐sa/3.0/.