diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..f06d999 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore index 6143e53..719dd1d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,10 @@ # Compiled class file +.idea/* *.class # Log file *.log - +*.iws # BlueJ files *.ctxt diff --git a/.project b/.project new file mode 100644 index 0000000..a33e785 --- /dev/null +++ b/.project @@ -0,0 +1,15 @@ + + + DesignPatternsJava9 + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.jdt.core.javanature + + diff --git a/DesignPatternsJava9.eml b/DesignPatternsJava9.eml new file mode 100644 index 0000000..c4aadb7 --- /dev/null +++ b/DesignPatternsJava9.eml @@ -0,0 +1,5 @@ + + + + + diff --git a/DesignPatternsJava9.iml b/DesignPatternsJava9.iml new file mode 100644 index 0000000..6d59135 --- /dev/null +++ b/DesignPatternsJava9.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/README.md b/README.md index 9e0034c..de17fa8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,25 @@ -# DesignPatternsJava9 -This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns. +# What is Memento Design Pattern +The memento pattern is a Behavioral design pattern that provides the ability to restore an object to its previous state (undo via rollback) + +## Diagram +![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/memento-pattern/diagrams/Memento-Pattern-generic.jpeg "Diagram") + +### Learn Design Patterns with Java by Aseem Jain +This repository contains working project code used in video Course by Packt Publication with title "Learn Design Patterns with Java " authored by "Aseem Jain". + +### Course link: +https://www.packtpub.com/application-development/learn-design-patterns-java-9-video + +### ![ http://in.linkedin.com/in/premaseem](https://github.com/premaseem/DesignPatternsJava9/blob/master/linkedin.png "http://in.linkedin.com/in/premaseem") Profile: http://in.linkedin.com/in/premaseem + +### Authors blog on design patterns: +https://premaseem.wordpress.com/category/computers/design-patterns/ + +### Software Design pattern community face book page: +https://www.facebook.com/DesignPatternGuru/ + +### Note: +* This code base will work on Java 9 and above versions. +* `diagrams` folders carry UML diagrams. +* `pattern` folder has code of primary example. +* `patternBonus` folder has code of secondary or bonus example. diff --git a/diagrams/Memento-Pattern-generic.jpeg b/diagrams/Memento-Pattern-generic.jpeg new file mode 100644 index 0000000..4c796a0 Binary files /dev/null and b/diagrams/Memento-Pattern-generic.jpeg differ diff --git a/pattern/pattern.iml b/pattern/pattern.iml new file mode 100644 index 0000000..e6d0514 --- /dev/null +++ b/pattern/pattern.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/pattern/src/com/premaseem/memento/videoGame/CareTaker.java b/pattern/src/com/premaseem/memento/videoGame/CareTaker.java new file mode 100644 index 0000000..b34d2ca --- /dev/null +++ b/pattern/src/com/premaseem/memento/videoGame/CareTaker.java @@ -0,0 +1,34 @@ +package com.premaseem.memento.videoGame; +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ + +import java.util.HashMap; +import java.util.Map; + +/* + * Store Room + */ +public class CareTaker { + Map restorePoints = new HashMap(); + Originator Originator = new Originator(); + + public void saveGame(String restorePointName, GamePlayer player){ + Memento memento = Originator.saveMemento(player); + restorePoints.put(restorePointName, memento); + } + + public void restoreGame(String restorePointName, GamePlayer player){ + GamePlayer playerSnapShot = null; + Memento memento = restorePoints.get(restorePointName); + if(memento==null){ + System.out.printf(" %s Restore point name is no correct \n",restorePointName); + }else{ + playerSnapShot = memento.getPlayerSnapShot(); + Originator.restorePlayerSnapshot(player, playerSnapShot); + System.out.printf(" %s Restore point is applied successfully \n ",restorePointName); + } + } +} \ No newline at end of file diff --git a/pattern/src/com/premaseem/memento/videoGame/ClientFile.java b/pattern/src/com/premaseem/memento/videoGame/ClientFile.java new file mode 100644 index 0000000..a5fbce6 --- /dev/null +++ b/pattern/src/com/premaseem/memento/videoGame/ClientFile.java @@ -0,0 +1,61 @@ +package com.premaseem.memento.videoGame; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ + +import java.util.Scanner; + +public class ClientFile { + + public static void main(String[] args) { + + System.out.println("Welcome to Memento Pattern Video Game "); + Scanner scan = new Scanner(System.in); + + CareTaker careTaker = new CareTaker(); + GamePlayer player = new GamePlayer(); + + System.out.println("Please enter your player name ... "); + player.setPlayerName(scan.next()); + + String repeatRunFlag = "yes"; + while (!repeatRunFlag.equalsIgnoreCase("no")) { + + System.out.println("Please enter your players Mask name eg. Mario, Batman, SuperMan "); + player.setMaskPicked(scan.next()); + + System.out.println("Game started - points gained and level changed "); + System.out.println("current state of player " + player); + + player.increase100Points(); + System.out.println("Current state of player " + player); + + System.out.println("Do you want to save the state of your game? Enter restore point name "); + careTaker.saveGame(scan.next(), player); + + player.increase100Points(); + System.out.println("Game Over :-( "); + System.out.println(player); + + System.out.println("Do you want to restore your game? Enter restore point "); + careTaker.restoreGame(scan.next(), player); + + System.out.println("Restored state of player to previous state " + player); + + System.out.println("============================="); + + System.out.println("Press No to Exit and any other character to repeat .... "); + try { + repeatRunFlag = scan.next(); + } catch (Exception e) { + repeatRunFlag = "no"; + } + } + + System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n "); + System.out.println("\n $$$$$$$$$$$$$$$$$$$$$ www.premaseem.me $$$$$$$$$$$$$$$$$$$$$$ \n "); + } +} diff --git a/pattern/src/com/premaseem/memento/videoGame/GamePlayer.java b/pattern/src/com/premaseem/memento/videoGame/GamePlayer.java new file mode 100644 index 0000000..71a0629 --- /dev/null +++ b/pattern/src/com/premaseem/memento/videoGame/GamePlayer.java @@ -0,0 +1,67 @@ +package com.premaseem.memento.videoGame; +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ + +public class GamePlayer implements Cloneable { + + String playerName; + String maskPicked="premAseem"; + Integer level=1; + Integer totalPoints=100; + + String getPlayerName() { + return playerName; + } + + void setPlayerName(String playerName) { + this.playerName = playerName; + } + + String getMaskPicked() { + return maskPicked; + } + + void setMaskPicked(String maskPicked) { + this.maskPicked = maskPicked; + } + + Integer getLevel() { + return level; + } + + void setLevel(Integer level) { + this.level = level; + } + + Integer getTotalPoints() { + return totalPoints; + } + + void setTotalPoints(Integer totalPoints) { + this.totalPoints = totalPoints; + } + + void increase100Points(){ + totalPoints+=100; + level+=1; + } + + + + @Override + protected Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + @Override + public String toString() { + System.out.printf(" name: %s - Mask: %s - Level: %d - Points : %d ",playerName,maskPicked,level,totalPoints); + return super.toString(); + } + + + +} diff --git a/pattern/src/com/premaseem/memento/videoGame/Memento.java b/pattern/src/com/premaseem/memento/videoGame/Memento.java new file mode 100644 index 0000000..421c761 --- /dev/null +++ b/pattern/src/com/premaseem/memento/videoGame/Memento.java @@ -0,0 +1,21 @@ +package com.premaseem.memento.videoGame; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ + +public class Memento { + private GamePlayer player; + + public Memento(GamePlayer player) { + super(); + this.player = player; + } + + GamePlayer getPlayerSnapShot(){ + return player; + } + +} \ No newline at end of file diff --git a/pattern/src/com/premaseem/memento/videoGame/Originator.java b/pattern/src/com/premaseem/memento/videoGame/Originator.java new file mode 100644 index 0000000..3eaea7e --- /dev/null +++ b/pattern/src/com/premaseem/memento/videoGame/Originator.java @@ -0,0 +1,36 @@ +package com.premaseem.memento.videoGame; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ + +public class Originator { + GamePlayer player; + + Memento saveMemento(GamePlayer player){ + GamePlayer playerSnapShot = createPlayerSnapshot(player); + Memento memento = new Memento(playerSnapShot); + return memento; + } + + private GamePlayer createPlayerSnapshot(GamePlayer player) { + GamePlayer playerSnapShot=null; + try { + playerSnapShot = (GamePlayer) player.clone(); + } catch (CloneNotSupportedException e) { + + e.printStackTrace(); + } + + return playerSnapShot; + } + + void restorePlayerSnapshot(GamePlayer playerSnapShot,GamePlayer player) { + playerSnapShot.setMaskPicked(player.getMaskPicked()); + playerSnapShot.setLevel(player.getLevel()); + playerSnapShot.setTotalPoints(player.getTotalPoints()); + playerSnapShot.setPlayerName(player.getPlayerName()); + } +} \ No newline at end of file diff --git a/pattern/src/module-info.java b/pattern/src/module-info.java new file mode 100644 index 0000000..939a84b --- /dev/null +++ b/pattern/src/module-info.java @@ -0,0 +1,8 @@ +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +module pattern { +} \ No newline at end of file diff --git a/patternBonus/patternBonus.iml b/patternBonus/patternBonus.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/patternBonus/patternBonus.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/patternBonus/src/module-info.java b/patternBonus/src/module-info.java new file mode 100644 index 0000000..e41318b --- /dev/null +++ b/patternBonus/src/module-info.java @@ -0,0 +1,7 @@ +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ +module patternBonus { +} \ No newline at end of file