Decorator Design Pattern

Decorator Design Pattern.


Decorator Design Pattern allows class to extend its functionalities dynamically without changing the actual class implementation.


Decorator Design Pattern Example In Java.

Say we are opening a Cake shop and planning to sell many flavours of cake.


Also, custom cake order will be accepted as per Customer needs. 
How to design a class for this purpose. Decorator pattern is helpful for this kind of requirement.


Cake.java
interface Cake{
 void flavour();
 int getAmount();
}
BasicCake.java
class BasicCake implements Cake{
 @Override
 public void flavour() {
  System.out.println("Basic Cake");
 }

 @Override
 public int getAmount() {
  return 50;
 }
}
CakeDecorator.java (It is made abstract because it is helper class and object of only implementations of it is possible)
abstract class CakeDecorator implements Cake{
 private Cake cake;
 
 public CakeDecorator(Cake cake) {
  this.cake = cake;
 }
 
 @Override
 public void flavour() {
  cake.flavour();
 }
 
 @Override
 public int getAmount() {
  return cake.getAmount();
 }
}
VanillaCake.java
class VanillaCake extends CakeDecorator{
 
 public VanillaCake(Cake cake) {
  super(cake);
 }
 
 @Override
 public void flavour() {
  super.flavour();
  System.out.println("Adding Vanilla falvour");
 }
 
 @Override
 public int getAmount() {
  return super.getAmount() + 10;
 }
}
StrawberyCake.java
class StrawberyCake extends CakeDecorator{
 
 public StrawberyCake(Cake cake) {
  super(cake);
 }
 
 @Override
 public void flavour() {
  super.flavour();
  System.out.println("Adding Strawberry falvour");
 }
 
 @Override
 public int getAmount() {
  return super.getAmount() + 10;
 }
}
DecoratorDesignPattern.java
public class DecoratorDesignPattern {

 public static void main(String[] args) {
  VanillaCake vanillaStrawberryCake = new VanillaCake(new StrawberyCake(new BasicCake()));
  vanillaStrawberryCake.flavour();
  System.out.println(vanillaStrawberryCake.getAmount());
 }
}


Decorator Design Pattern used in JDK API.


Base class
java.io.Reader;
java.io.InputStream;

Decorators:
java.io.BufferedReader(java.io.Reader);
java.io.FileReader(java.io.Reader);

java.io.DataInputStream(
java.io.InputStream)
java.io.BufferedInputStream
(java.io.InputStream)  

Constructor of BufferedReader takes parameter as Reader class and add more few more features like readLine() method, which is specific to BufferedReader class only.

Eg:  BufferedReader in = new BufferedReader(new FileReader("foo.in"));
Eg:  DataInputStream input = new DataInputStream(new ZipInputStream(new

        FileInputStream("foo.zip")));
 

You may also like to see


Observer Design Pattern

When to use Builder design pattern

Adapter Design Pattern

Command Design Pattern

Enjoy !!!! 

If you find any issue in post or face any error while implementing, Please comment.

Observer Design Pattern

Observer Design Pattern.


Observer Design Pattern is used to notify interested Observers on any change to subject.


Observer Design Pattern Example In Java.

In our example we will notify SocialMediaNotifier and SubscribedUserNotifier whenever a new post is published on JavaByPatel blog.  
SocialMediaNotifier and SubscribedUserNotifier will update post to necessary places. 

Observer : SocialMediaNotifier, SubscribedUserNotifier 
Subject : Blog post.


Observer.java
interface Observer{
 void notify(String post);
}
SocialMediaNotifier.java
class SocialMediaNotifier implements Observer{
 @Override
 public void notify(String post) {
  System.out.println("SocialMediaNotifier: New post published :"+post);
 }
}
SubscribedUserNotifier.java
class SubscribedUserNotifier implements Observer{
 @Override
 public void notify(String post) {
  System.out.println("SubscribedUserNotifier: New post published :"+post);
 }
}
Subject.java
interface Subject{
 void addObserver(Observer observer);
 void removeObserver(Observer observer);
 void notifyObservers();
}
JavaByPatelBlogPost.java
class JavaByPatelBlogPost implements Subject{
 private String post;
 private List<Observer> listOfObserver = new ArrayList<Observer>();
 
 @Override
 public void addObserver(Observer observer) {
  listOfObserver.add(observer);
 }

 @Override
 public void removeObserver(Observer observer) {
  listOfObserver.remove(observer);
 }

 @Override
 public void notifyObservers() {
  for (Observer observer : listOfObserver) {
   observer.notify(post);
  }
 }
 
 public void newPost(String post) {
  this.post = post;
  notifyObservers();
 }
}

ObserverDesignPattern.java
public class ObserverDesignPattern {

 public static void main(String[] args) {
  SocialMediaNotifier socialMediaNotifier = new SocialMediaNotifier();
  SubscribedUserNotifier subscribedUserNotifier = new SubscribedUserNotifier();
  
  JavaByPatelBlogPost blogPost = new JavaByPatelBlogPost();
  blogPost.addObserver(socialMediaNotifier);
  blogPost.addObserver(subscribedUserNotifier);
  
  blogPost.newPost("HelloPost");
 }
}

You may also like to see


Decorator Design Pattern

Adapter Design Pattern

When to use Builder design pattern

Command Design Pattern

Enjoy !!!! 

If you find any issue in post or face any error while implementing, Please comment.

Adapter Design Pattern

Adapter Design Pattern.


Adapter Design Pattern is used to make in-compatible interfaces compatible.



Adapter Design Pattern Example In Java.

We have Socket which charges Apple Mobile and we want same Socket to be used for Samsung Mobile as well, We will make socket compatible.  

Apple.java
interface Apple{
 void charge();
}
AppleImpl.java
class AppleImpl implements Apple{
 
 @Override
 public void charge() {
  System.out.println("Charging Apple Mobile...");
 }
}
SocketForAppleMobile.java
class SocketForAppleMobile{
 public static void charge(Apple apple){
  System.out.println("It is not correct but say: Start Electricity at home");
  apple.charge();
  System.out.println("It is not correct but say: End Electricity at home");
 }
}
Samsung.java
interface Samsung{
 void charge();
}
SamsumgImpl.java
class SamsumgImpl implements Samsung{
 
 @Override
 public void charge() {
  System.out.println("Charging Samsung Mobile...");
 }
}
AppleSamsungAdapter.java
class AppleSamsungAdapter implements Apple{
 
 private Samsung samsung;
 
 public AppleSamsungAdapter(Samsung samsung) {
  this.samsung = samsung;
 }
 
 @Override
 public void charge() {
  samsung.charge();
 }
}
AdapterDesignPattern.java
public class AdapterDesignPattern {

 public static void main(String[] args) {
  Apple appleMobile = new AppleImpl();
  SocketForAppleMobile.charge(appleMobile);
  
  Samsung samsungMobile = new SamsumgImpl();
  //SocketForAppleMobile.charge(samsungMobile); //It will not accept Samsung charger
  
  AppleSamsungAdapter appleSamsungMobile = new AppleSamsungAdapter(samsungMobile);
  SocketForAppleMobile.charge(appleSamsungMobile); //It will charge now..
 }
}

Decorator Design Pattern

When to use Builder design pattern

Observer Design Pattern

Command Design Pattern

Enjoy !!!! 

If you find any issue in post or face any error while implementing, Please comment.