Skip to main content

Posts

Showing posts from 2013

Interview question - Database schema design

This is a database schema design interview question I have attended recently. The interviewer asked me to design DB schema for the below data in an efficient and normalized way and write a query to retrieve " first name, last name, department and work phone numbers of  all employees who are managed by Frank Callea". The data is given below in the diagram. The schema that I have designed is given below. I have created 3 tables as below. Employee Table      EMPID FIRSTNAME                        LASTNAME                             DEPTID      MGRID ---------- -------------------------------- -------------------------------- ---------- ----------          1 Trad                             Lin                                       1          2          2 Frank                            Callea                                    2          0          3 Fred                             Snyder                                

Factory Design Pattern using Reflection

Here I have given an implementation of Factory design pattern in Java using reflection.Please refer to the below link for a quick presentation about Java Refection APIs. www.cs.washington.edu/education/courses/.../reflection.ppt I am creating a DB factory which will create an instance of Database manager objects depends on the argument which is passed. Assume that we have an application running in 2 different environment where one environment uses Oracle as a database and another uses Solaris as a database.Upon initialization of the program we have to create a DB manager instance which should return Oracle or Solaris DB manager for the program. Here I have used reflection to avoid modification to the Factory class if we need to support a new data base tomorrow. I have factory class as below, DBFactory.java import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; public class DBFactory { private static HashMap m_Regi

Simple Spring Dependency Injection example

In this article I will show you a simple example for Spring Dependency Injection which is also known as Inversion Of Control (IoC). I am using Spring Tool Suite for this example even though STS is not mandatory for Spring development. According to Wikipedia " Dependency injection is a software design pattern that allows removing hard-coded dependencies and making it possible to change them, whether at run-time or compile-time.". I think this is a precise and well defined definition for dependency Injection design pattern. Now it is our turn to understand how this design patter works and how it help to achieve the goal of design patterns i.e Well defined solutions to commonly existing problems or scenarios. Lets start with an example, We have a Vehicle interface and 2 child classes Car and Truck. Both Car and Truck has 2 methods printModel() and testEngine() inherited. I have created a File-> New -> Spring Project named SpringDI.I have create