Tuesday, 12 July 2016

Design Principle vs.Design Pattern

Object Oriented Programming
OOP is a programming paradigm that represents concepts as “objects” that have data fields (attributes that describe the object) and associated methods. Objects (instances of classes) are used to interact with one another to design applications and computer programs.

OO basics principles:
Polymorphism
Abstraction
Encapsulation
Inheritance


Design Principles:
Design principles are core abstract principles which we are supposed to follow while designing software architect. Remember they aren't concrete; rather abstract.

OO principle is a set of guidelines that ensures OOP concept.Based on the OOP concept, this defines ways to design better way, a better design.

Examples:
1. Single Responsibility Principle
2. Open Close Principle
3. Liskov’s Substitution Principle
4. Interface Segregation Principle
5. Dependency Inversion Principle


Design Patterns:
They are solutions to real world problems that pop up time and again, so instead of reinventing the wheel, we follow the design patterns that are well-proven, tested by others, and safe to follow. Now, design patterns are specific, there are terms and conditions only in which they can be applied.

OO design patterns (OODPs) are those which provide a general solution to object oriented design based OO principle.

Design patterns are discovered, not invented.

Examples:
Singleton Pattern
Factory pattern.

Sunday, 10 July 2016

Spring Bean

The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.

Any normal Java POJO class can be a Spring Bean that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML definitions.

"super" keyword in java

The super keyword is a reference variablethat is used to refer immediate parent class object. Whenever we create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable.

Usage of java super Keyword
1. super is used to refer immediate parent class instance variable.
class Parent { 
     String classN = "Parent"; 
}
class TestSuper extends Parent { 
     String classN = "Child";
     void display() {
           System.out.println(classN);
           System.out.println(super.classN); 
     } 
     public static void main(String args[]) { 
           TestSuper ts=new TestSuper(); 
           ts.display();
     } 
}
Output:
Child
Parent

2. super can be used to invoke parent class constructor.
class Parent {
     publicParent(String str) {
           System.out.println(str);
     }
} 

class TestSuper extends Parent { 

     TestSuper() { 
           super("call parent");//will invoke parent class constructor 
           System.out.println("Child"); 
     } 

     public static void main(String args[]) { 
           TestSuper ts=new TestSuper(); 
     } 
}

3. super can be used to invoke parent class method
The super keyword can also be used to invoke parent class method.
class Parent { 
     public voidmethod() {
           System.out.println("Parent class");
     }
}
class TestSuper extends Parent { 
     public voidmethod() {
           System.out.println("Child class");
     }
     public voiddisplay() {
           method();
           super.method();
     }
     public static void main(String args[]) { 
           TestSuper ts=newTestSuper(); 
           ts.display();
     } 
}

Note:
1. super() is added in each class constructor automatically by compiler.

super() is provided by the compiler implicitly:
class Parent { 
     public Parent() {
           System.out.println("Parent");
     }
} 

class TestSuper extends Parent { 

     TestSuper() {
           System.out.println("Child"); 
     } 

     public static void main(String args[]) { 
           TestSuper ts=new TestSuper(); 
     } 
}

2. No need of super keyword to call parent class method:
If there is no method in subclass as parent, there is no need to use super.

class Parent { 
     public void method() {
           System.out.println("Parent class");
     }
} 

class TestSuper extends Parent { 
     public void display() {
           method();
     }

     public static void main(String args[]) { 
           TestSuper ts=new TestSuper(); 
           ts.display();
     } 
}

"this" keyword in Java

"this" keyword can be used to represent current object or instance of any class in Java. It can also call constructor of same class and used to call overloaded constructor.

Usage of java this keyword
1. "this" keyword can be used to refer current class instance variable.
"this" keyword can be used to resolve the ambiguity between the instance variable and method/constructor parameter.
class Test { 
     int id; 

     Test(int id){ 
           this.id = id; 
     }
}

2. "this()" can be used to invoke current class constructor.

3. "this" keyword can be used to invoke current class method (implicitly).
class Test { 
     void method1() { 
           System.out.println("method is invoked"); 
     } 
     void method2() { 
           this.method1();//no need because compiler does it for you. 
     } 
     void method3() { 
           method1();
           //complier will add this to invoke method1() method as this.method1() 
     } 
     public static void main(String args[]) { 
           Test test = new Test(); 
           test.method2();
           test.method3();
     } 
}

4. "this" can be passed as an argument in the method/constructor call since it represent current object of class.

5. "this" can be used to return current class instance.
public MyClass getClassInstance(){
     return this;
     //"this" keyword cannot be used in static context i.e. inside static methods or static initializer block.
}

6. We can synchronize on this in synchronized block in Java
synchronized(this){
     //this synchronized block will be locked on current instance
}


Note:
1. "this" is a final variable in Java and you cannot assign value to this.
this= new MyClass (); //cannot assign value to final variable : this

2. If use this inside static context you will get compilation error.
public static void main(String args){
     this.toString{}; //compilation error: non static variable this cannot be used in static context
}

3. Inside a constructor, the call to this(...) must be the first instruction.

Spring/IoC container

The Spring/IoC container is at the core of the Spring Framework. It is responsible to instantiate, wire them together, configure them, and manage their complete lifecycle from creation till destruction.

The spring container uses dependency injection (DI) to manage the components that make up an application. The IoC container gets informations from the XML file and works accordingly.

Major tasks performed by IoC container:
To instantiate the application class
To configure the object
To assemble the dependencies between the objects

Different type of IoC containers:
1. BeanFactory
interface org.springframework.beans.factory.BeanFactory
The XmlBeanFactory is the implementation class for the BeanFactory interface.
Resource resource = new ClassPathResource("applicationContext.xml");  
BeanFactory factory = new XmlBeanFactory(resource); 
Employee s = (Employee) factory.getBean("e");
The constructor of XmlBeanFactory class receives the Resource object so we need to pass the resource object to create the object of BeanFactory.

2. ApplicationContext
interface org.springframework.context.ApplicationContext
The ClassPathXmlApplicationContext class is the implementation class of ApplicationContext interface.
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");


Related Posts Plugin for WordPress, Blogger...