Pattern: Part 2 (Factory Pattern)

Design Pattern Series: Part 2 (Factory Pattern)

Let us think of a scenario from the previous blog.

Say there is 3 banks listed for that financial service provider we are making the software. Let us say that we are providing a service to the company and using that service client can get their debit card through the banks. Let us think that by default bank A provide Visa card as debit card, bank B provide Master card and bank C provide Amex card.

The service name will be CardService and it will issue the default card of Bank A if customer of Bank A use this service, default card of Bank B if customer of Bank B use this service and default card of Bank C if customer of Bank C use this service.

This is how we have written the service

This is just an interface and it only know to issue the default card provided by the banks.

Bank A, B and C implements the CardService interface and available to issue the default card.
And we have a enum where we are keeping the name of all banks

Bank A:

Bank B

Bank C

Now let us create a CardServicesFactory class
It will return an object of CardService implemented class after taking the banks enum as parameter.

From the main method let us write following code

Now all the banks can provide their default debit card to the client using the common service

Output 

Git link is provided here to download

https://github.com/iamrezwan/Factory_Pattern

1 thought on “Pattern: Part 2 (Factory Pattern)”

  1. Pingback: Design Pattern Series: Part 2 (Abstract Factory Pattern)

Leave a Reply

Your email address will not be published. Required fields are marked *