Introduction to UML

Before i start discussing Design Patterns, i like to give a small introduction about Unified Modeling Language(UML).

It is a good design to prepare UML before you start coding. It's very easy to learn UML, i will continue guiding you the UML in simple steps:

Symbols to follow as prefix for classes, variables, access specifiers and methods.

i) default will be prefix by (~)
ii) private will be prefix by (-)
iii) public will be prefix by (+)
iv) protected will be prefix by (#)
v) abstract class,variables & methods must be in italics
vi) static variables & methods must be underlined

I will continue the presentation in the next section, thanks for your time

Creational design pattern tries to reduce the creational complexities (of an object) at its maximum level.

Factory Pattern:

One of the goals of object-oriented design is to delegate responsibility among different objects.A class may need it’s subclasses to specify the objects to be created or delegate responsibility to one of several helper subclasses so that knowledge can be localized to specific helper subclasses.

The Factory patterns can be used in following cases:

1 When a class does not know which class of objects it
must create.
2 A class specifies its sub-classes to specify which
objects to create.
3 In programmer’s language (very raw form), you can use
factory pattern where you have to create an object of
any one of sub-classes depending on the data provided.

Factory Pattern

Factory Pattern
Factory Pattern

Code Wise Example for Factory Pattern


package factorypattern;

public abstract class Builder {

    public abstract void build();
}

package factorypattern;

public class HomeBuilder extends Builder {

    HomeBuilder() {
        System.out.println("HomeBuilder");
    }
    public void build() {
        System.out.println("home builder builts home");
    }
}

package factorypattern;

public class FactoryBuilder extends Builder {

    FactoryBuilder() {
        System.out.println("factory builder");
    }
    public void build() {
        System.out.println("Factory builder builts factory");  
    }

}

package factorypattern;

public class ApartmentBuilder extends Builder {

    ApartmentBuilder() {
        System.out.println("Constructor ApartmentBuilder");
    }
    public void build() {
        System.out.println("Apartment builder builts apartment");
    }
}

package factorypattern;

public class BuilderFactory {

    public static Builder createBuilder(String builder) {
    
        if(builder.equals("HomeBuilder")) {
            return new HomeBuilder();
        } else if(builder.equals("FactoryBuilder")){
            return new FactoryBuilder();
        } else {
            return new ApartmentBuilder();
        }
    }
}

package factorypattern;

public class BuilderClient{

    public static void main(String[] args) {
        Builder builder = BuilderFactory.createBuilder("HomeBuilder");
    }
}

To Eliminate Duplicates From the List

To Eliminate duplicates and non duplicate items from the List.

Scenario:

For eg., if the list contains s1,s2,s3,s1,s2,s3,s4 then
the result will be like s1,s2,s3. This how the following code will work.

List list = new ArrayList();
		list.add("S1");
		list.add("S3");
		list.add("S4");
		list.add("S3");
		list.add("S1");
		
ArrayList tempList = new ArrayList();
Set set1 = new HashSet(list);

for (String setString : set1) {
if (Collections.frequency(list,setString) > 1) {
tempList.add(setString);
}
No posts.
No posts.