What is Abstract Factory Design Pattern ?

Download the working code from my git hub repository from this link https://github.com/premaseem/designPatterns/tree/master/AbstractFactoryPattern

Basic Definition of Abstract Factory :

Abstract Factory patterns works around a super-factory which creates other factories. This factory is also called as Factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

In Abstract Factory pattern an interface is responsible for creating a factory of related objects, without explicitly specifying their classes.

 

AbstractFactory1

 

The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.[1] In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the theme. The client doesn’t know (or care) which concrete objects it gets from each of these internal factories, since it uses only the generic interfaces of their products.[1] This pattern separates the details of implementation of a set of objects from their general usage and relies on object composition, as object creation is implemented in methods exposed in the factory interface.[2]

An example of this would be an abstract factory class DocumentCreator that provides interfaces to create a number of products (e.g. createLetter() and createResume()). The system would have any number of derived concrete versions of the DocumentCreator class like FancyDocumentCreator or ModernDocumentCreator, each with a different implementation of createLetter() and createResume() that would create a corresponding object like FancyLetter or ModernResume. Each of these products is derived from a simple abstract class like Letter or Resume of which the client is aware. The client code would get an appropriate instance of the DocumentCreator and call its factory methods. Each of the resulting objects would be created from the same DocumentCreator implementation and would share a common theme (they would all be fancy or modern objects). The client would only need to know how to handle the abstract Letter or Resume class, not the specific version that it got from the concrete factory.

A factory is the location of a concrete class in the code at which objects are constructed. The intent in employing the pattern is to insulate the creation of objects from their usage and to create families of related objects without having to depend on their concrete classes.[2] This allows for new derived types to be introduced with no change to the code that uses the base class.

Use of this pattern makes it possible to interchange concrete implementations without changing the code that uses them, even at runtime. However, employment of this pattern, as with similar design patterns, may result in unnecessary complexity and extra work in the initial writing of code. Additionally, higher levels of separation and abstraction can result in systems which are more difficult to debug and maintain. Therefore, as in all software designs, the trade-offs must be carefully evaluated.

JavaTip: Can we override private method in Java

No, We can not override private method in Java, just like we can not override static method in Java. Like static methods, private method in Java is also bonded during compile time using static binding by Type information and doesn’t depends on what kind of object a particular reference variable is holding. Since method overriding works on dynamic binding, its not possible to override private method in Java. private methods are not even visible to Child class, they are only visible and accessible in the class on which they are declared. private keyword provides highest level of Encapsulation in Java. Though you can hide private method in Java by declaring another private method with same name and different method signature.

 

Overriding private method in Java – Testing

Can private method be overriden in Java As per above paragraph we can not override private method in Java because its bonded during compile time using static binding. Now let’s test this theory by an example Java program :

/**
*
* Java program to demonstrate that private method can not be overridden in Java.
* This Java programs calls both private and non private method with child class
* object on constructor of parent class.
* Only non private method of Child class invoked while private method of
* Parent class is invoked, Which confirms that private method can not be overridden in Java
*  and only be hidden if we declare same message in Child class.
* @author
*/
public class PrivateMethodCanNotBeOverriden{
public static void main(String args[]) {
//shows that private method can not be overridden in Java    
        Parent parent = new Child();
}
}
class Parent{
public Parent(){
        name();
        normal();
}
private void name(){
System.out.printf(“private method inside Parent class in Java %n”);
}
public void normal(){
System.out.println(“non private method from Parent class can be overridden”);
}
}
class Child extends Parent{
/*
     * Private method can not be overridden in Java, they can only be hidden
     */

private void name(){
System.out.printf(“private method inside Child class in Java %n”);
}
    @Override
public void normal(){
System.out.println(“non private overridden method from Child class “);
}
}
Output
private method inside Parent class in Java
non private overridden method from Child class

String Comporator to sort collection efficiently

Below is small code to implement String sorting using collection.sort(collectionName,comparatorForsoring)
It does not return any thing, it works on the collection object which is passed as argument.

public class StringPropertyComparator implements Comparator{

private String property;
private boolean sortAsec;

public StringPropertyComparator(String property,boolean sort)
{
this.property=property;
this.sortAsec=sort;
}

public int compare(Object o1, Object o2) {
Object src = PropertyResolver.getValue(property, o1);
Object compareTo = PropertyResolver.getValue(property, o2);
String src1=src==null ? “”:src.toString();
String compareTo1=compareTo==null ? “”:compareTo.toString();
return sortAsec ? src1.compareToIgnoreCase(compareTo1):compareTo1.compareToIgnoreCase(src1);
}
}

ArrayListMultimap from google collection

For iterating and storing the nested arrays, it is a good collection which takes care of null, not null etc makes storage and retrieval handy.
Below is a small code snippet of usage while gets the list of object array returned from a hibernate query:
public ListMultimap getAssignmentEventStatus(List assignmentEvents) {
String hql = “select lpa.assignmentEvent.id, lpa.progression from LearningPathAssignment lpa where lpa.assignmentEvent in (:assignmentEvents)”;
List learningPathAssignmentStatus = getSession().createQuery(hql).setParameterList(“assignmentEvents”, assignmentEvents).list();

// Map> map = new HashMap>();

ArrayListMultimap map = ArrayListMultimap. create();
for (Object[] columns : learningPathAssignmentStatus) {
Long id = (Long) columns[0];
LearningPathAssignment.Progression progression = (Progression) columns[1];
map.put(id, progression);
/*
* if (map.get(id) == null) { ArrayList list = new
* ArrayList(); list.add(progression); map.put(id, list); }
* else { List list = map.get(id); list.add(progression); }
*/
return map;
}