Definition -
This is a creational pattern. It is also reffered to as Factory of Factories. This helps designers to decouple objects from the implementing systems. Here the abstract factory will have a set of core functionalities which has to be implemented by all the factories which implement them.
Scenario -
Lets take a scenario where in you are designing a software which would display images. The image can be either jpeg / png. In future there can be other format as well where in you would want to implement in an incremental manner. -
1) There will be an interface called imagerender where we state it can display an image. We will call this imagerender.
2) Since the feature of the software is to display image of either png or jpeg we will have an image adapter which will perform the action to choose which class to implement. We will call this imageAdapter
3) There will be an interface called advancedimageprocessor which will have the template for the different kinds of imageformat functionalities.
4) There will be two concrete factories called jpegimage and pngimage which will provide the actual implementation functionalities.
jpegimage.java
pngimage.java
5) We will have a class called imagedisplay which call the imageAdapter.
6) Finally we will have a client called adapterClient which will be calling either PNG or JPEG formats.
Place to use -
1. If you require middleman between two modules and at the same time you require to ensure it is a flexible design, I cannot say it is a scalable design (You can differ on this).
2. When you want to provide temporary fix the interaction between two modules till you figure out a permanent design.
3. Although you might consider this when you want to use a plug in kind of feature, but again be careful.
Place not to use -
1. Do not use this if you have other options because debugging is quiet an headache.
2. It is not a neat design.
3. Makes maintainence of the code a nightmare.
The code for this is available in github link here
This is a creational pattern. It is also reffered to as Factory of Factories. This helps designers to decouple objects from the implementing systems. Here the abstract factory will have a set of core functionalities which has to be implemented by all the factories which implement them.
Scenario -
Lets take a scenario where in you are designing a software which would display images. The image can be either jpeg / png. In future there can be other format as well where in you would want to implement in an incremental manner. -
1) There will be an interface called imagerender where we state it can display an image. We will call this imagerender.
package AdapterPattern;
public interface imagerender {
public void displayimage(String imagetype, String path);
}
2) Since the feature of the software is to display image of either png or jpeg we will have an image adapter which will perform the action to choose which class to implement. We will call this imageAdapter
package AdapterPattern;
public class imageAdapter implements imagerender{
Advancedimageprocessor aip;
public imageAdapter(String imagetype) {
if(imagetype.equalsIgnoreCase("JPEG")) {
System.out.println("Creating object ");
aip=new jpegimage();
}
else if(imagetype.equalsIgnoreCase("PNG")){
System.out.println("Creating object ");
aip=new pngimage();
}
}
@Override
public void displayimage(String imagetype, String path) {
if(imagetype.equalsIgnoreCase("JPEG")) {
System.out.println("displaying object ");
aip.jpegdisplay(path);
}
else if(imagetype.equalsIgnoreCase("PNG")) {
System.out.println("displaying object ");
aip.pngdisplay(path);
}
}
}
3) There will be an interface called advancedimageprocessor which will have the template for the different kinds of imageformat functionalities.
package AdapterPattern;
public interface Advancedimageprocessor {
public void jpegdisplay(String path);
public void pngdisplay(String path);
}
4) There will be two concrete factories called jpegimage and pngimage which will provide the actual implementation functionalities.
jpegimage.java
package AdapterPattern;
public class jpegimage implements Advancedimageprocessor {
public jpegimage() {
System.out.println("Inside class jpeg image");
}
@Override
public void jpegdisplay(String path) {
System.out.println("Displaying jpeg image from the path: "+path);
}
@Override
public void pngdisplay(String path) {
// TODO Auto-generated method stub
}
}
pngimage.java
package AdapterPattern;
public class pngimage implements Advancedimageprocessor {
public pngimage() {
System.out.println("Inside class png image");
}
@Override
public void jpegdisplay(String path) {
}
@Override
public void pngdisplay(String path) {
System.out.println("Displaying png image from the path: "+path);
}
}
5) We will have a class called imagedisplay which call the imageAdapter.
package AdapterPattern;
public class imageDisplay implements imagerender{
imageAdapter iA;
public imageDisplay() {
System.out.println("Inside imageDisplay");
}
@Override
public void displayimage(String imagetype, String path) {
System.out.println("Displaying "+ imagetype+" in path "+path);
iA=new imageAdapter(imagetype);
iA.displayimage(imagetype, path);
}
}
6) Finally we will have a client called adapterClient which will be calling either PNG or JPEG formats.
package AdapterPattern;
public class adapterClient {
public static void main(String[] args) {
imageDisplay iD = new imageDisplay();
iD.displayimage("PNG", "C:\test1");
//iD.displayimage("JPEG", "C:\test2");
}
}
Place to use -
1. If you require middleman between two modules and at the same time you require to ensure it is a flexible design, I cannot say it is a scalable design (You can differ on this).
2. When you want to provide temporary fix the interaction between two modules till you figure out a permanent design.
3. Although you might consider this when you want to use a plug in kind of feature, but again be careful.
Place not to use -
1. Do not use this if you have other options because debugging is quiet an headache.
2. It is not a neat design.
3. Makes maintainence of the code a nightmare.
The code for this is available in github link here
No comments:
Post a Comment