Polymorphism Pattern.

Slides:



Advertisements
Similar presentations
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Advertisements

CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Lab 8: States and Transitions User Interface Lab: GUI Lab Oct. 16 th, 2013.
Java Syntax. Basic Output public class test1 { public static void main(String[] args) { System.out.println("Hello"); }
Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
The Template Method By Sinclair Schuller. What is the Template Method? “Skeleton” definition of an algorithm Allows redefinition of predetermined points.
Exception Handling (Chapter 8) CS 180 Recitation - February 29, 2008 Department of Computer Science Purdue University.
LAB 10.
Computer Programming Lab(4).
Using Jeroo To Teach Object-Oriented Concepts By Christian Digout.
Packages F Package is a container for classes F A package is a grouping of related types (classes and interfaces) providing access protection and name.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);
Recitation 1 CS0445 Data Structures Mehmud Abliz.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Object-Oriented Analysis and Design An Introduction.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Object Oriented Programming Lecture 11: Polymorphism.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
Java Programming: From the Ground Up
CSC241 Object-Oriented Programming (OOP) Lecture No. 16.
Information System Design (IT60105) Lecture 26 Object-Oriented System Testing.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
CREATING MENUS IN JAVA Mimi Opkins CECS 174. Menus Menus work well for console applications. The menu can be contained within a do-while loop and the.
Chain of Responsibility Behavioral Pattern. Defination Avoid coupling between the sender and receiver by giving more than one object a chance to handle.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
Repeating patterns Can you work out the next shape in the pattern?
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Chapter 4 Unordered List.
Web Design & Development Lecture 9
Unit II-Chapter No. : 5- design Patterns
Chapter 2 Clarifications
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Chapter 5 Ordered List.
Chapter 6 Queue.
Conception OBJET GRASP Patterns
Interface.
Polymorphism.
10.2 Implementation and Execution of Recursive Code
Name the shape below. rectangle rhombus square triangle A B C D
Data types, Expressions and assignment, Input from User
Something about Java Introduction to Problem Solving and Programming 1.
ABSTRACT FACTORY.
Chapter 4 Unordered List.
Conditional Loops.
Chapter 5 Ordered List.
Object-Oriented Programming (OOP) Lecture No. 28
Sampath Kumar S Assistant Professor, SECE
CSE 1020:Programming by Delegation
Chapter 6 Queue.
Factory Pattern.
Polymorphism CMSC 202, Version 4/02.
Shapes.
class PrintOnetoTen { public static void main(String args[]) {
Chapter 6 Queue.
SPLITTING OF COMBINED SHAPES
Java Programming: From the Ground Up
Decorator Pattern.
Consider the following code:
Can you work out the next shape in the pattern?
Can you work out the next shape in the pattern?
Presentation transcript:

Polymorphism Pattern

Problem: How handle alternatives based on type? How to create pluggable software components?   Solution: When related alternatives or behaviors vary by type (class), assign responsibility for the behavior using polymorphic operations to the types for which the behavior varies. Corollary: Do not test for the type of an object and use conditional logic to perform varying alternatives based on type.

Structure: Participant Classes: TheInterface:   TheInterface: This interface will provide the behavior which varies according to the class type. All classes implementing this interface will write the method accordingly. Implementer: The classes will implement the operations from the interface as per the polymorphic nature.

Example:   We have to develop an application which will draw different shapes. The user will use this application to draw shapes like Circle, Rectangle, triangle. At a later stage we can have some more shapes be added to the application. Use case diagram

Class Diagram

Sequence Diagram:

Java Code: Java Programs Shape.java public interface Shape { public void draw(); } Circle.java public class Circle implements Shape { public void draw() { System.out.println("This is a Circle"); Rectangle.java public class Rectangle implements Shape { System.out.println("This is a Rectangle"); Triangle.java public class Triangle implements Shape { System.out.println("This is a Triangle");

DrawShape. java import java. util DrawShape.java import java.util.Scanner; public class DrawShape { private static Shape shape; public static void main(String a[]) { System.out.println("Please enter options Draw 1.Circle 2.Rectangle 3.Trianle"); Scanner sin=new Scanner(System.in); int opt; opt=sin.nextInt(); switch(opt) { case 1: shape=new Circle(); break; case 2: shape=new Rectangle(); case 3: shape=new Triangle(); default: System.out.println("Invalid option "); System.exit(0); } shape.draw();