Skip to main content

Factory Pattern

Factory Pattern


       Factory Pattern is way to create object in such Way using Common Interface without exposing the creation logic

Simple Example
     
Example:
step  1.  creation of interface.

    public interface BANK {   
    double getIntrest();
  }

step  2.
 implementation of interface for BOB.

public class BOB implements BANK {

    @Override
    public double getIntrest() {
        return 8.7;
    }
}


step 3.
 implementation of interface for SBI
.
public class SBI implements BANK{

    @Override
    public double getIntrest() {
        return 5.7;
    }
    
}

step 4.
 implementation of interface for UNION  .

public class UNION  implements BANK{

    @Override
    public double getIntrest() {
        return  10.2;
    }
    
}


step 5.

class Imp to get Factory Based on input from user.

public class Factory {

    public BANK getBank(String bankName) {
        switch (bankName) {

            case "SBI":
                return new SBI();
            case "BOB":
                return new BOB();
            case "UNION":
                return new UNION();

        }
        return null;
    }
}

step 6.


public class BankTest {

  
    public static void main(String[] args) {
       Factory ff=new Factory();
        BANK bank = ff.getBank("SBI");
        double intrest = bank.getIntrest();
        System.out.println("Interest is:"+intrest);
               
    }
    
}

Result:
Interest is:5.7


Comments

Popular posts from this blog

Java 8 installation

Java 8 installation setup:       First of all ,we need to install java 8 jdk in your  machine.so for that you can download  it from following  url. click here download  jdk 1.8.0_11 and install it on your machine. Once installation has been done. Please checkout version of jdk. C:\Users\hp>java -version java version "1.8.0_11" Java(TM) SE Runtime Environment (build 1.8.0_11-b12) Java HotSpot(TM) Client VM (build 25.11-b03, mixed mode, sharing) once you have installed Java on your machine, you would need to set environment variables to point to correct installation directories                                                       ...

Reading csv/Google Sheet in Python

 Reading google sheet file using function: File_Url= "https://docs.google.com/spreadsheets/d/458qrjGFH9CD050rRwEHfayWVd4x9VheVAiZkGsj4lIo/export?format=tsv" def getGoogle(google):     import pandas as pd     import numpy as np     import sys     if sys.version_info.major == 3:         import ssl         ssl._create_default_https_context = ssl._create_unverified_context     try:         df = pd.read_csv(google, delimiter='\t').rename(columns=lambda x: x.strip())     except Exception as e:         import requests, io         x = requests.get(url=google).content         df = pd.read_csv(io.StringIO(x.decode('utf8')), delimiter='\t').rename(columns=lambda x: x.strip())     df = df.replace({np.nan: None})     return df.to_dict('records')     googleSheetRecord=getGoogle(File_Url) ...

Interface in Java

Interface:          An interface is a nothing but  reference type,it is same as class.In short Interface is collection of Abstract Method .          It can  also contain  final, static fields  .               you can't create object of Interface.    it can't have Constructor.   Interface can't implement another interface it can extend another one. Interface not extended by class but implemented. Declare Interface: public interface Example {     // final, static fields  .     //Method } Example 1.create interface that contain method. public interface IA {     public void show(); } 2.implement by class  public class ClassA  implements IA{     @Override     public void show() {         System.out.println("from class A called");   ...