Design Patterns:
Classified as :
Creational Pattern :
Singleton DesignPattern
package test.creational.designPattern;
class DatabaseConnection{
private static DatabaseConnection instance = new DatabaseConnection();
private DatabaseConnection(){
}
public static DatabaseConnection getDBConnection(){
return instance;
}
public void getConnection(){
System.out.println(" Established to Database successfully ");
}
}
public class SingletonExample {
public static void main(String args[]) {
DatabaseConnection.getDBConnection().getConnection();
}
}
Factory Design Pattern
package test.creational.designPattern;
interface Shape {
void getShape();
}
class Circle implements Shape {
@Override
public void getShape() {
System.out.println(" Inside Circle ");
}
}
class Rectangle implements Shape {
@Override
public void getShape() {
System.out.println(" Inside Rectangle ");
}
}
class Square implements Shape {
@Override
public void getShape() {
System.out.println(" Inside Square ");
}
}
class Shapefactory {
public Shape getType(String shape) {
if (shape.equals("circle")) {
return new Circle();
}
if (shape.equals("rect")) {
return new Rectangle();
}
if (shape.equals("square")) {
return new Square();
}
return null;
}
}
public class FactoryExample {
public static void main(String args[]) {
Shapefactory shapefactory = new Shapefactory();
Shape shape1 = shapefactory.getType("rect");
shape1.getShape();
}
}
Abstract Factory Pattern
Structural Patterns :
Adapter:
package test.creational.designPattern;
interface MediaPlayer {
public void play(String audioType, String filename);
}
class MediaAdapter implements MediaPlayer {
AdvancedMediaPlayer advancedMusicPlayer;
public MediaAdapter(String audioType) {
if (audioType.equalsIgnoreCase("vlc")) {
advancedMusicPlayer = new VlcPlayer();
} else if (audioType.equalsIgnoreCase("mp4")) {
advancedMusicPlayer = new Mp4Player();
}
}
public void play(String audioType, String fileName) {
if (audioType.equalsIgnoreCase("vlc")) {
advancedMusicPlayer.playVlc(fileName);
} else if (audioType.equalsIgnoreCase("mp4")) {
advancedMusicPlayer.playMp4(fileName);
}
}
}
class AudioPlayer implements MediaPlayer {
MediaAdapter mediaAdapter;
@Override
public void play(String audioType, String fileName) {
// inbuilt support to play mp3 music files
if (audioType.equalsIgnoreCase("mp3")) {
System.out.println("Playing mp3 file. Name: " + fileName);
}
// mediaAdapter is providing support to play other file formats
else if (audioType.equalsIgnoreCase("vlc")
|| audioType.equalsIgnoreCase("mp4")) {
mediaAdapter = new MediaAdapter(audioType);
mediaAdapter.play(audioType, fileName);
}
else {
System.out.println("Invalid media. " + audioType
+ " format not supported");
}
}
}
interface AdvancedMediaPlayer {
public void playVlc(String fileName);
public void playMp4(String fileName);
}
class VlcPlayer implements AdvancedMediaPlayer {
@Override
public void playVlc(String fileName) {
System.out.println("Playing vlc file. Name: " + fileName);
}
@Override
public void playMp4(String fileName) {
// do nothing
}
}
class Mp4Player implements AdvancedMediaPlayer {
@Override
public void playVlc(String fileName) {
// do nothing
}
@Override
public void playMp4(String fileName) {
System.out.println("Playing mp4 file. Name: " + fileName);
}
}
public class AdapterSample {
public static void main(String[] args) {
AudioPlayer audioPlayer = new AudioPlayer();
audioPlayer.play("mp3", "beyond the horizon.mp3");
audioPlayer.play("mp4", "alone.mp4");
audioPlayer.play("vlc", "far far away.vlc");
audioPlayer.play("avi", "mind me.avi");
}
}
Proxy :
Protection , Remote
Bridge
Facade
package test.structural.designPattern;
interface Goods {
void getGoods();
}
class PackagedGoods implements Goods {
public void getGoods() {
System.out.println(" Allow to get the Packaged Foods");
}
}
class LooseGoods implements Goods {
public void getGoods() {
System.out.println(" Allow to get the Loose Food items");
}
}
class StoreOwner {
private Goods looseGoods;
private Goods packagedGoods;
public StoreOwner() {
this.looseGoods = new LooseGoods();
this.packagedGoods = new PackagedGoods();
}
public void getPackagedGoods() {
packagedGoods.getGoods();
}
public void getLooseGoods() {
looseGoods.getGoods();
}
}
public class FacadeSample {
public static void main(String[] args) {
StoreOwner storeOwner = new StoreOwner();
storeOwner.getPackagedGoods();
storeOwner.getLooseGoods();
}
}
FlyWeight
Behavioural Patterns
State
package test.behavioural.designPattern;
interface CompressionType {
void compressionType(CompressionContext context);
}
class ZipCompression implements CompressionType {
public void compressionType(CompressionContext context) {
System.out.println(" Compression Strategy is : ZIP");
}
}
class WinrarCompression implements CompressionType {
public void compressionType(CompressionContext context) {
System.out.println(" Compression Strategy is : WINRAR");
}
}
class CompressionContext {
CompressionType compressionType = new ZipCompression();
public void setCompressionType(CompressionType compressionType) {
this.compressionType = compressionType;
}
void getCompression() {
compressionType.compressionType(this);
}
}
public class StateDPDSample {
public static void main(String args[]) {
CompressionContext compressionContext = new CompressionContext();
compressionContext.getCompression();
compressionContext.setCompressionType(new WinrarCompression());
compressionContext.getCompression();
}
}
Strategy
package test.behavioural.designPattern;
interface Country {
void currency();
}
class Asian implements Country {
@Override
public void currency() {
System.out.println(" Currncy is INR");
}
}
class European implements Country {
@Override
public void currency() {
System.out.println(" Currency is EURO");
}
}
class CountryContext {
Country country;
public void setCountry(Country country) {
this.country = country;
}
public void currency() {
country.currency();
}
}
public class StrategyDesignPattern {
public static void main(String args[]) {
CountryContext countryContext = new CountryContext();
countryContext.setCountry(new Asian());
countryContext.currency();
countryContext.setCountry(new European());
countryContext.currency();
}
}
Observer
package test.behavioural.designPattern;
import java.util.ArrayList;
import java.util.List;
class NasdaqStock {
private int stockPrice;
List<StockExchange> stockExchangeList = new ArrayList<StockExchange>();
public void setStockPrice(int stockPrice) {
this.stockPrice = stockPrice;
notifyOtherExchanges();
}
public int getStockPrice() {
return stockPrice;
}
void notifyOtherExchanges() {
for (StockExchange s : stockExchangeList) {
s.getStockValue();
}
}
public void attach(StockExchange stockExchange) {
stockExchangeList.add(stockExchange);
}
}
abstract class StockExchange {
NasdaqStock nasdaqStock;
abstract void getStockValue();
}
class NSE extends StockExchange {
NSE(NasdaqStock nasdaqStock) {
this.nasdaqStock = nasdaqStock;
this.nasdaqStock.attach(this);
}
@Override
void getStockValue() {
System.out.println(" Value at NSE after change at Nasdaq is "
+ this.nasdaqStock.getStockPrice());
}
}
class BSE extends StockExchange {
BSE(NasdaqStock nasdaqStock) {
this.nasdaqStock = nasdaqStock;
this.nasdaqStock.attach(this);
}
@Override
void getStockValue() {
System.out.println(" Value at BSE after change at Nasdaq is "
+ this.nasdaqStock.getStockPrice());
}
}
public class ObserverPatternSample {
public static void main(String args[]) {
NasdaqStock nasdaqStock = new NasdaqStock();
new BSE(nasdaqStock);
new NSE(nasdaqStock);
nasdaqStock.setStockPrice(100);
nasdaqStock.setStockPrice(5000);
}
}
Chain of Responsibility