QA's approach 2 Java - Business logic vs Execution logic class

Business logic vs Execution logic class

Business logic classes are helper classes, which doesn't contain main method.
Execution logic class is the one which contains the main method from where the actual execution begins.

Example using Singleton static factory method:


Create a new java file with BusinessLogic & define the business logic & save it

public class BusinessLogic {

static String name = null;

static private BusinessLogic bl = null;

private BusinessLogic(String name) {

this.name = name;

}

//Singleton Factory Design Pattern
public static BusinessLogic getObject(String name) {

if (bl == null) {

bl = new BusinessLogic(name);

return bl;

} else

return bl;

}

}


Output:

Run BusinessLogic.java



Use the command > Alt+Shift+X, J









Create a new java file with ExecutionLogic, define the execution logic & save it

public class ExecutionLogic {

public static void main(String[] args) {

BusinessLogic bl1 = BusinessLogic.getObject("AJAY");

System.out.println("Memory address of the object bl1 created is:" + bl1.hashCode() + "Name is:" + bl1.name);

BusinessLogic bl2 = BusinessLogic.getObject("AJAY SREEDHAR J");

System.out.println("Memory address of the object bl1 created is:" + bl2.hashCode() + "Name is:" + bl2.name);

}

}


Output:

Run ExecutionLogic.java

Use the command > Alt+Shift+X, J



Debugging the Solution:

Place a breakpoints, where ever we wanted to stop execution.

Click on Debug icon or press F11

We can see from the above, the following sequence of steps

1. main thread is called implicitly by JVM

2. main thread now calls business logic class, static getObject(String name) method for Business class object creation.

3. getObject method calls parameterized private constructor ->private BusinessLogic(String name)
& initializes name field with "AJAY". The returned object is stored in bl1.

4. Memory address of the object bl1 & value of Name field is printed on to console.


 5. Similar main thread calls getObject method again, steps 2 are repeated for different parameter "AJAY SREEDHAR", but since the object is already initialized, it will return the existing object instead of creating new object - "Concept of Singleton Design Pattern" 

4. Hence the same Memory address is even referenced by object bl2 & value of Name field is printed as "AJAY" to console.


Output is printed on console as follows


Example using Non Singleton static factory method:


Create a new java file with BusinessLogic & define the business logic & save it 

public class BusinessLogic {

static String name = null;

static private BusinessLogic bl = null;

private BusinessLogic(String name) {

this.name = name;

}

//Non Singleton Factory Design Pattern
public static BusinessLogic getObject(String name) {
return new BusinessLogic(name);

}

}

Create a new java file with ExecutionLogic, define the execution logic & save it 

public class ExecutionLogic {

public static void main(String[] args) {

BusinessLogic bl1 = BusinessLogic.getObject("AJAY");

System.out.println("Memory address of the object bl1 created is:" + bl1.hashCode() + "Name is:" + bl1.name);

BusinessLogic bl2 = BusinessLogic.getObject("AJAY SREEDHAR J");

System.out.println("Memory address of the object bl1 created is:" + bl2.hashCode() + "Name is:" + bl2.name);

}

}

Output printed on the console is as follows:

Every time a new memory location is created & different values are assigned to Name field



Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?