Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Object-Oriented PHP (1)
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
ASP.NET Programming with C# and SQL Server First Edition
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Lecture 9 Polymorphism Richard Gesick.
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Design Patterns Introduction to Design Patterns Eriq Muhammad Adams J. Mail : | Blog :
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Introduction to Object-Oriented Programming Lesson 2.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Classes (Part 1) Lecture 3
Lecture 12 Inheritance.
Inheritance and Polymorphism
Anatomy of a Class & Method
Lecture 23 Polymorphism Richard Gesick.
Abstract Classes.
Chapter 9 Object-Oriented Programming: Inheritance
Java Inheritance.
Fundaments of Game Design
Inheritance.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Banaras Hindu University

A Course on Software Reuse by Design Patterns and Frameworks

by Dr. Manjari Gupta Department of Computer Science Banaras Hindu University

Lecture 5 Basic Design Patterns

 common properties of design patterns  common, basic and important design patterns in the areas of object-oriented design and programming. Basic Design Patterns

 service provider objects corresponding to a specific service request are always of the same class type  Client object interacts directly with the service provider object  ties the client with a specific class type for a given service request  may not be adequate when there is more than one class of objects provide the same service  requires changes to the design and implementation of the client by other objects Interface design pattern

Client

Service Provider Service()

Client > Service Provider Service()

Service Provider_1 Service() Different Classes of Service Providers Offering the Same Set of Services

Service Provider_1 Service() Service Provider_1 Service() Different Classes of Service Providers Offering the Same Set of Services

Client Service Provider_1 Service() Service Provider_2 Service() Different Classes of Service Providers Offering the Same Set of Services

Client Service Provider_1 Service() Service Provider_2 Service() Different Classes of Service Providers Offering the Same Set of Services >

Client Service Provider_1 Service() Service Provider_2 Service() Different Classes of Service Providers Offering the Same Set of Services

 interface pattern can be used to better design  enable the client object to use different classes of service provider objects with little or no need for altering the client code  common services abstracted out and declared as a separate interface  each of the service provider classes implements this common interface Interface design patternCont…

Client

> Service()

Client > Service() > Service()

Client > Service() > Service() > Service()

Client > Service() > Service() > Service() Common Interface with Different Service Providers as Implementers

> Service() > Client > Service() > Service() Common Interface with Different Service Providers as Implementers

 client can safely assume the service provider object to be of the interface type  objects of different service provider classes can be treated as objects of the interface type  different types of service provider objects in a seamless manner without requiring any changes  client does not need to be altered Interface design patternCont…

 useful for consistent implementation of functionality common to a set of related classes  functionality outlined by abstract methods in an abstract class needs to be implemented differently  subclass must implement all of the abstract methods declared in the parent abstract class  ensures that the variable part of the functionality will be implemented in a consistent manner in terms of the method signatures. Abstract Parent class design pattern

 nonabstract subclasses of an abstract class can be instantiated  set of methods implemented by the abstract parent class is automatically inherited by all subclasses  eliminates the need for redundant implementations of these methods by each subclass. Abstract Parent class design patternCont…

Counter Subclass_1 Abstract method()

Counter Subclass_1 Abstract method() Counter Subclass_2 Abstract method

Counter Subclass_1 Abstract method() Counter Subclass_2 Abstract method Abstract class Abstract method

Abstract class Abstract method Counter Subclass_1 Abstract method() Counter Subclass_2 Abstract method An Abstract Class with Two Concrete Subclasses

 each method is designed to perform a single, defined task  methods may use the  functionality offered by other methods or  other objects to perform the task they are designed for.  Not all methods of a class are always meant to be used by external client objects  object’s public protocol are declared as public methods. Private Methods design pattern

 methods may exist to be used internally by other methods or inner classes of the same object  Private Methods  hides the behavior contained in these methods from client objects Private Methods design patternCont…

 state of an object can be grouped into two categories- public and private  Public state of an object  available to different client objects to access  Private state of an object  used internally by the object itself and not to be accessed by other objects Accessor methods design pattern

Customer Class id : int name :string SSN : String address: String Customer

 instance variable ID is  maintained separately and  used internally by each Customer class instance and  not to be known by other objects  the private state of a Customer object  variables such as name, SSN (Social Security Number) and the address  the public state of the Customer object and  supposed to be used by client objects Accessor methods design pattern Cont…

 In case of such an object, accessor Method pattern recommends  accessor methods to access the public state of an object  prevents external client objects from accessing object instance variables directly  hide from the client whether a property is stored as a direct attribute or as a derived one Accessor methods design pattern Cont…

 Client objects can make use of accessor methods to move a Customer object from one state (source) to another state (target).  If target state is not reached, it should notify the caller object that the transition could not be completed.  throw an exception Accessor methods design pattern Cont…

 If an object accesses its private variables directly  greatly affect the maintainability of an application  if an object access its instance variables through accessor methods  any change to the definition of an instance variable requires a change only to its accessor methods. Accessor methods design pattern Cont…

 data used by an object can either be variable data or constant data.  useful for designing an efficient storage mechanism for the constant data used by different objects  recommends all such constant data be kept in a separate object and accessed by other objects in the application.  easy to maintain,  centralized repository for the constant data in an application Constant data manager design pattern

customer data management Account final ACCOUNT_DATA_FILE : String = “ACCOUNT.TXT” final VALID_MIN_LNAME_LEN : int =2 Save () Address final ADDRESS_DATA_FILE : String = “ADDRESS.TXT” final VALID_MIN_LNAME_LEN : int =2 final DEFAULT_COUNTRY : String = “USA” CreditCard Save final CC_DATA_FILE : String = “CC.TXT” final VALID_CC_CHARS : String = “ ” final MASTER : String = “MASTER” final VISA : String = “VISA” final DISCOVER : String =”DISCOVER”

Address save () Account save () CreditCard save () ConstantDataManager save () final ACCOUNT_DATA_FILE : String = “ACCOUNT.TXT” final VALID_MIN_LNAME_LEN : int =2 final ADDRESS_DATA_FILE : String = “ADDRESS.TXT” final VALID_MIN_LNAME_LEN : int =2 final DEFAULT_COUNTRY : String = “USA” final CC_DATA_FILE : String = “CC.TXT” final VALID_CC_CHARS : String = “ ” final MASTER : String = “MASTER” final VISA : String = “VISA” final DISCOVER : String =”DISCOVER”

 data model classes  instances of these classes are referred to as data objects  several client objects may simultaneously access instances of such data model classes  changes to the state of a data object may not coordinated properly  ensures the concurrent access to a data object by several client objects without any problem  without involving the overhead of synchronizing the methods to access the object data Immutable object design pattern

 Applying the immutable object pattern, instances of the data model class become immutable  data of an immutable object cannot be modified makes it automatically thread-safe and eliminates any concurrent access related problems  designing an object as immutable is an important decision  If objects that were once thought of as immutable are in fact mutable  could result in difficult implementation changes Immutable object design patternCont…

Employee and EmployeeModel class representation EmployeeEmployeeModel firstName : String lastName : String SSN : String address : String car : Car getFirstName () : String getLastName () : String getSSN () : String getAddress () : String getCar () : Car setFirstName (fname : String) setLastName (lname : String) setSSN (ssn : String) setAddress (addr : String) setCar (c : Car) firstName : String lastName : String SSN : String address : String car : Car getFirstName () : String getLastName () : String getSSN () : String getAddress () : String getCar () : Car setFirstName (fname : String) setLastName (lname : String) setSSN (ssn : String) setAddress (addr : String) setCar (c : Car) save () : boolean delete () : boolean isvalid () : boolean update () : boolean

EmployeeModel Class: Immutable Version return car.clone (); final class EmployeeModel final firstName : String final lastName : String final SSN : String final address : String final car : Car getFirstName () : String getLastName () : String getSSN () : String getAddress () : String getCar () : Car

 an object may need to access shared resources  methods of such an object are accessed simultaneously by more than one thread,  could result in unpredictable behavior- race condition  ensures only one thread to execute any method on that object at any given time.  service provider object itself responsible to ensure that no two threads can execute its methods simultaneously  In Java, using the synchronized keyword Monitor design pattern