Toggling application features using wrapper and factory patterns

Toggling application features using wrapper and factory patternsKasun PereraBlockedUnblockFollowFollowingFeb 10As we know an application consists of different features.

There are times that a customer needs to enable and disable these custom features to optimize an application.

As a programmer we need to enable customers to toggle features like above.

There are many ways to achieve this.

But in this blog we will discuss about how to use wrapper and factory patterns to achieve this task.

Toggling features means simply enabling or disabling certain functionalities.

For an example it is like getting money from an ATM with receipt or without receipt.

So before we start with complex things let’s discuss briefly what factory method and wrapping patterns refer to.

Factory Method patternFactory Method Pattern is a creational design pattern which uses factory methods to create objects without the need to specify a class of the object to be instantiated.

That means simply you can call a method and instantiate a certain object without worrying about the creational logic.

Example:Let’s think about a car factory and driving.

Customer needs a car.

So there is a car interface,public interface Car { public void drive();}There should be different car types.

public class Lamborghini implements Car { public void drive() { System.

out.

prinln(“Driving Lambogini”); }}public class Mustang implements Car { public void drive() { System.

out.

prinln(“Driving Mustang”); }}And then there should be a car factory which gives the needed car to the consumer.

public class CarFactory { public Car getCar(String carType) { if(carType == “Lamborghini”) return new Lamborghini(); else if(carType == “Mustang”) return new Mustang(); return null; }}So when a customer wants a car he will get the car from the car factory and drive without knowing the creational logic.

public class Customer { public static void main(String[] args) { CarFactory carFactory = new CarFactory(); Car car1 = carFactory.

getCar(“Lambogini”); Car car2 = carFactory.

getCar(“Mustang”); Car1.

drive(); //Prints “Driving Lambogini” Car2.

drive(); //Prints “DrivAing Mustang” }}WrapperA wrapper is a class which contains an instance of another class.

In here the instance is not exposed to the outside environment directly.

The main purpose of wrapping is to give a different or an additional functionality to an instance.

There are many design patterns which use wrapping.

For example Decorator and Adapter.

In here let’s discuss a simple wrapper similar to the decorator design pattern.

Example: Let’s discuss about a dashboard of a car.

So there should be a Dashboard interface.

public interface Dashboard { public void viewDashboard();}There can be a dashboard which provides the basic function.

public class DefaultDashboard implements Dashboard { public void viewDashboard() { System.

out.

println(“This is a simple dashboard”); }}Some dashboards can be digital with extra technologies.

public class DigitalDashboard implements Dashboard { private Dashboard dashboard; private DigitalScreen digitalScreen; public DigitalDashboard(Dashboard dboard, DigitalScreen dscreen){ dashboard = dboard; digitalScreen = dscreen; } public void viewDashboard() { dashboard.

viewDashboard(); digitalscreen.

view(); }}public class DigitalScreen { public void view() { System.

out.

println(“View digital results”); }}Toggling featuresNow let’s give our attention to toggling features using factories and wrappers.

It can be done in 4 simple steps.

First identify the base classes in which you need to add features.

Create interfaces for the base classes.

Create wrappers implementing those interfaces with the necessary functionality to add the feature.

Use factories to load the needed wrapper or base implementation according to customer preference.

Let’s look at this using an example, Think about a washing machine which has a single button called “Start”.

And there is a switch that enables only wash or both wash and dry.

You just have to turn the switch to the preferred side and then press “Start” button.

So as we can see here the normal process in this instance is wash and the extra feature dry can be added or removed.

Now let’s put this into our 4 steps.

Step 1There should be class which does the washing activity.

Let’s just call it “BasicWasher” in this situation.

public class BasicWasher { public void wash() { System.

out.

println("Washing"); }}Step 2So now we need to create an interface for the “ BasicWasher” class.

Let’s name the interface “Washer”public interface Washer { void wash();}Modify the base class by implementing the interface.

public class BasicWasher implements Washer{ public void wash() { System.

out.

println("Washing"); }}Step 3Now we need to create a wrapper “DryingWasher” implementing “Washer” interface and wraps the “BasicWasher” to include drying feature.

public class DryingWasher implements Washer{ Washer washer; String dryerType; public DryingWasher(Washer washer, String dryerType) { this.

washer = washer; this.

dryerType = dryerType; } public void wash() { System.

out.

println("Washing with" + dryerType); }}Step 4Now it’s time to create our factories for washer “WasherFactory”.

If the user switches to only wash option the factory should give “BasicWasher”.

But if the user switches to wash and dry option, the factory should give the “DryingWasher”.

public class WasherFactory { public Washer getWasher(boolean dryingEnabled) { if (dryingEnabled) { return new DryingWasher(new BasicWasher(), "Soft Dryer"); } else { return new BasicWasher(); } }}The main method would be like below.

public class WashingMachineDemo { public static void main(String[] args) { boolean dryingEnabled = true; WasherFactory washerFactory = new WasherFactory(); Washer dryWasher = washerFactory.

getWasher(dryingEnabled); dryWasher.

wash(); //Washes with drying effect dryingEnabled = false; Washer basicWasher = washerFactory.

getWasher(dryingEnabled); basicWasher.

wash(); //Washes without drying effect }}This is a basic example on how to use this pattern.

There are several advantages in using this pattern,One good thing about this pattern is you can add any feature you want independently without affecting base function and other features.

And also using this pattern you can build your application in a layered manner.

You can change, remove or add layers independently without affecting others.

ConclusionI hope you got a clear understanding about how to use wrappers and factory method patterns to toggle features.

In my upcoming blogs I will also discuss about a real world application of this pattern.

.

. More details

Leave a Reply