SlideShare a Scribd company logo
Design Pattern
Iterator、Mediator、Memento
Sean Tsai 2015.11.10
Iterator
Provide a way to access the elements of an aggregate object
sequentially without exposing its underlying representation.
Also knowns as Cursor
Sample - McDonald’s + KFC
Iterator
Sample - McDonald’s + KFC
McDonaldMenu mcDonaldMenu = new McDonaldMenu();
ArrayList breakfastItems = mcDonaldMenu.getMenuItems();
KFCMenu kfcMenu = new KFCMenu();
MenuItem[] lunchItems = dinerMenu.getMenuItems();
Iterator
Sample - McDonald’s + KFC
Iterator
Sample - McDonald’s + KFC
for (int i = 0; i < breakfastItems.size(); i++) {
MenuItem menuItem = (MenuItem)breakfastItems.get(i);
System.out.print(menuItem.getName() + “ “);
System.out.println(menuItem.getPrice() + “ “);
System.out.println(menuItem.getDescription());
}
for (int i = 0; i < lunchItems.length; i++) {
MenuItem menuItem = lunchItems[i];
System.out.print(menuItem.getName() + “ “);
System.out.println(menuItem.getPrice() + “ “);
System.out.println(menuItem.getDescription());
}
Iterator
Diagram
Iterator
Pros and Cons
Pros:
● It supports variations in the traversal of an aggregate.
● Iterators simplify the Aggregate interface.
Iterator
Mediator
Mediator pattern is used to reduce communication complexity
between multiple objects or classes.
Sample 1 - Unix System
Mediator
Sample 2 - Control Tower
Mediator
Sample 3 - Dialog box
MediatorMediator
Diagram
Mediator
Pros and Cons
Pros:
● Limit subclassing and specialization.
● Decouples colleagues.
Cons:
● Reducing the complexity of colleagues increases the complexity of the
Mediator itself.
Mediator
Memento
Memento pattern is used to restore state of an object to a previous
state. Memento pattern falls under behavioral pattern category.
Diagram
Memento
Sample Code
public class Memento {
private String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
}
Memento
Sample Code
public class Originator {
private String state;
public void setState(String state) {
this.state = state;
}
public String getState() {
return state;
}
public Memento saveStateToMemento() {
return new Memento(state);
}
public void getStateFromMemento(Memento Memento) {
state = Memento.getState();
}
}
Memento
Sample Code
Memento
import java.util.ArrayList;
import java.util.List;
public class CareTaker {
private List<Memento> mementoList = new ArrayList<Memento>();
public void add(Memento state){
mementoList.add(state);
}
public Memento get(int index){
return mementoList.get(index);
}
}
Diagram
Mediator
public class MementoPatternDemo {
public static void main(String[] args) {
Originator originator = new Originator();
CareTaker careTaker = new CareTaker();
originator.setState("State #1");
originator.setState("State #2");
careTaker.add(originator.saveStateToMemento());
originator.setState("State #3");
careTaker.add(originator.saveStateToMemento());
originator.setState("State #4");
System.out.println("Current State: " + originator.getState());
originator.getStateFromMemento(careTaker.get(0));
System.out.println("First saved State: " + originator.getState());
originator.getStateFromMemento(careTaker.get(1));
System.out.println("Second saved State: " + originator.getState());
}
}
Sample Code
Memento
import java.util.ArrayList;
import java.util.List;
public class CareTaker {
private List<Memento> mementoList =
new ArrayList<Memento>();
public void add(Memento state){
mementoList.add(state);
}
public Memento get(int index){
return mementoList.get(index);
}
}
Pros and Cons
Memento
Pros:
● The history is maintained without knowing what changed inside the
object.
Cons:
● Using mementos might be expensive.
● It may be difficult in some languages to ensure that only the originator can
access the memento's state.
End

More Related Content

What's hot

Erlang assembly
Erlang assemblyErlang assembly
Erlang assembly
rstudnicki
 
Data Storage
Data StorageData Storage
Data Storage
mailalamin
 
Jp notes
Jp notesJp notes
Control de acceso con excel
Control de acceso con excelControl de acceso con excel
Control de acceso con excel
Reember Alex Arteaga Ticona
 
Aula 6 - 08/05 (Menu)
Aula 6 - 08/05 (Menu)Aula 6 - 08/05 (Menu)
Aula 6 - 08/05 (Menu)
Ricardo Longa
 
Annihilate test smells by refactoring to patterns
Annihilate test smells by refactoring to patternsAnnihilate test smells by refactoring to patterns
Annihilate test smells by refactoring to patterns
cenny2
 
culadora cientifica en java
culadora cientifica en javaculadora cientifica en java
culadora cientifica en java
Jorge Llocclla Rojas
 
JDBC (JAVA Database Connectivity)
JDBC (JAVA Database Connectivity)JDBC (JAVA Database Connectivity)
JDBC (JAVA Database Connectivity)
HimanshiSingh71
 
Studyx4
Studyx4Studyx4
Java swing
Java swingJava swing
Java swing
Arati Gadgil
 
Studyx1
Studyx1Studyx1
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
PRN USM
 
Yappo Groonga - with japanese search software history @ osdc.tw 2011
Yappo Groonga - with japanese search software history @ osdc.tw 2011Yappo Groonga - with japanese search software history @ osdc.tw 2011
Yappo Groonga - with japanese search software history @ osdc.tw 2011
Kazuhiro Osawa
 
Antenna Physical Characetristics
Antenna Physical CharacetristicsAntenna Physical Characetristics
Antenna Physical CharacetristicsAssignmentpedia
 
Studyx2
Studyx2Studyx2
Windows 8 Training Fundamental - 1
Windows 8 Training Fundamental - 1Windows 8 Training Fundamental - 1
Windows 8 Training Fundamental - 1Kevin Octavian
 
Aula 29/05 (AlarmManager)
Aula 29/05 (AlarmManager)Aula 29/05 (AlarmManager)
Aula 29/05 (AlarmManager)
Ricardo Longa
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 

What's hot (20)

Erlang assembly
Erlang assemblyErlang assembly
Erlang assembly
 
Data Storage
Data StorageData Storage
Data Storage
 
Jp notes
Jp notesJp notes
Jp notes
 
Control de acceso con excel
Control de acceso con excelControl de acceso con excel
Control de acceso con excel
 
Aula 6 - 08/05 (Menu)
Aula 6 - 08/05 (Menu)Aula 6 - 08/05 (Menu)
Aula 6 - 08/05 (Menu)
 
Annihilate test smells by refactoring to patterns
Annihilate test smells by refactoring to patternsAnnihilate test smells by refactoring to patterns
Annihilate test smells by refactoring to patterns
 
culadora cientifica en java
culadora cientifica en javaculadora cientifica en java
culadora cientifica en java
 
JDBC (JAVA Database Connectivity)
JDBC (JAVA Database Connectivity)JDBC (JAVA Database Connectivity)
JDBC (JAVA Database Connectivity)
 
Studyx4
Studyx4Studyx4
Studyx4
 
Java swing
Java swingJava swing
Java swing
 
Studyx1
Studyx1Studyx1
Studyx1
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
 
Yappo Groonga - with japanese search software history @ osdc.tw 2011
Yappo Groonga - with japanese search software history @ osdc.tw 2011Yappo Groonga - with japanese search software history @ osdc.tw 2011
Yappo Groonga - with japanese search software history @ osdc.tw 2011
 
Into Clojure
Into ClojureInto Clojure
Into Clojure
 
Antenna Physical Characetristics
Antenna Physical CharacetristicsAntenna Physical Characetristics
Antenna Physical Characetristics
 
Studyx2
Studyx2Studyx2
Studyx2
 
Windows 8 Training Fundamental - 1
Windows 8 Training Fundamental - 1Windows 8 Training Fundamental - 1
Windows 8 Training Fundamental - 1
 
Aula 29/05 (AlarmManager)
Aula 29/05 (AlarmManager)Aula 29/05 (AlarmManager)
Aula 29/05 (AlarmManager)
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
 

Similar to Design pattern - Iterator, Mediator and Memento

Imagine a world without mocks
Imagine a world without mocksImagine a world without mocks
Imagine a world without mocks
kenbot
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
Soluto
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
Iram Ramrajkar
 
Oleksandr Tolstykh
Oleksandr TolstykhOleksandr Tolstykh
Oleksandr Tolstykh
CodeFest
 
Converting Db Schema Into Uml Classes
Converting Db Schema Into Uml ClassesConverting Db Schema Into Uml Classes
Converting Db Schema Into Uml ClassesKaniska Mandal
 
This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdf
indiaartz
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdf
ShaiAlmog1
 
Design patterns in the 21st Century
Design patterns in the 21st CenturyDesign patterns in the 21st Century
Design patterns in the 21st CenturySamir Talwar
 
Initial UI Mockup - Part 3.pdf
Initial UI Mockup - Part 3.pdfInitial UI Mockup - Part 3.pdf
Initial UI Mockup - Part 3.pdf
ShaiAlmog1
 
Beautiful java script
Beautiful java scriptBeautiful java script
Beautiful java script
Ürgo Ringo
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEDarwin Durand
 
Creating a Facebook Clone - Part XI.pdf
Creating a Facebook Clone - Part XI.pdfCreating a Facebook Clone - Part XI.pdf
Creating a Facebook Clone - Part XI.pdf
ShaiAlmog1
 
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdfPLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
mohammedfootwear
 
Sustaining Test-Driven Development
Sustaining Test-Driven DevelopmentSustaining Test-Driven Development
Sustaining Test-Driven DevelopmentAgileOnTheBeach
 
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
solit
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
javatwo2011
 
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
Ali Parmaksiz
 

Similar to Design pattern - Iterator, Mediator and Memento (20)

Imagine a world without mocks
Imagine a world without mocksImagine a world without mocks
Imagine a world without mocks
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
 
Unit testing with mock libs
Unit testing with mock libsUnit testing with mock libs
Unit testing with mock libs
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
 
Oleksandr Tolstykh
Oleksandr TolstykhOleksandr Tolstykh
Oleksandr Tolstykh
 
Converting Db Schema Into Uml Classes
Converting Db Schema Into Uml ClassesConverting Db Schema Into Uml Classes
Converting Db Schema Into Uml Classes
 
Eclipse Tricks
Eclipse TricksEclipse Tricks
Eclipse Tricks
 
This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdf
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdf
 
Design patterns in the 21st Century
Design patterns in the 21st CenturyDesign patterns in the 21st Century
Design patterns in the 21st Century
 
Initial UI Mockup - Part 3.pdf
Initial UI Mockup - Part 3.pdfInitial UI Mockup - Part 3.pdf
Initial UI Mockup - Part 3.pdf
 
Beautiful java script
Beautiful java scriptBeautiful java script
Beautiful java script
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
 
Creating a Facebook Clone - Part XI.pdf
Creating a Facebook Clone - Part XI.pdfCreating a Facebook Clone - Part XI.pdf
Creating a Facebook Clone - Part XI.pdf
 
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdfPLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!i have to submit it before.pdf
 
Bar graph
Bar graphBar graph
Bar graph
 
Sustaining Test-Driven Development
Sustaining Test-Driven DevelopmentSustaining Test-Driven Development
Sustaining Test-Driven Development
 
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
 
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
 

More from Sean Tsai

What new in android studio 2.2
What new in android studio 2.2What new in android studio 2.2
What new in android studio 2.2
Sean Tsai
 
Effective Objective-C 2.0 (Item 1 - 7)
Effective Objective-C 2.0 (Item 1 - 7)Effective Objective-C 2.0 (Item 1 - 7)
Effective Objective-C 2.0 (Item 1 - 7)
Sean Tsai
 
Android testing
Android testingAndroid testing
Android testing
Sean Tsai
 
Extensible Messaging and Presence Protocol (XMPP)
Extensible Messaging and Presence Protocol (XMPP)Extensible Messaging and Presence Protocol (XMPP)
Extensible Messaging and Presence Protocol (XMPP)
Sean Tsai
 
Introduction of Google Tag Manager
Introduction of Google Tag ManagerIntroduction of Google Tag Manager
Introduction of Google Tag Manager
Sean Tsai
 
Google analytics
Google analyticsGoogle analytics
Google analytics
Sean Tsai
 
Dependency injection with koin
Dependency injection with koinDependency injection with koin
Dependency injection with koin
Sean Tsai
 
Kotlin - Coroutine
Kotlin - CoroutineKotlin - Coroutine
Kotlin - Coroutine
Sean Tsai
 

More from Sean Tsai (8)

What new in android studio 2.2
What new in android studio 2.2What new in android studio 2.2
What new in android studio 2.2
 
Effective Objective-C 2.0 (Item 1 - 7)
Effective Objective-C 2.0 (Item 1 - 7)Effective Objective-C 2.0 (Item 1 - 7)
Effective Objective-C 2.0 (Item 1 - 7)
 
Android testing
Android testingAndroid testing
Android testing
 
Extensible Messaging and Presence Protocol (XMPP)
Extensible Messaging and Presence Protocol (XMPP)Extensible Messaging and Presence Protocol (XMPP)
Extensible Messaging and Presence Protocol (XMPP)
 
Introduction of Google Tag Manager
Introduction of Google Tag ManagerIntroduction of Google Tag Manager
Introduction of Google Tag Manager
 
Google analytics
Google analyticsGoogle analytics
Google analytics
 
Dependency injection with koin
Dependency injection with koinDependency injection with koin
Dependency injection with koin
 
Kotlin - Coroutine
Kotlin - CoroutineKotlin - Coroutine
Kotlin - Coroutine
 

Recently uploaded

openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
Google
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 

Recently uploaded (20)

openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 

Design pattern - Iterator, Mediator and Memento

  • 2. Iterator Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Also knowns as Cursor
  • 3. Sample - McDonald’s + KFC Iterator
  • 4. Sample - McDonald’s + KFC McDonaldMenu mcDonaldMenu = new McDonaldMenu(); ArrayList breakfastItems = mcDonaldMenu.getMenuItems(); KFCMenu kfcMenu = new KFCMenu(); MenuItem[] lunchItems = dinerMenu.getMenuItems(); Iterator
  • 5. Sample - McDonald’s + KFC Iterator
  • 6. Sample - McDonald’s + KFC for (int i = 0; i < breakfastItems.size(); i++) { MenuItem menuItem = (MenuItem)breakfastItems.get(i); System.out.print(menuItem.getName() + “ “); System.out.println(menuItem.getPrice() + “ “); System.out.println(menuItem.getDescription()); } for (int i = 0; i < lunchItems.length; i++) { MenuItem menuItem = lunchItems[i]; System.out.print(menuItem.getName() + “ “); System.out.println(menuItem.getPrice() + “ “); System.out.println(menuItem.getDescription()); } Iterator
  • 8. Pros and Cons Pros: ● It supports variations in the traversal of an aggregate. ● Iterators simplify the Aggregate interface. Iterator
  • 9. Mediator Mediator pattern is used to reduce communication complexity between multiple objects or classes.
  • 10. Sample 1 - Unix System Mediator
  • 11. Sample 2 - Control Tower Mediator
  • 12. Sample 3 - Dialog box MediatorMediator
  • 14. Pros and Cons Pros: ● Limit subclassing and specialization. ● Decouples colleagues. Cons: ● Reducing the complexity of colleagues increases the complexity of the Mediator itself. Mediator
  • 15. Memento Memento pattern is used to restore state of an object to a previous state. Memento pattern falls under behavioral pattern category.
  • 17. Sample Code public class Memento { private String state; public Memento(String state) { this.state = state; } public String getState() { return state; } } Memento
  • 18. Sample Code public class Originator { private String state; public void setState(String state) { this.state = state; } public String getState() { return state; } public Memento saveStateToMemento() { return new Memento(state); } public void getStateFromMemento(Memento Memento) { state = Memento.getState(); } } Memento
  • 19. Sample Code Memento import java.util.ArrayList; import java.util.List; public class CareTaker { private List<Memento> mementoList = new ArrayList<Memento>(); public void add(Memento state){ mementoList.add(state); } public Memento get(int index){ return mementoList.get(index); } }
  • 20. Diagram Mediator public class MementoPatternDemo { public static void main(String[] args) { Originator originator = new Originator(); CareTaker careTaker = new CareTaker(); originator.setState("State #1"); originator.setState("State #2"); careTaker.add(originator.saveStateToMemento()); originator.setState("State #3"); careTaker.add(originator.saveStateToMemento()); originator.setState("State #4"); System.out.println("Current State: " + originator.getState()); originator.getStateFromMemento(careTaker.get(0)); System.out.println("First saved State: " + originator.getState()); originator.getStateFromMemento(careTaker.get(1)); System.out.println("Second saved State: " + originator.getState()); } }
  • 21. Sample Code Memento import java.util.ArrayList; import java.util.List; public class CareTaker { private List<Memento> mementoList = new ArrayList<Memento>(); public void add(Memento state){ mementoList.add(state); } public Memento get(int index){ return mementoList.get(index); } }
  • 22. Pros and Cons Memento Pros: ● The history is maintained without knowing what changed inside the object. Cons: ● Using mementos might be expensive. ● It may be difficult in some languages to ensure that only the originator can access the memento's state.
  • 23. End