Writing Generic classes and methods Part I

Two likely question to be asked is Why and When to write generic classes and methods ?
1 Why : When you want to saves time and effort, in terms of avoiding redundancy and creating more reusable and more general API.

2. When : You need a generic class that can be reused without writing additional code, thus saving times.

1. Writing Generic Classes
Suppose that we are developing a account library called Account Data Access (ADA) for a program that manages the workers salary of a company. We would write a ADA class for managing SoftwareEngr like the following code:

public class SoftwareEngr {
   public void paySalary(SoftwareEngr data, Double newSalary) {
// code to save software engineer details to database

   public SoftwareEngr withdrawlSalary(Double amount) {
// code to get software engineer details from database...
// ...and return a Software engineer object

This looks pretty fine, as this class does its job well: persisting SoftwareEngr objects to database.

Next, we need to persist Admin objects to database. We would write another ADA class like this:

public class AdminADA {
   public void paySalary(Admin data, Double newSalary) {
// code to save Admin details to database

   public Admin withDrawlSalary(Double amount) {
// code to get admin details from database...
// ...and return Admin object

Hmmm ! Is this AdminADA class similar to the SoftwareEngr class, isn’t it? What if new entity classes added to the system: Mechnical engineer, Security staff, and cleaners, etc? It looks like we will continue creating many ADA classes whose code is almost identical.

This creates redundancy and needs time to write similar classes each time a new entity added to the system.

So how will generics help us to avoid this redundancy and time-wasting?
Using generics, we can write a more general ADA class like the following:

public class GeneralADA {

   public void paySalary(T staffData, Double newSalary) {
// code to save new salary details to database

   public T withdrawlSalary(Double id) {
// code to get staff salary from database...
// ...and return a T object

Here, T is called type parameter of the GeneralADA class. T stands for any type. The GeneralADA class is said to be generified. The following code illustrates how to use this generic class:
GeneralADA softwareEngr = new GeneralADA<>();

SoftwareEngr newSoftwareEngr = new SoftwareEngr();
Double newSalary = “1000.00123”
newSoftwareEngr.paySalary(newSoftwareEngr, newSalary);

SoftwareEngr softEngr = newSoftwareEngr.withDrawl(250);

With this generic class, we can manage other entities easily without writing additional ADAO classes, for example:
GeneralADA adminADA = new GeneralADA<>();

Admin newAdmin = new Admin();
Integer newSalary = 5000;
admin.paySalary(newAdmin , newSalary);

Admin admin = newAdmin.withDrawl(4000);

Now you can see clearly how we can write one generic class that works with different types. This removes redundancy and makes the code more readable, more reusable and more general. Also, when a new type is added, the generic class can be reused without writing additional code, thus saving times.


One thought on “Writing Generic classes and methods Part I

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: