Merge pull request #56 from KunwarVikas/stackandqueue
Java Design Patterns
This commit is contained in:
16
com/javadevjournal/design/creational/prototype/Circle.java
Normal file
16
com/javadevjournal/design/creational/prototype/Circle.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package javadevjournal.design.creational.prototype;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class Circle extends Shape {
|
||||||
|
|
||||||
|
public Circle() {
|
||||||
|
type = "Circle";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw() {
|
||||||
|
System.out.println("Circle's draw() method.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package javadevjournal.design.creational.prototype;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class PrototypePatternDemo {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ShapeCache.loadCache();
|
||||||
|
|
||||||
|
Shape clonedShape = (Shape) ShapeCache.getShape("1");
|
||||||
|
System.out.println("Shape : " + clonedShape.getType());
|
||||||
|
|
||||||
|
Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
|
||||||
|
System.out.println("Shape : " + clonedShape2.getType());
|
||||||
|
|
||||||
|
Shape clonedShape3 = (Shape) ShapeCache.getShape("3");
|
||||||
|
System.out.println("Shape : " + clonedShape3.getType());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package javadevjournal.design.creational.prototype;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class Rectangle extends Shape {
|
||||||
|
|
||||||
|
public Rectangle() {
|
||||||
|
type = "Rectangle";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw() {
|
||||||
|
System.out.println("Rectangle's draw() method.");
|
||||||
|
}
|
||||||
|
}
|
||||||
39
com/javadevjournal/design/creational/prototype/Shape.java
Normal file
39
com/javadevjournal/design/creational/prototype/Shape.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package javadevjournal.design.creational.prototype;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public abstract class Shape implements Cloneable {
|
||||||
|
private String id;
|
||||||
|
protected String type;
|
||||||
|
|
||||||
|
abstract void draw();
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use Object class's Clone() method to do the cloning.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Object clone() {
|
||||||
|
Object cloneObject = null;
|
||||||
|
try {
|
||||||
|
cloneObject = super.clone();
|
||||||
|
} catch (CloneNotSupportedException e) {
|
||||||
|
System.out.println("cloning failed");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return cloneObject;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package javadevjournal.design.creational.prototype;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ShapeCache {
|
||||||
|
private static Map<String, Shape> shapeMap = new HashMap<String, Shape>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return cloned object of Shape class implementing classes.
|
||||||
|
*
|
||||||
|
* @param shapeId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Shape getShape(String shapeId) {
|
||||||
|
Shape toBeClonedShape = shapeMap.get(shapeId);
|
||||||
|
return (Shape) toBeClonedShape.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In real-world applications, the loading of details will be from a Database.
|
||||||
|
* We are using a HaspMap to demonstrate the same behavior.
|
||||||
|
*/
|
||||||
|
public static void loadCache() {
|
||||||
|
Circle circle = new Circle();
|
||||||
|
circle.setId("1");
|
||||||
|
shapeMap.put(circle.getId(), circle);
|
||||||
|
|
||||||
|
Square square = new Square();
|
||||||
|
square.setId("2");
|
||||||
|
shapeMap.put(square.getId(), square);
|
||||||
|
|
||||||
|
Rectangle rectangle = new Rectangle();
|
||||||
|
rectangle.setId("3");
|
||||||
|
shapeMap.put(rectangle.getId(), rectangle);
|
||||||
|
}
|
||||||
|
}
|
||||||
16
com/javadevjournal/design/creational/prototype/Square.java
Normal file
16
com/javadevjournal/design/creational/prototype/Square.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package javadevjournal.design.creational.prototype;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class Square extends Shape {
|
||||||
|
|
||||||
|
public Square() {
|
||||||
|
type = "Square";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw() {
|
||||||
|
System.out.println("Square's draw() method.");
|
||||||
|
}
|
||||||
|
}
|
||||||
12
com/javadevjournal/design/structural/decorator/Circle.java
Normal file
12
com/javadevjournal/design/structural/decorator/Circle.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package javadevjournal.design.structural.decorator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class Circle implements Shape {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawShape() {
|
||||||
|
System.out.println("The shape is: Circle");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package javadevjournal.design.structural.decorator;// DecoratorPatternDemo.java
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class DecoratorPatternDemo {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Creating objects of Shape interface
|
||||||
|
Shape circle = new Circle();
|
||||||
|
Shape rectangle = new Rectangle();
|
||||||
|
|
||||||
|
// Creating objects of decorated classes
|
||||||
|
Shape redCircle = new RedShapeDecorator(new Circle());
|
||||||
|
Shape redRectangle = new RedShapeDecorator(new Rectangle());
|
||||||
|
|
||||||
|
System.out.println("Circle with normal fill");
|
||||||
|
circle.drawShape();
|
||||||
|
|
||||||
|
System.out.println("Rectangle with normal fill");
|
||||||
|
rectangle.drawShape();
|
||||||
|
|
||||||
|
System.out.println("\nCircle of red fill");
|
||||||
|
redCircle.drawShape();
|
||||||
|
|
||||||
|
System.out.println("\nRectangle of red fill");
|
||||||
|
redRectangle.drawShape();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package javadevjournal.design.structural.decorator;// Class 1
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class Rectangle implements Shape {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawShape() {
|
||||||
|
System.out.println("The shape is: Rectangle");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package javadevjournal.design.structural.decorator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class RedShapeDecorator extends ShapeDecorator {
|
||||||
|
|
||||||
|
public RedShapeDecorator(Shape decoratedShape) {
|
||||||
|
super(decoratedShape);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawShape() {
|
||||||
|
decoratedShape.drawShape();
|
||||||
|
//additional method to change the behavior of the shape object
|
||||||
|
shapeFill(decoratedShape);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will change the behavior of the shape object at runtime.
|
||||||
|
*
|
||||||
|
* @param decoratedShape
|
||||||
|
*/
|
||||||
|
private void shapeFill(Shape decoratedShape) {
|
||||||
|
System.out.println("Shape Fill color: Red");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package javadevjournal.design.structural.decorator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public interface Shape {
|
||||||
|
void drawShape();
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package javadevjournal.design.structural.decorator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public abstract class ShapeDecorator implements Shape {
|
||||||
|
|
||||||
|
//protected object of Shape Interface.
|
||||||
|
protected Shape decoratedShape;
|
||||||
|
|
||||||
|
public ShapeDecorator(Shape decoratedShape) {
|
||||||
|
this.decoratedShape = decoratedShape;
|
||||||
|
}
|
||||||
|
|
||||||
|
//calling the drawShape method on decoratedShape
|
||||||
|
public void drawShape() {
|
||||||
|
decoratedShape.drawShape();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package javadevjournal.design.structural.facade;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class FacadePatternClient {
|
||||||
|
private static int choice;
|
||||||
|
|
||||||
|
public static void main(String args[]) throws NumberFormatException, IOException {
|
||||||
|
do {
|
||||||
|
System.out.print("========= Mobile Shop ============ \n");
|
||||||
|
System.out.print("1. IPHONE. \n");
|
||||||
|
System.out.print("2. SAMSUNG. \n");
|
||||||
|
System.out.print("3. NOKIA. \n");
|
||||||
|
System.out.print("4. Exit. \n");
|
||||||
|
System.out.print("Enter your choice: ");
|
||||||
|
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
choice = Integer.parseInt(br.readLine());
|
||||||
|
ShopKeeper shopKeeper = new ShopKeeper();
|
||||||
|
|
||||||
|
switch (choice) {
|
||||||
|
case 1: {
|
||||||
|
shopKeeper.iphonePhoneSale();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 2: {
|
||||||
|
shopKeeper.samsungPhoneSale();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 3: {
|
||||||
|
shopKeeper.nokiaPhoneSale();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default: {
|
||||||
|
System.out.println("Nothing You purchased");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} while (choice != 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
16
com/javadevjournal/design/structural/facade/IMobileShop.java
Normal file
16
com/javadevjournal/design/structural/facade/IMobileShop.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package javadevjournal.design.structural.facade;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public interface IMobileShop {
|
||||||
|
/**
|
||||||
|
* Mobile Model Number
|
||||||
|
*/
|
||||||
|
public void getMobileModelNumber();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mobile Price
|
||||||
|
*/
|
||||||
|
public void getMobilePrice();
|
||||||
|
}
|
||||||
16
com/javadevjournal/design/structural/facade/Iphone.java
Normal file
16
com/javadevjournal/design/structural/facade/Iphone.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package javadevjournal.design.structural.facade;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class Iphone implements IMobileShop {
|
||||||
|
@Override
|
||||||
|
public void getMobileModelNumber() {
|
||||||
|
System.out.println("The model is: IPhone 13");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void getMobilePrice() {
|
||||||
|
System.out.println("The price is: 75000INR ");
|
||||||
|
}
|
||||||
|
}
|
||||||
16
com/javadevjournal/design/structural/facade/Nokia.java
Normal file
16
com/javadevjournal/design/structural/facade/Nokia.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package javadevjournal.design.structural.facade;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class Nokia implements IMobileShop {
|
||||||
|
@Override
|
||||||
|
public void getMobileModelNumber() {
|
||||||
|
System.out.println("The model is: Nokia 1100");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void getMobilePrice() {
|
||||||
|
System.out.println("The price is: 1500INR ");
|
||||||
|
}
|
||||||
|
}
|
||||||
16
com/javadevjournal/design/structural/facade/Samsung.java
Normal file
16
com/javadevjournal/design/structural/facade/Samsung.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package javadevjournal.design.structural.facade;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class Samsung implements IMobileShop {
|
||||||
|
@Override
|
||||||
|
public void getMobileModelNumber() {
|
||||||
|
System.out.println("The model is: Galaxy 11");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void getMobilePrice() {
|
||||||
|
System.out.println("The price is: 85000INR ");
|
||||||
|
}
|
||||||
|
}
|
||||||
34
com/javadevjournal/design/structural/facade/ShopKeeper.java
Normal file
34
com/javadevjournal/design/structural/facade/ShopKeeper.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package javadevjournal.design.structural.facade;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kunwar
|
||||||
|
*/
|
||||||
|
public class ShopKeeper {
|
||||||
|
private IMobileShop iphone;
|
||||||
|
private IMobileShop samsung;
|
||||||
|
private IMobileShop nokia;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* no args constructor
|
||||||
|
*/
|
||||||
|
public ShopKeeper() {
|
||||||
|
iphone = new Iphone();
|
||||||
|
samsung = new Samsung();
|
||||||
|
nokia = new Nokia();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void iphonePhoneSale() {
|
||||||
|
iphone.getMobileModelNumber();
|
||||||
|
iphone.getMobilePrice();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void samsungPhoneSale() {
|
||||||
|
samsung.getMobileModelNumber();
|
||||||
|
samsung.getMobilePrice();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void nokiaPhoneSale() {
|
||||||
|
nokia.getMobileModelNumber();
|
||||||
|
nokia.getMobilePrice();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user