עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns.

Slides:



Advertisements
Similar presentations
Chapter 5: The Singleton Pattern
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
Advanced Programming in Java
Oct Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Singleton and Basic UML CS340100, NTHU Yoshi. What is UML Unified Modeling Language A standardized general-purpose modeling language in the field of software.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Lecture 12 March 16, The Scope of a Variable What if there are two variables with the same name? –A local or block-local variable can have the same.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
July 28, 2015IAT 2651 Design Patterns. “Gang of Four” July 28, 2015IAT 2652.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Object Oriented Programming
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
עקרונות תכנות מונחה עצמים תרגול 10: Generics. Outline  Generic classes  Generics & Inheritance  Wild Cards  Case study: Generic Graph.
Practical Session 11: The Visitor Pattern. Reminder: Polymorphic Variables.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Data Structures and Algorithms in JAVA Chapter 2.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Modern Programming Tools And Techniques-I
Reference: Object Oriented Design and Programming (Horstmann)
Advanced Programming in Java
Advanced Programming in Java
Design Patterns C++ Java C#.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Design Patterns C++ Java C#.
Design Patterns Damian Gordon.
Chapter 3: Using Methods, Classes, and Objects
Week 4 Object-Oriented Programming (1): Inheritance
PH page GoF Singleton p Emanuel Ekstrom.
עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns
MSIS 670 Object-Oriented Software Engineering
Advanced Programming Behnam Hatami Fall 2017.
What is Singleton Category: Creational pattern
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
CS18000: Problem Solving and Object-Oriented Programming
Advanced Programming in Java
Inheritance Inheritance is a fundamental Object Oriented concept
Advanced Programming in Java
CS 350 – Software Design Singleton – Chapter 21
SE-2811 Software Component Design
Review of Previous Lesson
5. Strategy, Singleton Patterns
עקרונות תכנות מונחה עצמים תרגול 7 – Design Patterns
Presentation transcript:

עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns

Outline  Design Patterns  Singleton pattern  Visitor pattern

Obejct-Oriented Software Design  Designing object-oriented software is hard – designing reusable object oriented software if even harder.  Your design should be specific to the problem at hand, but also general enough to address future problems and requirements.  Don’t solve every problem from first principles - Reuse solutions that have worked in the past.

Design patterns  A general reusable solution to a commonly occurring problem within a given context in software design.  A design pattern is not a finished design.  It is a description or template for how to solve a problem.  Patterns are formalized best practices that the programmer can use to solve common problems when designing.

Example 1 - problem  Design problem: We want to ensure a class only has one instance, and provide a global point pf access to it. Examples: - Log file - Printer spooler - File system access

Example 1- Solution Singleton Pattern

Singleton pattern The instance is held privately in the class. A getter is responsible of returning the instance. The constructor is private.

Singleton pattern Eager initialization public class Singleton { private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; }

Singleton pattern Lazy initialization public class SingletonDemo { private static SingletonDemo instance = null; private SingletonDemo() { } public static SingletonDemo getInstance(){ if (instance == null) { instance = new SingletonDemo(); } return instance; }

Visitor pattern Example 2

Reminder: Polymorphic Variables

Animal * + say() * Cat + say() Dog + say() System.out.println(“Miau”) System.out.println(“Woof”) Animal animal; animal = new Dog; animal.say(); \\ “Woof” animal = new Cat; animal.say(); \\ “Miau”

What happens if a method accepts an argument? Animal * + say() * + eats(mouse: Mouse) : bool * + eats(cat: Cat): bool * Cat + say() + eats(mouse : Mouse) : bool + eats(cat : Cat) : bool Mouse + say() + eats(mouse : Mouse) : bool + eats(cat : Cat) : bool boolean b; Animal cat = new Cat(); Animal mouse = new Mouse(); b = cat.eats(mouse); //compilation error

No support for polymorphism of arguments Java (and many other languages) supports only polymorphism of the receiver (the variable on which the method is invoked). Polymorphism of the receiver is performed using single dispatch. Polymorphism of arguments is performed using multiple-level dispatch, which is resource consuming. Therefore, it is not supported. Nevertheless, we can simulate it using the Visitor Pattern.

The visitor pattern (for methods with one argument)  Define two interfaces: Visitor and Visited.  The Visitor interface contains a method for each possible argument type.  The Visited interface contains an accpet() method. Visitor +visit (cat : Cat) +visit (mouse : Mouse) Visited +accept(v : Visitor)

The visitor pattern (for methods with one argument) The receiver's base class implements the visitor interface The argument’s base class implements the visited interface Animal * +eats(animal: Animal)* +visit (cat : Cat)* +visit (mouse : Mouse)* Visitor +visit (cat : Cat) +visit (mouse : Mouse) Visited +accept (v : Visitor)

Implementation of Visitor subclasses public class Cat extends Animal{ public void eats(Animal animal){ animal.accept(this); } public void visit(Cat cat){ s.o.p(“No”); } public void visit(Mouse mouse){ s.o.p(“Yes”); } … Implementation of Visited subclasses … public void accept(Animal animal){ animal.visit(this); }

Implementation of Visitor subclasses public class Mouse extends Animal{ public void eats(Animal animal){ animal.accept(this); } public void visit(Cat cat){ s.o.p(“No”); } public void visit(Mouse mouse){ s.o.p(“No”); } … Implementation of Visited subclasses … public void accept(Animal animal){ animal.visit(this); }

Putting it all together Animal * +eats (animal: Animal) Visited +accept (v : Visitor) Visitor +visit (cat : Cat) +visit (mouse : Mouse) animal.accept(this) Cat +accept(animal: Animal) +visit (cat : Cat) +visit (mouse : Mouse) Mouse +accept(animal: Animal) +visit (cat : Cat) +visit (mouse : Mouse) animal.visit(this)

Different types of Visitor and Visited Employees and Reports example

Block print(r: Regular)+ print(m: Manager)+ print(s: Specialist)+ Employee Regular Manager Specialist CSV print(r: Regular)+ print(m: Manager)+ print(s: Specialist)+ Format print(r: Regular)+ print(m: Manager)+ print(s: Specialist)+ Format f = new CSV(); Employee e = new Manager(); f.print(e);

Format print(e :Employee)+ Block visit(r: Regular)+ visit(m: Manager)+ visit(s: Specialist)+ Csv visit(r : Regular)+ visit(m :Manager)+ visit(s :Specialist)+ Employee accept(v: Visitor) Visitor visit(r : Regular) visit(m :Manager) visit(s :Specialist) v.visit(this) e.accept(this) Regular accept(v: Visitor) + Manager accept(v: Visitor)+ Specialist accept(v: Visitor)+ Visited accept(v: Visitor)

The Visitor Pattern Advantages Simulating multiple-level dispatch. Type-based treatment without instanceof. Extending the functionality of a class hierarchy by an external class.

The Visitor Pattern Shortcomings Tedious to code. Adding a Visited class requires modifying existing Visitor classes. Dispatching multiple arguments becomes inconvenient.