Adapter Pattern with Composition

Adapter pattern makes two incompatible interfaces to work together. These Incompatible interfaces are not designed and developed for one another, and assuming they have evolved separately.

Adapter Pattern can be implemented using any of the below.

  1. Inheritance
  2. Composition

Implementing Adapter pattern with Composition

In below example NewArithmetic and Arithmetic are classes, which are incompatible. And they have been designed separately, and one is not designed for another.

Now ArithmeticAdapter is implemented to make NewArithmetic and Arithmetic classes, compatible with each other.

Here ArithmeticAdapter contains Adaptee, i..e NewArithmetic

public class NewArithmetic
{
    public float add_values(float val1, float val2)
    {
        return val1+val2;
    }

    public float sub_values(float val1, float val2)
    {
        return val1-val2;
    }
}

public class ArithmeticAdapter{
    private NewArithmetic obj;

    public ArithmeticAdapter()
    {
        obj = new NewArithmetic();
    }

    public int add_adptr(int val1, int val2)
    {
        return (int)obj.add_values(val1,val2);
    }

    public int sub_adptr(float val1, float val2)
    {
        return (int)obj.sub_values(val1,val2);
    }
}

public class Arithmetic
{
    public void add(int val1,int val2)
    {
        System.out.println("Added Result:"+new ArithmeticAdapter().add_adptr(val1, val2));
    }

    public void sub(int val1,int val2)
    {
        System.out.println("Subtraction Result:"+new ArithmeticAdapter().sub_adptr(val1, val2));
    }
}

public class AdapterComposition {
    public static void main(String[] args) {
        Arithmetic obj = new Arithmetic();

        obj.add(23, 42);
        obj.sub(46, 21);
    }

}

Output:
Added Result:65
Subtraction Result:25

You may also like to read: Adapter Pattern with Inheritance

Adapter Pattern with Inheritance

Adapter pattern makes two incompatible interfaces to work together. These Incompatible interfaces are not designed and developed for one another, and assuming they have evolved separately.

Adapter Pattern can be implemented using any of the below.

  1. Inheritance
  2. Composition

Implementing Adapter pattern with Inheritance

In below example NewArithmetic and Arithmetic are classes, which are incompatible. And they have been designed separately, and one is not designed for another.

Now ArithmeticAdapter is implemented to make NewArithmetic and Arithmetic classes, compatible with each other.

Here ArithmeticAdapter inherits from Adaptee, i..e NewArithmetic

public class NewArithmetic
{
    public float add_values(float val1, float val2)
    {
        return val1+val2;
    }

    public float sub_values(float val1, float val2)
    {
        return val1-val2;
    }
}

public class ArithmeticAdapter extends NewArithmetic{
    public int add_adptr(int val1, int val2)
    {
        return (int)add_values(val1,val2);
    }

    public int sub_adptr(float val1, float val2)
    {
        return (int)sub_values(val1,val2);
    }
}

public class Arithmetic
{
    public void add(int val1,int val2)
    {
        System.out.println("Added Result:"+new ArithmeticAdapter().add_adptr(val1, val2));
    }

    public void sub(int val1,int val2)
    {
        System.out.println("Subtraction Result:"+new ArithmeticAdapter().sub_adptr(val1, val2));
    }
}

public class AdapterPattern {
    public static void main(String[] args) {
        Arithmetic obj = new Arithmetic();

        obj.add(23, 42);
        obj.sub(46, 21);
    }

}

Output:
Added Result:65
Subtraction Result:25

You may also like to read: Adapter Pattern with Composition

What is Singleton Design Pattern

Singleton Design Pattern ensures that maximum one object can exist for a class. Below are steps you need to make a class Singleton.

1. Make all constructors private.
2. Add a static method, which creates and returns object of the class.
3. Add a static data member of its type, so that it can be returned, if object already exists.
4. Add a static int data member, which stores number of objects currently exists(whose value will be either 0 or 1, always)
5. In static method(added above in step 2), if number of objects is zero, create new object(by invoking private constructor), and return it. If number of objects is one, return existing object, without creating new object.

Below is example Design pattern code.
public class Abc {

private static int objCount = 0;

//step 3
private static Abc obj;

//step 1
private Abc(){ System.out.println(“This is Constructor”);}

//step 2
public static Abc createInstance(){
if(objCount ==0)
{
objCount++;
obj= new Abc();
System.out.println(“object created”);
}
else
{
System.out.println(“Returning existing object”);
}

return obj;
}

}

public class MySingleton {
public static void main(String[] args) {

Abc nc = Abc.createInstance();
Abc nc1 = Abc.createInstance();
Abc nc2 = Abc.createInstance();
}
}

Output:
object created
Returning existing object
Returning existing object