Sunday, 25 October 2015

Inject Spring inner bean as Constructor parameter

Spring - Injecting Inner Beans as constructor parameter

package com.rk.inner;
public class Person {
      private String name;
      private Address address;

      public Person(Address address) {
            this.address = address;
      }

      @Override
      public String toString() {
            return name +" lives at "+ address;
      }
     
      // setters and getters
}

public class Address {
      private String area;
      private String city;

      @Override
      public String toString() {
            return "area : "+area+" city: "+city;
      }
     
      // setters and getters
}

Use inner class as property:

Spring-Person-Constructor.xml
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

      <bean id="PersonBean" class="com.rk.inner.Person">
            <property name="name" value="rajesh"/>
            <constructor-arg>
                  <bean class="com.rk.inner.Address">
                        <property name="area" value="dempier"/>
                        <property name="city" value="mathura"/>
                  </bean>
            </constructor-arg>
      </bean>
</beans>

importorg.springframework.context.ApplicationContext;
import org.springframework.context.support.
ClassPathXmlApplicationContext;

public class TestInnerClass {
      public static void main( String[] args ) {
            ApplicationContext context =
            new ClassPathXmlApplicationContext(
            new String[] {"com/rk/inner/Spring-Person-Constructor.xml"});

            Person cust = (Person) context.getBean("PersonBean");
            System.out.println(cust);
      }
}

Output: rajesh lives at area : dempier city: mathura


Inject Spring inner bean using setter

Spring - Injecting Inner Beans as setter injection

package com.rk.inner;
public class Person {
      private String name;
      private Address address;

      public Person() {
      }
     
      @Override
      public String toString() {
            return name +" lives at "+ address;
      }
     
      // setters and getters
}

public class Address {
      private String area;
      private String city;

      @Override
      public String toString() {
            return "area : "+area+" city: "+city;
      }
     
      // setters and getters
}

Use inner class as property:

Spring-Person-Inner.xml
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

      <bean id="PersonBean" class="com.rk.inner.Person">
            <property name="name" value="rajesh"/>
            <property name="address">
                  <bean class="com.rk.inner.Address">
                        <property name="area" value="dempier"/>
                        <property name="city" value="mathura"/>
                  </bean>
            </property>
      </bean>
</beans>

Use inner class as “ref” property:

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

      <bean id="PersonBean" class="com.rk.inner.Person">
            <property name="name" value="rajesh"/>
            <property name="address" ref="addressBean"/>
      </bean>

      <bean id="addressBean" class="com.rk.inner.Address">
            <property name="area" value="dempier"/>
            <property name="city" value="mathura"/>
      </bean>
</beans>

importorg.springframework.context.ApplicationContext;
import org.springframework.context.support.
ClassPathXmlApplicationContext;

public class TestInnerClass {
      public static void main( String[] args ) {
            ApplicationContext context =
                  new ClassPathXmlApplicationContext(
                  new String[] {"com/rk/inner/Spring-Person-Inner.xml"});
           
            Person cust = (Person) context.getBean("PersonBean");
            System.out.println(cust);
      }
}

Output: rajesh lives at area : dempier city: mathura

Thursday, 15 October 2015

Strategy Pattern (Policy Pattern) - A behavioral design pattern

Strategy pattern is used when we have multiple algorithm for a specific task and client decides the actual implementation to be used at runtime.
We defines multiple algorithms/classes and let client application pass the algorithm to be used as a parameter.

Example: Collections.sort() method that takes Comparator parameter. The list can be sorted in different ways using different implementations of Comparator interfaces.
                       

Elements Strategy Pattern

Strategy  (SortStrategy)
Common Interface that implemented by to all supported algorithms (ConcreteStrategy).

ConcreteStrategy  (QuickSort, ShellSort, MergeSort)
Implements the algorithm using the Strategy interface.

Context  (SortedList)
Maintains a reference to a Strategy object and logic to use Strategy object.

package strategy;

interface Strategy {
      public void sort(int[] numbers);
}

class BubbleSort implements Strategy {
      @Override public void sort(int[] numbers) {
            System.out.println("Buuble sort !!");
      }
}

class InsertionSort implements Strategy {
      @Override public void sort(int[] numbers) {
            System.out.println("Insertion sort !!");
      }
}

class QuickSort implements Strategy {
      @Override public void sort(int[] numbers) {
            System.out.println("Quick sort !!");
      }
}

class MergeSort implements Strategy {
      @Override public void sort(int[] numbers) {
            System.out.println("Merge sort !!");
      }
}

class Context {
      private final Strategy strategy;
      public Context(Strategy strategy) {
            this.strategy = strategy;
      }
      public void arrange(int[] input) {
            strategy.sort(input);
      }
}

public class StrategyTest {
      public static void main(String args[]) throws InterruptedException {

            int[] var = {1, 2, 3, 4, 5};
           
            /** Define Bubble sort to at run time. */
            Context ctx = new Context(new BubbleSort());
            ctx.arrange(var); // we can change the strategy without changing Context class
           
            /** Define Quick sort to at run time. */
            ctx = new Context(new QuickSort());
            ctx.arrange(var);
      }
}
Output:
      Buuble sort !!
      Quick sort !!
Related Posts Plugin for WordPress, Blogger...