├── .project
├── README.md
└── src
└── com
└── vish
└── pattern
├── abstrct
└── factory
│ ├── AbstractFactory.java
│ ├── Blue.java
│ ├── Circle.java
│ ├── Color.java
│ ├── Colorfactory.java
│ ├── DemoAbstractFactory.java
│ ├── FactoryProducer.java
│ ├── Green.java
│ ├── Rectangle.java
│ ├── Red.java
│ ├── Shape.java
│ ├── Shapefactory.java
│ ├── Square.java
│ └── content.xt
├── adapter
├── AdapterDemo.java
├── AdvanceMediaPlayer.java
├── AudioPlayer.java
├── MediaAdapter.java
├── MediaPlayer.java
├── Mp4Player.java
├── VlcPlayer.java
└── content.txt
├── bridge
├── BridgePatternDemo.java
├── Circle.java
├── GreenColor.java
├── IDraw.java
├── Rectangle.java
├── RedColor.java
└── Shape.java
├── builder
├── Bottle.java
├── BuilderPatternDemo.java
├── Burger.java
├── ChickenBurger.java
├── Coke.java
├── ColdDrink.java
├── Item.java
├── Meal.java
├── MealBuilder.java
├── Packing.java
├── Pepsi.java
├── VegBurger.java
├── Wrapper.java
└── content.txt
├── factory
├── Currency.java
├── CurrencyFactory.java
├── Factory.java
├── Rupee.java
├── SGDollar.java
└── USDollar.java
├── prototype
├── Circle.java
├── PrototypeDemo.java
├── Rectangle.java
├── Shape.java
├── ShapeCache.java
├── Square.java
└── content.txt
└── singleton
├── BillPughSingleton.java
├── DoubleCheckedSingleton.java
├── EagerSingleton.java
├── EnumSinleton.java
├── LazySingleton.java
├── ReflectionSingletonTest.java
├── SerializedSingleton.java
├── SingletonSerialzeTest.java
├── StaticBlockSingleton.java
└── ThreadSafeSingleton.java
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Design-Patterns
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | fr.obeo.dsl.viewpoint.nature.modelingproject
16 | org.eclipse.jdt.core.javanature
17 |
18 |
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Design-Patterns
2 | ===============
3 |
4 | Contains Details description of various design patter in java and Use-case of them
5 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/AbstractFactory.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public abstract class AbstractFactory {
4 | abstract Color getColor(String color);
5 | abstract Shape getShape(String shape) ;
6 | }
7 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/Blue.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public class Blue implements Color{
4 |
5 | @Override
6 | public void fill() {
7 | // TODO Auto-generated method stub
8 | System.out.println("Inside Blue::fill() method.");
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/Circle.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public class Circle implements Shape{
4 |
5 | @Override
6 | public void draw() {
7 | // TODO Auto-generated method stub
8 | System.out.println("Inside Circle::draw() method.");
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/Color.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public interface Color {
4 | void fill();
5 | }
6 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/Colorfactory.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public class Colorfactory extends AbstractFactory {
4 |
5 | @Override
6 | Color getColor(String color) {
7 | if (color == null) {
8 | return null;
9 | }
10 | if (color.equalsIgnoreCase("RED")) {
11 | return new Red();
12 | } else if (color.equalsIgnoreCase("GREEN")) {
13 | return new Green();
14 | } else if (color.equalsIgnoreCase("BLUE")) {
15 | return new Blue();
16 | }
17 | return null;
18 | }
19 |
20 | @Override
21 | Shape getShape(String shape) {
22 | // TODO Auto-generated method stub
23 | return null;
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/DemoAbstractFactory.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public class DemoAbstractFactory {
4 | public static void main(String[] args) {
5 | // get shape factory
6 | AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
7 | // get an object of Shape Circle
8 | Shape shape1 = shapeFactory.getShape("CIRCLE");
9 | // call draw method of Shape Circle
10 | shape1.draw();
11 | // get an object of Shape Rectangle
12 | Shape shape2 = shapeFactory.getShape("RECTANGLE");
13 | // call draw method of Shape Rectangle
14 | shape2.draw();
15 | // get an object of Shape Square
16 | Shape shape3 = shapeFactory.getShape("SQUARE");
17 | // call draw method of Shape Square
18 | shape3.draw();
19 | // get color factory
20 | AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
21 | // get an object of Color Red
22 | Color color1 = colorFactory.getColor("RED");
23 | // call fill method of Red
24 | color1.fill();
25 | // get an object of Color Green
26 | Color color2 = colorFactory.getColor("Green");
27 | // call fill method of Green
28 | color2.fill();
29 | // get an object of Color Blue
30 | Color color3 = colorFactory.getColor("BLUE");
31 | //call fill method of Color Blue
32 | color3.fill();
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/FactoryProducer.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public class FactoryProducer {
4 | public static AbstractFactory getFactory(String choice) {
5 | if (choice.equalsIgnoreCase("SHAPE")) {
6 | return new Shapefactory();
7 | } else if (choice.equalsIgnoreCase("COLOR")) {
8 | return new Colorfactory();
9 | }
10 | return null;
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/Green.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public class Green implements Color{
4 |
5 | @Override
6 | public void fill() {
7 | // TODO Auto-generated method stub
8 | System.out.println("Inside Green::fill() method.");
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/Rectangle.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public class Rectangle implements Shape{
4 |
5 | @Override
6 | public void draw() {
7 | // TODO Auto-generated method stub
8 | System.out.println("Inside Rectangle::draw() method.");
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/Red.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public class Red implements Color{
4 |
5 | @Override
6 | public void fill() {
7 | // TODO Auto-generated method stub
8 | System.out.println("Inside Red::fill() method.");
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/Shape.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public interface Shape {
4 | void draw();
5 | }
6 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/Shapefactory.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public class Shapefactory extends AbstractFactory {
4 |
5 | @Override
6 | Color getColor(String color) {
7 | // TODO Auto-generated method stub
8 | return null;
9 | }
10 |
11 | @Override
12 | public Shape getShape(String shapeType) {
13 | if (shapeType == null) {
14 | return null;
15 | }
16 | if (shapeType.equalsIgnoreCase("CIRCLE")) {
17 | return new Circle();
18 | } else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
19 | return new Rectangle();
20 | } else if (shapeType.equalsIgnoreCase("SQUARE")) {
21 | return new Square();
22 | }
23 | return null;
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/Square.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.abstrct.factory;
2 |
3 | public class Square implements Shape{
4 |
5 | @Override
6 | public void draw() {
7 | // TODO Auto-generated method stub
8 | System.out.println("Inside Square::draw() method.");
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/abstrct/factory/content.xt:
--------------------------------------------------------------------------------
1 | Abstract Factory patterns works around a super-factory which creates other factories. This factory is also called as
2 | Factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the
3 | best ways to create an object.
4 | In Abstract Factory pattern an interface is responsible for creating a factory of related objects,
5 | without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern.
--------------------------------------------------------------------------------
/src/com/vish/pattern/adapter/AdapterDemo.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.adapter;
2 |
3 | public class AdapterDemo {
4 | public static void main(String[] args) {
5 | AudioPlayer audioPlayer = new AudioPlayer();
6 | audioPlayer.play("mp3", "beyond the horizon.mp3");
7 | audioPlayer.play("mp4", "alone.mp4");
8 | audioPlayer.play("vlc", "far far away.vlc");
9 | audioPlayer.play("avi", "mind me.avi");
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/adapter/AdvanceMediaPlayer.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.adapter;
2 |
3 | public interface AdvanceMediaPlayer {
4 | public void playVlc(String fileName);
5 |
6 | public void playMp4(String fileName);
7 | }
8 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/adapter/AudioPlayer.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.adapter;
2 |
3 | public class AudioPlayer implements MediaPlayer {
4 | MediaAdapter mediaAdapter;
5 |
6 | @Override
7 | public void play(String audioType, String fileName) { // inbuilt support to
8 | // play mp3 music
9 | // files
10 | if (audioType.equalsIgnoreCase("mp3")) {
11 | System.out.println("Playing mp3 file. Name: " + fileName);
12 | }
13 | // mediaAdapter is providing support to play other file formats
14 | else if (audioType.equalsIgnoreCase("vlc")
15 | || audioType.equalsIgnoreCase("mp4")) {
16 | mediaAdapter = new MediaAdapter(audioType);
17 | mediaAdapter.play(audioType, fileName);
18 | } else {
19 | System.out.println("Invalid media. " + audioType
20 | + " format not supported");
21 | }
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/adapter/MediaAdapter.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.adapter;
2 |
3 | public class MediaAdapter implements MediaPlayer{
4 | AdvanceMediaPlayer advancedMusicPlayer;
5 |
6 | public MediaAdapter(String audioType) {
7 | if (audioType.equalsIgnoreCase("vlc")) {
8 | advancedMusicPlayer = new VlcPlayer();
9 | } else if (audioType.equalsIgnoreCase("mp4")) {
10 | advancedMusicPlayer = new Mp4Player();
11 | }
12 | }
13 |
14 | @Override
15 | public void play(String audioType, String fileName) {
16 | if (audioType.equalsIgnoreCase("vlc")) {
17 | advancedMusicPlayer.playVlc(fileName);
18 | } else if (audioType.equalsIgnoreCase("mp4")) {
19 | advancedMusicPlayer.playMp4(fileName);
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/adapter/MediaPlayer.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.adapter;
2 |
3 | public interface MediaPlayer {
4 | public void play(String audioType, String fileName);
5 | }
6 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/adapter/Mp4Player.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.adapter;
2 |
3 | public class Mp4Player implements AdvanceMediaPlayer{
4 |
5 |
6 | public void playVlc(String fileName) {
7 | // TODO Auto-generated method stub
8 |
9 | }
10 |
11 |
12 | public void playMp4(String fileName) {
13 | // TODO Auto-generated method stub
14 | System.out.println("Playing mp4 file. Name: "+ fileName);
15 | }
16 |
17 | }
--------------------------------------------------------------------------------
/src/com/vish/pattern/adapter/VlcPlayer.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.adapter;
2 |
3 | public class VlcPlayer implements AdvanceMediaPlayer{
4 |
5 |
6 | public void playVlc(String fileName) {
7 | // TODO Auto-generated method stub
8 | System.out.println("Playing Vlc file. Name: "+ fileName);
9 | }
10 |
11 |
12 | public void playMp4(String fileName) {
13 | // TODO Auto-generated method stub
14 |
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/adapter/content.txt:
--------------------------------------------------------------------------------
1 | Adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under structural
2 | pattern as this pattern combines the capability of two independent interfaces.
3 |
4 | This pattern involves a single class which is responsible to join functionalities of independent or incompatible
5 | interfaces. A real life example could be a case of card reader which acts as an adapter between memory card and a
6 | laptop.
7 | You plugins the memory card into card reader and card reader into the laptop so that memory card can be read
8 | via laptop. We are demonstrating use of Adapter pattern via following example in which an audio player device
9 | can play mp3 files only and wants to use an advanced audio player capable of playing vlc and mp4 files.
--------------------------------------------------------------------------------
/src/com/vish/pattern/bridge/BridgePatternDemo.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.vish.pattern.bridge;
5 |
6 | /**
7 | * @author Vishnu
8 | *
9 | */
10 | public class BridgePatternDemo {
11 | public static void main(String[] args) {
12 | Shape rect = new Rectangle(new RedColor());
13 | rect.draw();
14 |
15 | Shape circle = new Circle(new GreenColor());
16 | circle.draw();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/bridge/Circle.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.vish.pattern.bridge;
5 |
6 | /**
7 | * @author Vishnu
8 | *
9 | */
10 | public class Circle extends Shape {
11 |
12 | /**
13 | * @param drawAPI
14 | */
15 | protected Circle(IDraw iDraw) {
16 | super(iDraw);
17 | // TODO Auto-generated constructor stub
18 | }
19 |
20 | /* (non-Javadoc)
21 | * @see com.vish.pattern.bridge.Shape#draw()
22 | */
23 | @Override
24 | public void draw() {
25 | // TODO Auto-generated method stub
26 | iDraw.drawColor();
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/bridge/GreenColor.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.vish.pattern.bridge;
5 |
6 | /**
7 | * @author Vishnu
8 | *
9 | */
10 | public class GreenColor implements IDraw{
11 |
12 | /* (non-Javadoc)
13 | * @see com.vish.pattern.bridge.IDraw#drawColor()
14 | */
15 | @Override
16 | public void drawColor() {
17 | // TODO Auto-generated method stub
18 | System.out.println("Green Color");
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/bridge/IDraw.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.vish.pattern.bridge;
5 |
6 | /**
7 | * @author Vishnu
8 | *
9 | */
10 | public interface IDraw {
11 | public void drawColor( );
12 | }
13 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/bridge/Rectangle.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.vish.pattern.bridge;
5 |
6 | /**
7 | * @author Vishnu
8 | *
9 | */
10 | public class Rectangle extends Shape {
11 |
12 | /**
13 | * @param drawAPI
14 | */
15 | protected Rectangle(IDraw iDraw) {
16 | super(iDraw);
17 | // TODO Auto-generated constructor stub
18 | }
19 |
20 | /* (non-Javadoc)
21 | * @see com.vish.pattern.bridge.Shape#draw()
22 | */
23 | @Override
24 | public void draw() {
25 | // TODO Auto-generated method stub
26 | iDraw.drawColor();
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/bridge/RedColor.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.vish.pattern.bridge;
5 |
6 | /**
7 | * @author Vishnu
8 | *
9 | */
10 | public class RedColor implements IDraw{
11 |
12 | /* (non-Javadoc)
13 | * @see com.vish.pattern.bridge.IDraw#drawColor()
14 | */
15 | @Override
16 | public void drawColor() {
17 | // TODO Auto-generated method stub
18 | System.out.println("Red Color");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/bridge/Shape.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package com.vish.pattern.bridge;
5 |
6 | /**
7 | * @author Vishnu
8 | *
9 | */
10 | public abstract class Shape {
11 | protected IDraw iDraw;
12 |
13 | protected Shape(IDraw drawAPI) {
14 | this.iDraw = drawAPI;
15 | }
16 | public abstract void draw();
17 | }
18 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/Bottle.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | public class Bottle implements Packing{
4 |
5 | public String pack() {
6 | // TODO Auto-generated method stub
7 | return "bottle";
8 | }
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/BuilderPatternDemo.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | public class BuilderPatternDemo {
4 | public static void main(String[] args) {
5 | MealBuilder mealBuilder = new MealBuilder();
6 | Meal vegMeal = mealBuilder.prepareVegMeal();
7 | System.out.println("Veg Meal");
8 | vegMeal.showItems();
9 | System.out.println("Total Cost: " + vegMeal.getCost());
10 | Meal nonVegMeal = mealBuilder.prepareNonVegMeal();
11 | System.out.println("\n\nNon-Veg Meal");
12 | nonVegMeal.showItems();
13 | System.out.println("Total Cost: " + nonVegMeal.getCost());
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/Burger.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | public abstract class Burger implements Item {
4 | @Override
5 | public Packing packing() {
6 | return new Wrapper();
7 | }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/ChickenBurger.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | public class ChickenBurger extends Burger{
4 | public float price() {
5 | return 25.0f;
6 | }
7 | public String name() {
8 | return "Chicken Burger";
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/Coke.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | public class Coke extends ColdDrink{
4 | public float price() {
5 | return 25.0f;
6 | }
7 | public String name() {
8 | return "Coke";
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/ColdDrink.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | public abstract class ColdDrink implements Item {
4 | @Override
5 | public Packing packing() {
6 | return new Bottle();
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/Item.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | public interface Item {
4 | public String name();
5 |
6 | public Packing packing();
7 |
8 | public float price();
9 | }
10 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/Meal.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class Meal {
7 | private List- items = new ArrayList
- ();
8 |
9 | public void addItem(Item item) {
10 | items.add(item);
11 | }
12 |
13 | public float getCost() {
14 | float cost = 0.0f;
15 | for (Item item : items) {
16 | cost += item.price();
17 | }
18 | return cost;
19 | }
20 |
21 | public void showItems() {
22 | for (Item item : items) {
23 | System.out.print("Item : " + item.name());
24 | System.out.print(", Packing : " + item.packing().pack());
25 | System.out.println(", Price : " + item.price());
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/MealBuilder.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | public class MealBuilder {
4 | public Meal prepareVegMeal() {
5 | Meal meal = new Meal();
6 | meal.addItem(new VegBurger());
7 | meal.addItem(new Coke());
8 | return meal;
9 | }
10 |
11 | public Meal prepareNonVegMeal() {
12 | Meal meal = new Meal();
13 | meal.addItem(new ChickenBurger());
14 | meal.addItem(new Pepsi());
15 | return meal;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/Packing.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | public interface Packing {
4 | public String pack();
5 | }
6 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/Pepsi.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | public class Pepsi extends ColdDrink{
4 | public float price() {
5 | return 25.0f;
6 | }
7 | public String name() {
8 | return "Pepsi";
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/VegBurger.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | public class VegBurger extends Burger {
4 |
5 | public float price() {
6 | return 25.0f;
7 | }
8 |
9 | public String name() {
10 | return "Veg Burger";
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/Wrapper.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.builder;
2 |
3 | public class Wrapper implements Packing{
4 |
5 |
6 | public String pack() {
7 | // TODO Auto-generated method stub
8 | return "wrapper";
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/builder/content.txt:
--------------------------------------------------------------------------------
1 | Builder pattern builds a complex object using simple objects and using a step by step approach. T
2 | his type of design pattern comes under creational pattern as this pattern provides one of the best ways to
3 | create an object.
--------------------------------------------------------------------------------
/src/com/vish/pattern/factory/Currency.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.factory;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public interface Currency {
8 | String getSymbol();
9 | }
10 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/factory/CurrencyFactory.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.factory;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public class CurrencyFactory {
8 | public static Currency createCurrency(String country) {
9 | if (country.equalsIgnoreCase("India")) {
10 | return new Rupee();
11 | } else if (country.equalsIgnoreCase("Singapore")) {
12 | return new SGDollar();
13 | } else if (country.equalsIgnoreCase("US")) {
14 | return new USDollar();
15 | }
16 | throw new IllegalArgumentException("No such currency");
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/factory/Factory.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.factory;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public class Factory {
8 | public static void main(String args[]) {
9 | Currency currency = CurrencyFactory.createCurrency("India");
10 | System.out.println(currency.getSymbol());
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/factory/Rupee.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.factory;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public class Rupee implements Currency{
8 |
9 | @Override
10 | public String getSymbol() {
11 | return "Rs.";
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/factory/SGDollar.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.factory;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public class SGDollar implements Currency{
8 |
9 | @Override
10 | public String getSymbol() {
11 | return "SGD";
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/factory/USDollar.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.factory;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public class USDollar implements Currency{
8 |
9 | @Override
10 | public String getSymbol() {
11 | return "USD";
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/prototype/Circle.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.prototype;
2 |
3 | public class Circle extends Shape{
4 | public Circle() {
5 | type = "Circle";
6 | }
7 |
8 | @Override
9 | public void draw() {
10 | System.out.println("Inside Circle::draw() method.");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/prototype/PrototypeDemo.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.prototype;
2 |
3 | public class PrototypeDemo {
4 | public static void main(String[] args) {
5 | ShapeCache.loadCache();
6 | Shape clonedShape = (Shape) ShapeCache.getShape("1");
7 | System.out.println("Shape : " + clonedShape.getType());
8 | Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
9 | System.out.println("Shape : " + clonedShape2.getType());
10 | Shape clonedShape3 = (Shape) ShapeCache.getShape("3");
11 | System.out.println("Shape : " + clonedShape3.getType());
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/prototype/Rectangle.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.prototype;
2 |
3 | public class Rectangle extends Shape {
4 |
5 | public Rectangle() {
6 | type = "Rectangle";
7 | }
8 |
9 | @Override
10 | public void draw() {
11 | System.out.println("Inside Rectangle::draw() method.");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/prototype/Shape.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.prototype;
2 |
3 | public abstract class Shape implements Cloneable {
4 | private String id;
5 | protected String type;
6 |
7 | abstract void draw();
8 |
9 | public String getType() {
10 | return type;
11 | }
12 |
13 | public String getId() {
14 | return id;
15 | }
16 |
17 | public void setId(String id) {
18 | this.id = id;
19 | }
20 |
21 | public Object clone() {
22 | Object clone = null;
23 | try {
24 | clone = super.clone();
25 | } catch (CloneNotSupportedException e) {
26 | e.printStackTrace();
27 | }
28 | return clone;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/prototype/ShapeCache.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.prototype;
2 |
3 | import java.util.Hashtable;
4 |
5 | public class ShapeCache {
6 | private static Hashtable shapeMap = new Hashtable();
7 |
8 | public static Shape getShape(String shapeId) {
9 | Shape cachedShape = shapeMap.get(shapeId);
10 | return (Shape) cachedShape.clone();
11 | }
12 |
13 | // for each shape run database query and create shape //
14 | // shapeMap.put(shapeKey, shape); // for example, we are adding three
15 | // shapes
16 | public static void loadCache() {
17 | Circle circle = new Circle();
18 | circle.setId("1");
19 | shapeMap.put(circle.getId(), circle);
20 |
21 | Square square = new Square();
22 | square.setId("2");
23 | shapeMap.put(square.getId(), square);
24 |
25 | Rectangle rectangle = new Rectangle();
26 | rectangle.setId("3");
27 | shapeMap.put(rectangle.getId(), rectangle);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/prototype/Square.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.prototype;
2 |
3 | public class Square extends Shape{
4 | public Square() {
5 | type = "Square";
6 | }
7 |
8 | @Override
9 | public void draw() {
10 | System.out.println("Inside Square::draw() method.");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/prototype/content.txt:
--------------------------------------------------------------------------------
1 | Prototype pattern refers to creating duplicate object while keeping performance in mind.
2 | This type of design pattern comes under creational pattern as this pattern provides one of the best way to
3 | create an object.
--------------------------------------------------------------------------------
/src/com/vish/pattern/singleton/BillPughSingleton.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.singleton;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public class BillPughSingleton {
8 | private BillPughSingleton(){}
9 | private static class SingletonHelper{
10 | private static final BillPughSingleton INSTANCE = new BillPughSingleton();
11 | }
12 | public static BillPughSingleton getInstance(){
13 | return SingletonHelper.INSTANCE;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/singleton/DoubleCheckedSingleton.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.singleton;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public class DoubleCheckedSingleton {
8 | private static DoubleCheckedSingleton instance;
9 |
10 | private DoubleCheckedSingleton(){}
11 |
12 | public static DoubleCheckedSingleton getInstance(){
13 | if(instance == null){
14 | synchronized (ThreadSafeSingleton.class) {
15 | if(instance == null)
16 | instance = new DoubleCheckedSingleton();
17 | }
18 | }
19 | return instance;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/singleton/EagerSingleton.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.singleton;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public class EagerSingleton {
8 | private static EagerSingleton instance = new EagerSingleton();
9 |
10 | private EagerSingleton() {
11 | }
12 |
13 | /**
14 | * @return
15 | */
16 | public static EagerSingleton getInstance() {
17 | return instance;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/singleton/EnumSinleton.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.singleton;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public enum EnumSinleton {
8 | INSTANCE;
9 | public void someMethod(String param) {
10 | // some class member
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/singleton/LazySingleton.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.singleton;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public class LazySingleton {
8 | private static LazySingleton instance;
9 |
10 | private LazySingleton(){}
11 |
12 | public static LazySingleton getInstance(){
13 | if(instance == null){
14 | instance = new LazySingleton();
15 | }
16 | return instance;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/singleton/ReflectionSingletonTest.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.singleton;
2 |
3 | import java.lang.reflect.Constructor;
4 |
5 | /**
6 | * @author Vishnu
7 | *
8 | */
9 | public class ReflectionSingletonTest {
10 | public static void main(String[] args) {
11 | EagerSingleton instanceOne = EagerSingleton.getInstance();
12 | EagerSingleton instanceTwo = null;
13 | try {
14 | Constructor[] constructors = EagerSingleton.class.getDeclaredConstructors();
15 | for (Constructor constructor : constructors) {
16 | //Below code will destroy the singleton pattern
17 | constructor.setAccessible(true);
18 | instanceTwo = (EagerSingleton) constructor.newInstance();
19 | break;
20 | }
21 | } catch (Exception e) {
22 | e.printStackTrace();
23 | }
24 | System.out.println(instanceOne.hashCode());
25 | System.out.println(instanceTwo.hashCode());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/singleton/SerializedSingleton.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.singleton;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * @author Vishnu
7 | *
8 | */
9 | public class SerializedSingleton implements Serializable{
10 | private static final long serialVersionUID = -7604766932017737115L;
11 |
12 | private SerializedSingleton(){}
13 |
14 | private static class SingletonHelper{
15 | private static final SerializedSingleton instance = new SerializedSingleton();
16 | }
17 |
18 | public static SerializedSingleton getInstance(){
19 | return SingletonHelper.instance;
20 | }
21 | protected Object readResolve() {
22 | return SingletonHelper.instance;
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/singleton/SingletonSerialzeTest.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.singleton;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.FileNotFoundException;
5 | import java.io.FileOutputStream;
6 | import java.io.IOException;
7 | import java.io.ObjectInput;
8 | import java.io.ObjectInputStream;
9 | import java.io.ObjectOutput;
10 | import java.io.ObjectOutputStream;
11 |
12 | /**
13 | * @author Vishnu
14 | *
15 | */
16 | public class SingletonSerialzeTest {
17 | public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
18 | SerializedSingleton instanceOne = SerializedSingleton.getInstance();
19 | ObjectOutput out = new ObjectOutputStream(new FileOutputStream(
20 | "filename.ser"));
21 | out.writeObject(instanceOne);
22 | out.close();
23 |
24 | //deserailize from file to object
25 | ObjectInput in = new ObjectInputStream(new FileInputStream(
26 | "filename.ser"));
27 | SerializedSingleton instanceTwo = (SerializedSingleton) in.readObject();
28 | in.close();
29 |
30 | System.out.println("instanceOne hashCode="+instanceOne.hashCode());
31 | System.out.println("instanceTwo hashCode="+instanceTwo.hashCode());
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/singleton/StaticBlockSingleton.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.singleton;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public class StaticBlockSingleton {
8 | private static StaticBlockSingleton instance;
9 |
10 | private StaticBlockSingleton(){}
11 |
12 | static{
13 | try{
14 | instance = new StaticBlockSingleton();
15 | }catch(Exception e){
16 | throw new RuntimeException("Exception is occured in creating singleton instance");
17 | }
18 | }
19 |
20 | public static StaticBlockSingleton getInstance(){
21 | return instance;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/com/vish/pattern/singleton/ThreadSafeSingleton.java:
--------------------------------------------------------------------------------
1 | package com.vish.pattern.singleton;
2 |
3 | /**
4 | * @author Vishnu
5 | *
6 | */
7 | public class ThreadSafeSingleton {
8 | private static ThreadSafeSingleton instance;
9 |
10 | private ThreadSafeSingleton(){}
11 |
12 | public static ThreadSafeSingleton getInstance(){
13 | if(instance == null){
14 | synchronized (ThreadSafeSingleton.class) {
15 | instance = new ThreadSafeSingleton();
16 | }
17 | }
18 | return instance;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------