Wednesday, December 13, 2017

Visitor Pattern


Definition -

    Allows one or more operations to be applied at run time allowing the operations to be decided at runtime rather than compile time.


Scenario -
   
    Let us take the same example of a car customization plant where you can customize the windshield color, wheel, color, stripes.
1) We will have an interface called customizable which will be implemented by the components which can be customized based on the users wish else it will use the default. The following things can be customized - windshield color, stripes, wheel.

package VisitorPattern;

public interface customizable {
    
    public void addtocar(customizer c);

}

2) We will have concrete classes for the same windshieldcolor, stripes, wheel.
windshield.java

package VisitorPattern;

public class windowshield implements customizable{
    
    String color;
    
    public windowshield(String color) {
        this.color=color;
    }

    @Override
    public void addtocar(customizer c) {
        
        c.addcomponent(this);
        
    }
    

}

stripes.java
package VisitorPattern;

public class stripes implements customizable{
    
    String stripestyle;
    
    public stripes(String stripestyle) {
        this.stripestyle=stripestyle;
    }

    @Override
    public void addtocar(customizer c) {
        
        c.addcomponent(this);
        
    }
    
    

}

wheel.java

package VisitorPattern;

public class wheel implements customizable {

    String style;
    
    public wheel(String style) {
        this.style=style;
    }
    
    @Override
    public void addtocar(customizer c) {
        
        c.addcomponent(this);
        
    }

}

3) We will have a component called customizer which will be an interface and will have the call to all the customizable components. The interface will enable us to extend in future and be used as a standard across different types of vehicles in future.
package VisitorPattern;

public interface customizer {
    
    public void addcomponent(windowshield w);
    public void addcomponent(wheel wl);
    public void addcomponent(stripes s);

}

4) We will have implementer which is a concrete implementation of the customizer.

package VisitorPattern;

public class customizerimpl implements customizer {
    
    

    @Override
    public void addcomponent(windowshield w) {
        
        System.out.println("Adding windshield of color: "+w.color);
        
    }

    @Override
    public void addcomponent(wheel wl) {
        
        System.out.println("Adding wheel of style: "+wl.style);
        
    }

    @Override
    public void addcomponent(stripes s) {
        
        System.out.println("Adding stripes of style: "+s.stripestyle);
        
    }

}


To see this pattern in action use the code below - 
package VisitorPattern;

import java.util.ArrayList;

public class visitordemo {

    public static void main(String[] args) {
        
        ArrayList<customizable> customcomp=new ArrayList<customizable>();
        customcomp.add(new windowshield("Blue"));
        customcomp.add(new wheel("Yokohama Pro ++"));
        customcomp.add(new stripes("Stripe1"));
        
        customizer creator=new customizerimpl();
        
        for(customizable tempc: customcomp) {
            tempc.addtocar(creator);
        }

    }

}



Place to use -
1) When you want to call different functionalities based on user input and it might be unrelated objects which might be passed based on scenario.

The code for the above is present in the
link

No comments:

Post a Comment