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

Slides:



Advertisements
Similar presentations
Chapter 5: The Singleton Pattern
Advertisements

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.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns.
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.
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.
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.
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
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.
עקרונות תכנות מונחה עצמים תרגול 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.
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.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Inheritance.
Modern Programming Tools And Techniques-I
Reference: Object Oriented Design and Programming (Horstmann)
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Advanced Programming in Java
Advanced Programming in Java
Unit II-Chapter No. : 5- design Patterns
Inheritance ITI1121 Nour El Kadri.
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.
CSC 143 Inheritance.
MSIS 670 Object-Oriented Software Engineering
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Programming Behnam Hatami Fall 2017.
Encapsulation Inheritance PolyMorhpism
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
Parameter Passing Actual vs formal parameters
Inheritance Inheritance is a fundamental Object Oriented concept
Advanced Programming in Java
CS 350 – Software Design Singleton – Chapter 21
CMSC 202 Generics.
SE-2811 Software Component Design
Review of Previous Lesson
Final and Abstract Classes
5. Strategy, Singleton Patterns
Refactoring.
עקרונות תכנות מונחה עצמים תרגול 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 of 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;

Example 2 Visitor pattern

Reminder: Polymorphic Variables

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

What happens if a method accepts an argument? Animal * + 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 Cat + say() + eats(mouse : Mouse) : bool + eats(cat : Cat) : bool Mouse + say() + eats(mouse : Mouse) : bool + eats(cat : Cat) : bool

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 Visitor +visit (cat : Cat) +visit (mouse : Mouse) Visited +accept (v : Visitor) Animal * +eats(animal: Animal)* +visit (cat : Cat)* +visit (mouse : Mouse)*

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 Visitor +visit (cat : Cat) +visit (mouse : Mouse) Visited +accept (v : Visitor) Animal * +eats (animal: Animal) 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) animal.visit(this)

Different types of Visitor and Visited Employees and Reports example

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

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

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.