Introduction to Abstract Factory Design Pattern

Abstract factory design pattern provides an interface for creating families of related or dependent objects without a specifying their concrete classes.

The abstract factory design patter is very similar to factory method pattern. One difference between the two is that with the abstract factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the factory method pattern uses inheritance and relies on subclass to handle the desired object instantiation.

Actually, the delegated objects frequently factory methods to perform the instantiation.

abstract factory design pattern

Applicability

Use the abstract factory pattern in any of following situation.

  • A system should be independent of how it’s products are created, composed, and represented
  • A class can’t anticipate the class of objects it must create
  • A system must use just one of a set of families of products

A family of related product objects is designed to be used together, and you need to enforce this constraint.

 

Participants

  • AbstractFactory

o Declare a interface for operations that create abstract products objects

  • Concretefactory

o Implements the operations to create concrete product objects

  • AbstractProduct

o Declare an interface for type of product object

  • ConcreteProduct

o Define a product object to be created by the corresponding concrete factory

o Implements the AbstractProduct interface

  • Client

o Uses only interfaces declare by AbstractFactory and AbstractProduct classes

 

Collaborations

Normally single instance of ConcreteFactory class is created at run time. (This is example of Singleton Pattern) This concrete factory creates product objects having particular implementations. To create different product objects, clients should use a different concrete factory.

AbstractFactory defers creation of product objects to its ConcreteFactory

 

Example

Let see how Abstractfactory design pattern applied to charting application which generate the chart depending upon user selection like bar chart, pie chart

  • First create an interface IChart which have drawChart method take a string arg

publicinterface IChart {

public void drawChart();

}

  • Create a abstract class say ChartAbstractFactory which have abstract method as getchart which takes string arg and return the interface type

publicabstractclass ChartAbstractFactory

publicabstract IChart getChart(String ChartType);

}

 

  • Create a class say BarChart and impelmets IChart interface

publicclass BarChart implements IChart {

@Override

public void drawChart () {

System.out.println(“charting Logic”);

}

}

 

  • Create a class ChartFactory which extends the ChartAbstractFactory

publicclass ChartFactory extends ChartAbstractFactory {

@Override

public void getChart(String chartType) {

if(chartType == null){

return null;

}

if(chartType == BarChart){

return new BarChart();

}

elseif(chartType ==PieChart)

{

return new PieChart();

}

}

return null;

}

  • Crate a class say FactoryProducer to get factories by passing information such as Chart

publicclassFactoryProducer {

    public static ChartAbstractFactory getFactory(String choice)

{

If(choice==chart)

{

return new ChartFactory();

}

return null;

}

}

  • Create a Client with main method

publicclass AbstractFactoryDemo{

ChartAbstractFactory factory = FactoryProducer.getFactory(“chart”);

IChart chart = factory. getChart(BarChart);

chart.drawChart();

}

 

Muqtar Ahmed

Helical IT Solutions

CREATE EVENT IN MYSQL AND SOME ADVANCED MYSQL COMMANDS

Dear Readers, this blog will be talking about how to create even in MySQL and some advanced MySQL commands. To create event in MySQL, we have to follow a Pattern which is written below..

1. Declare Delimiter

2. Define Name of the event

3. Define when to schedule

4. Start with “DO”

5. Then “Begin”

6. Define Business logic

(Like variable declaration, job which you want to schedule through your Event, any condition…So mainly it is the body of your Event)

7. Then Declare end of your event like “END <delimiter>”

8. Change Delimiter to normal Delimiter.

Example:- Here I tried to call a stored procedure in this event which is scheduled after every 4-Hour, and passed the parameter for that stored procedure by taking two date parameter, and also with some additional parameters, I tried to use loop and if-else condition also in this event…     (Tested and executed Event)

delimiter $$

CREATE EVENT ue_schedule_test

ON SCHEDULE

EVERY 4 HOUR

DO

BEGIN

DECLARE to_temp TEXT(25);

DECLARE from_temp TEXT(25);

DECLARE pv_temp TEXT(20);

DECLARE done INT DEFAULT FALSE;

DECLARE curs1 CURSOR FOR SELECT name FROM data ;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN curs1;

read_loop: LOOP

FETCH curs1 INTO pv_temp;

SELECT DATE_FORMAT(DATE_ADD(convert_tz(CURDATE(),'SYSTEM','+00:00'),INTERVAL -4 HOUR),'%Y-%m-%d %H:%i:%S') INTO from_temp;

SELECT DATE_FORMAT(DATE_ADD(convert_tz(CURDATE(),'SYSTEM','+00:00'),INTERVAL 4 HOUR),'%Y-%m-%d %H:%i:%S') INTO to_temp;

DELETE FROM availibility WHERE Date=from_temp;

CALL usp_availability_test(from_temp,to_temp,pv_temp,'Total','+00:00','custom');

IF done THEN

LEAVE read_loop;

END IF;

END LOOP;

CLOSE curs1;

END $$

delimiter ;

Some Other Important My-Sql Commands
1. To clear console — \! clear
2. To delete procedure — drop prcedure
3. Show all stored procedure — show procedure status
4. To get 2nd highest salary —SELECT DISTINCT(Salary) FROM employee ORDER BY Salary DESC LIMIT 1,1
5. To get 3rd highest salary —SELECT DISTINCT(Salary) FROM employee ORDER BY Salary DESC LIMIT 2,1
6. To convert string into datetime type —SELECT STR_TO_DATE(yourdatefield, ‘%m/%d/%Y’) FROM
7. To check event scheduler is ON/OFF — select @@event_scheduler
8. To start event-scheduler — set GLOBAL event_scheduler=ON
9. To delete duplicate records from table
— delete from table1 USING table1, table1 as vtable
WHERE table1.ID<vtable.ID AND table1.field_name=vtable.field_name;
So, These are the some Advanced My-SQL features , which may help you…

Have a Good Day………!!!
PUSHPRAJ KUMAR
Helical IT Solutions

Make Batch count for Incremental loading in Talend (TOS_5.3)

This blog will talk about how to make Batch count for Incremental loading in Talend (TOS_5.3).

First all we have t_source and t_target tables
Both Tables have (t_source,t_target) data like this….

before_execute_job

Objective

INSERT into t_source

We inserted one record into t_source
Insert into t_source(id,name,city,s_date) values (111,’vic’,’del’,’2014-03-01 01:02:00′)
UPDATE from t_source
We updated from t_source
Update t_source set name=’don’,s_date=’2014-02-01 01:02:00′ where id = 109;
DELETE from t_source
We deleted from t_source
Delete from t_source where id = 108;

Finally we have records from t_source table and t_target tables

update_Tsource

We want make Batch count in TALEND(TOS)

We created one job…

test

Details of Job

Subjob (1)

We are fetched max(id) from t_target table and we updated into property variable

context.last_id = input_row.id;

Subjob (2)

We are fetching min (id) and max (id) from t_source and we updated into property variables

context.sr_max_id = input_row.max_id;

context.sr_min_id = input_row.min_id;

Subjob (3)

we are selecting from t_source

select * from t_source where  id > “+context.last_id+” order by id

and insert into t_target table by primary key is id

Subjob(4)

we need to count between primary key from t_source

select count(*) as batch_count from t_source where id between “+context.sr_min_id+” and “+context.sr_max_id+”

and updated into property variable. We want to calculate Batch count

We will define by divide count (context.MT_COUNT = 5)  . context.max_count, context.min_count is 0 before Execution of job.

context.count = input_row.count;

System.out.println(“Count of primary key from source “+context.UPLOAD_FILE_NAME+” Table : “+context.count);

 

int x = (context.count / context.MT_COUNT) + 3;

context.batch_count = x;

System.out.println(“Batch Count : “+context.batch_count);

context.max_count = context.MT_COUNT;

 

context.min_count = context.sr_min_id ;

context.max_count = context.sr_min_id + context.max_count;

SubJob (5)

We will iterate by context.batch_count. We have another job(test123) by Iterating.

1.Test123 Job

test123a.       SubJob(5.1)

We are printing Batch count min_count to max_count

System.out.println(“Batch “+Numeric.sequence(“s1″,1,1)+”: Count of “+context.min_count+” to “+context.max_count);    

b.      Subjob(5.2)

We are selecting from t_source between primary key

select * from t_source  where id >= “+context.min_count+” and id <= “+context.max_count+” order by id

and collects data into Buffer output

c.       SubJob (5.3)

We compared by inner join from Buffer input(t_source) and t_target tables in tmap. If any reject output will be there then updated into t_target.

T_target sql query: select * from t_target  where id >= “+context.min_count+” and id <= “+context.max_count+” order by id

d.      SubJob (5.4)

We compared by left outer join from t_target and Buffer input(t_source) in tmap. We filtered t_source.id == 0 and if any output is there then deleted

T_target sql query: select * from t_target  where id >= “+context.min_count+” and id <= “+context.max_count+” order by id

And we have t_javarow(Min, Max of  p_key)

In that,

context.min_count = input_row.id;

context.max_count = context.min_count + context.MT_COUNT;

Results

We Executed the job by defined (property variables)MT_COUNT = 5.

extecion

 

Finally we have records from t_source and t_target.

afterexecited

Thanks & regards

Vishwanth suraparaju

Senior ETL Developer