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..833d912 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,32 @@ -# 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 Template Method Design Pattern +Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithms structure and sequence of execution. + +## Diagram +![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/template-method-pattern/diagrams/template-method-Pattern-class-diagram.jpeg "Diagram") + +![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/template-method-pattern/diagrams/Template-method-Design-Pattern-generic.jpeg "Diagram") + +![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/template-method-pattern/diagrams/template-method-sequence.png "Diagram") + +### When to use Template Method Design Pattern +The template method is used in frameworks, where each implements the invariant parts of a domain's architecture, leaving "placeholders" for customization options. + +### 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/Template-method-Design-Pattern-generic.jpeg b/diagrams/Template-method-Design-Pattern-generic.jpeg new file mode 100644 index 0000000..ff8839c Binary files /dev/null and b/diagrams/Template-method-Design-Pattern-generic.jpeg differ diff --git a/diagrams/template-method-Pattern-class-diagram.jpeg b/diagrams/template-method-Pattern-class-diagram.jpeg new file mode 100644 index 0000000..c890e57 Binary files /dev/null and b/diagrams/template-method-Pattern-class-diagram.jpeg differ diff --git a/diagrams/template-method-sequence.png b/diagrams/template-method-sequence.png new file mode 100644 index 0000000..ecbbaef Binary files /dev/null and b/diagrams/template-method-sequence.png 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/templateMethod/AbstractBaseJuice.java b/pattern/src/com/premaseem/templateMethod/AbstractBaseJuice.java new file mode 100644 index 0000000..09bc7ac --- /dev/null +++ b/pattern/src/com/premaseem/templateMethod/AbstractBaseJuice.java @@ -0,0 +1,112 @@ +package com.premaseem.templateMethod; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ + +public abstract class AbstractBaseJuice { + + /* + * This is template method which defines the sequence and steps for preparation. + */ + // Note: Sequence is important and should be modified by sub classes + final public void prepare() { + pickUpFruits(); + washFruits(); + pileFruits(); + cutFruits(); + addLiquid(); + operateJuicer(); + decorateJuiceHook(); + } + + // Variable methods would be implemented by sub classes + abstract String getFruitName(); + + abstract void pickUpFruits(); + + abstract void addLiquid(); + + // Reusable methods are defined in base class + void washFruits() { + System.out.println("Washing the fruit ... " + getFruitName()); + } + + void pileFruits() { + System.out.println("piling the fruit ... " + getFruitName()); + } + + void cutFruits() { + System.out.println("cutting the fruit ... " + getFruitName()); + } + + void operateJuicer() { + System.out.println("... operate juicer Ghur Ghur Ghur ... "); + } + + void decorateJuiceHook() { + System.out.println("add few dry fruits on top "); + } +} + +class AppleMilkJuice extends AbstractBaseJuice { + + @Override + String getFruitName() { + return " Apples "; + } + + @Override + void pickUpFruits() { + System.out.println(getFruitName() + " is picked up for Juice preparation "); + } + + @Override + void addLiquid() { + System.out.println("Add Milk in apple milk shake "); + } + + void decorateJuiceHook() { + System.out.println("decorate with Strawberry on top "); + } +} + +class OrangeJuice extends AbstractBaseJuice { + @Override + String getFruitName() { + return " Orange "; + } + + @Override + void pickUpFruits() { + System.out.println(getFruitName() + " is picked up for Juice prepration "); + } + + @Override + void addLiquid() { + System.out.println("Add some water in Orange juice "); + } + + void decorateJuiceHook() { + System.out.println("decorate with lime slice and straw "); + } +} + +class MixFruitJuice extends AbstractBaseJuice { + @Override + String getFruitName() { + return " Mixfruit "; + } + + @Override + void pickUpFruits() { + System.out.println(getFruitName() + " is picked up for Juice preparation "); + } + + @Override + void addLiquid() { + System.out.println("Adding some flavour "); + } +} diff --git a/pattern/src/com/premaseem/templateMethod/ClientForTemplateMethodJuicer.java b/pattern/src/com/premaseem/templateMethod/ClientForTemplateMethodJuicer.java new file mode 100644 index 0000000..e399f69 --- /dev/null +++ b/pattern/src/com/premaseem/templateMethod/ClientForTemplateMethodJuicer.java @@ -0,0 +1,55 @@ +package com.premaseem.templateMethod; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ + +import java.util.Scanner; + +public class ClientForTemplateMethodJuicer { + + public static void main(String[] args) { + + System.out.println("Welcome to Template Method Fruit Juice Extractor Program "); + + AbstractBaseJuice juice; + Scanner scan = new Scanner(System.in); + int repeatRunFlag = 1; + while (repeatRunFlag == 1) { + System.out.println("What fruit Juice would you like to have today "); + System.out.println(" Press 1 for Apple Milk Shake Juice"); + System.out.println(" Press 2 for Orange Juice "); + System.out.println(" Press 3 for Mix fruit Juice "); + + int type = scan.nextInt(); + + switch (type) { + case 1: + juice = new AppleMilkJuice(); + break; + case 2: + juice = new OrangeJuice(); + break; + case 3: + juice = new MixFruitJuice(); + break; + default: + juice = new MixFruitJuice(); + } + + juice.prepare(); + System.out.println("============================="); + + System.out.println("Press 1 to make more Juices ;-) and 0 for EXIT .... "); + try { + repeatRunFlag = scan.nextInt(); + } catch (Exception e) { + repeatRunFlag = 0; + } + } + System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n "); + + } +} diff --git a/pattern/src/com/premaseem/templateMethod/PatternDescription.txt b/pattern/src/com/premaseem/templateMethod/PatternDescription.txt new file mode 100644 index 0000000..9d20045 --- /dev/null +++ b/pattern/src/com/premaseem/templateMethod/PatternDescription.txt @@ -0,0 +1,27 @@ +Template Method Fruit Juice example defines a recipe (with exact steps and quantity) to prepare the drink which is kept in the extract class. However the flavor of the fruit Juice can be changed by it subclasses by overriding (which is defined as hooks/abstractor in parent abstract class). + +Intent: +Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. +Base class declares algorithm 'placeholders', and derived classes implement the placeholders. + + +Problem: +Two different components have significant similarities, but demonstrate no reuse of common interface or implementation. If a change common to both components becomes necessary, duplicate effort must be expended. + +Definition: +Template method design pattern is to define an algorithm as skeleton of operations and leave the details to be implemented by the child classes. The overall structure and sequence of the algorithm is preserved by the parent class. + + +The component designer decides which steps of an algorithm are invariant (or standard), and which are variant (or customizable). The invariant steps are implemented in an abstract base class, while the variant steps are either given a default implementation, or no implementation at all. The variant steps represent "hooks", or "placeholders", that can, or must, be supplied by the component's client in a concrete derived class. + +The component designer mandates the required steps of an algorithm, and the ordering of the steps, but allows the component client to extend or replace some number of these steps. + +Template Method is used prominently in frameworks. Each framework implements the invariant pieces of a domain's architecture, and defines "placeholders" for all necessary or interesting client customization options. In so doing, the framework becomes the "center of the universe", and the client customizations are simply "the third rock from the sun". This inverted control structure has been affectionately labelled "the Hollywood principle" - "don't call us, we'll call you". + + + +Rules of thumb: +Strategy is like Template Method except in its granularity. +Template Method uses inheritance to vary part of an algorithm. Strategy uses delegation to vary the entire algorithm. +Strategy modifies the logic of individual objects. Template Method modifies the logic of an entire class. +Factory Method is a specialization of Template Method. \ 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