Frameworks & Patterns Use of Organized Classes. Frameworks vs Toolkits Framework Framework  Start with classes and interfaces that define a rudimentary.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
OBJECT ORIENTED PROGRAMMING M Taimoor Khan
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
. Virtual Classes & Polymorphism. Example (revisited) u We want to implement a graphics system u We plan to have lists of shape. Each shape should be.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 25 GRASP: More Objects with Responsibilities 1CS6359 Fall 2011 John Cole.
R R R CSE870: Advanced Software Engineering: Frameworks (Cheng, Sp2003)1 Frameworks A Brief Introduction.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Inheritance using Java
MVC pattern and implementation in java
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Design Patterns.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Introduction to Object-oriented programming and software development Lecture 1.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Object Oriented Design. OOP Object Oriented programming (OOP) is the idea of developing programs by defining objects that interact with each other Java.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
GoF Sections Design Problems and Design Patterns.
Architectural pattern: Interceptor Source: POSA II pp 109 – 140POSA II Environment: developing frameworks that can be extended transparently Recurring.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
More Design Patterns From: Shalloway & Trott, Design Patterns Explained, 2 nd ed.
Design Patterns Introduction
Java Polymorphism. What we have learned Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. What are the other two?
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
Chapter Eight Expanding Our Horizons Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
S.Ducasse Stéphane Ducasse 1 Some Principles Stéphane Ducasse ---
CS 350 – Software Design The Adapter Pattern – Chapter 7 Gang of Four Definition: Convert the interface of a class into another interface that the client.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Design Patterns: MORE Examples
Object-Oriented Design
Web Design & Development Lecture 9
Design Patterns: Brief Examples
Inheritance ITI1121 Nour El Kadri.
Inheritance and Polymorphism
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
Informatics 122 Software Design II
Subtype Polymorphism, Subtyping vs
Software Design Lecture : 12.
Software Design Lecture : 14.
Informatics 122 Software Design II
Presentation transcript:

Frameworks & Patterns Use of Organized Classes

Frameworks vs Toolkits Framework Framework  Start with classes and interfaces that define a rudimentary app  Subclass and override to produce your app  Use polymorphism Example Example  MVC Frameworks (e.g. Struts)  Extend the Controller class to register and handle your events. Specify your view  Extend the View to resppond to user actions, possibly forwarding Events to a Dispatcher  Extend the Dispatcher, adding controllers of your choice

Frameworks vs Toolkits Toolkits Toolkits  Provide general purpose functionality for reuse  Do not impose a design  User calls the reused code Example: Google Web Toolkit (GWT) Example: Google Web Toolkit (GWT)  Provides core Java APIs and Resources  Compile your app to Java Script  Result is an Ajax app running on iPhone, e.g.  E.g. Google Maps and Gmail

Frameworks vs Toolkits Framework: Reuse the frameworks main body and write the code it calls Framework: Reuse the frameworks main body and write the code it calls  Customize by creating application-specific subclasses of the framework’s abstract classes  Emphasizes design reuse over code reuse Toolkit: Write the main program and call the code you want to reuse Toolkit: Write the main program and call the code you want to reuse  Do not impose a design  Emphasize code reuse

Design Patterns Are a way of organizing classes to solve certain kinds of problems Are a way of organizing classes to solve certain kinds of problems “Each Design Pattern describes a problem which occurs over and over again... and then describes the core of the solution in such a way that you can use this solution a million times over, without ever doing it the same way twice.” --Gamma, et.al “Each Design Pattern describes a problem which occurs over and over again... and then describes the core of the solution in such a way that you can use this solution a million times over, without ever doing it the same way twice.” --Gamma, et.al

Design Patterns Components Name the pattern Name the pattern State the problem it solves, when to apply State the problem it solves, when to apply Give the solution as a template Give the solution as a template State the consequences and tradeoffs State the consequences and tradeoffs

Design Patterns vs Frameworks Frameworks are more specialized Frameworks are more specialized Design Patterns do not solve specific problems Design Patterns do not solve specific problems Frameworks are the way OOP systems achieve reuse Frameworks are the way OOP systems achieve reuse

Encapsulation An object’s internal state cannot be directly accessed--it’s representation is invisible from outside. An object’s internal state cannot be directly accessed--it’s representation is invisible from outside. Pertains to hiding of Pertains to hiding of  Data fields  Methods  Other objects  Subclasses

Encapsulation Example +display +unDisplay +fill +setLocation +getLocation +setColor Circle XCircle +displayIt +unDisplayIt +fillIt +setLocation +getLocation +SetItsColor Shape

Encapsulation Note that the class Circle encapsulates the XCircle class Note that the class Circle encapsulates the XCircle class “Consider what should be variable in your design. This approach is the opposite if focusing on the cause of redesign. Instead of considering what might force a change in design, consider what you might want to be able to change without redesign. The focus here is on encapsulating the concept that varies, a theme of many design patterns”--GoF “Consider what should be variable in your design. This approach is the opposite if focusing on the cause of redesign. Instead of considering what might force a change in design, consider what you might want to be able to change without redesign. The focus here is on encapsulating the concept that varies, a theme of many design patterns”--GoF You can change design patterns without changing the design You can change design patterns without changing the design

Conclusions A program using a Shape only sees that, not a specific Shape. The abstract class Shape encapsulates the hierarchy of Shape subclasses A program using a Shape only sees that, not a specific Shape. The abstract class Shape encapsulates the hierarchy of Shape subclasses Abstract classes represent concepts Abstract classes represent concepts The operations represent a specification The operations represent a specification The concrete classes represent the implementation The concrete classes represent the implementation

The Open-Closed Principle Software units, e.g. classes or modules should be open for extension, but closed for modification Software units, e.g. classes or modules should be open for extension, but closed for modification  When a change in a program results in a cascade of changes to that program, then that program exhibits bad design  Design module/classes that never change  But be able to add new code, never changing old code that works The secret: Create fixed abstract entities that allow many possible behaviors The secret: Create fixed abstract entities that allow many possible behaviors

enum ShapeType {Circle, Square} struct Shape{ ShapeType itsType;} struct Circle{ ShapeType itsType; double itsRadius; point itsCenter:} struct Square{ ShapeType itsType; double itsSide; point itsTopLeft;} void DraweSquare(struct Square *); void DrawCircle(struct Circle *); typedef struct Shape *ShapePointer; void drawAllShapes(ShapePointer list[], int n){ int i; for(i=0;i<n;i++){ struct Shape *s = list[i]; switch(s->itsType){ case Square: DrawSquare((struct Square*)s); break; case Circle: DrawCircle(“ “ “ Circle*)s);..

Judgement This code does not adhere to the open- closed principle, because adding new Shapes would require modifying the code This code does not adhere to the open- closed principle, because adding new Shapes would require modifying the code Solution: Use an abstract Shape class and call the draw routines polymorphically Solution: Use an abstract Shape class and call the draw routines polymorphically

public abstract class Shape{ public abstract void draw();...} public class Square extends Shape{ public void draw();...} public class Circle extends Shape{ public void draw();...} void drawAll(List list) { ListIterator it = list.listIterator(); while (it,hasNext()){ Shape obj = it.next(); obj.draw();}

What If You Now Modify? Say, draw all squares before circles Say, draw all squares before circles Define a method precedes on the Shapes Define a method precedes on the Shapes This will not work, because it is not closed against new Shapes This will not work, because it is not closed against new Shapes Also uses reflection Also uses reflection Better: use a table of class names Better: use a table of class names  Shapes not found always those that are

Conclusions Never, never use public data in a class--it can never be closed to change Never, never use public data in a class--it can never be closed to change No class, not even derived ones, should depend on the data fields of a given one No class, not even derived ones, should depend on the data fields of a given one Avoid run-time identification--use polymorphism Avoid run-time identification--use polymorphism Stay away from global variables Stay away from global variables