Skip to main content

Factory Pattern

Factory pattern comes from creational design pattern category.main objective of this pattern is to instantiate an object and Factory pattern interface is responsible for creating objects but the sub-classes desides which class to instantiate.


Im going to start with a simple example.Suppose an application asks for entering the name and sex of a person. If the sex is Male (M), it displays welcome message saying Hello Mr. <Name> and if the sex is Female (F), it displays message saying Hello Ms <Name>.


First Im going to create a Person class..

public class Person {

    public String name;
    public String gender;

    public String getName() {
        return name;
    }

    public String getGender() {
        return gender;
    }
}


we have 2 sub classes which will print welcome messages

public class Male extends Person {

    public Male(String name) {
        System.out.println("Mr. " + name);
    }
}


public class Female extends Person {

    public Female(String name) {
        System.out.println("Ms." + name);
    }
}


Im going to create a WelcomeMessageFactory class which will return welcome messages according to the input data..


public class WelcomeMessageFactory{

    private static void main(String args[]) {
        WelcomeMessageFactory factory = new WelcomeMessageFactory();
        factory.getPerson(args[0], args[1]);
    }

    public Person getPerson(String name, String gender) {
        if (gender.equals("M")) {
            return new Male(name);
        } else if (gender.equals("F")) {
            return new Female(name);
        } else
            return null;
    }
}


This class accepts two arguments from the system at runtime and prints the names and sex..


after compiling run the program with 
java Madura M


out put is 
Mr. Madura

Comments

Popular posts from this blog

Reverse Proxy configuration with WSO2 Identity Server 5.0.0

Reverse proxy is a type of a proxy which can hide back end servers from the client applications. According to the above figure, Original servers are not exposed to the internet. Only reverse proxy is exposed to the internet.Client knows only the reverse proxy IP address. So he thinks that he is sending a request to the reverse proxy.He doesn't know anything about the original server. You can avoid some attacks using this architecture. Today I'm going to configure Apache HTTPD server(reverse proxy) and WSO2 identity server 5.0. Please download WSO2 identity server 5.0 from here You can install apache httpd server using below commands sudo apt - get update sudo apt - get install apache2 Restart the newly install apache server sudo service apache2 restart Apache is a modular server. This implies that only the most basic functionality is included in the core server.So You have to enable few other required features. Please use below command ...

Logging parameters in WSO2 ESB Script mediator

Script mediator is helpful in many ways and it can be written in different scripting languages such as JavaScript, Groovy and Ruby. I'm going t o use JavaScript for this example. I'm trying to show you the way that you can use to log some values when you are writing a mediator. This will be helpful when you need to log values when you are writing a complex script mediators. This is a sample script <script language= "js" > var log = mc.getServiceLog(); log.info( "started" ); var temp_auth = mc.getProperty('authheader'); log.info( "temp_auth : " + temp_auth); log.info( "out" ); </script> When you define a " var log = mc.getServiceLog();" variable, you can use log object to display values as below. log.info( "started" ); log.info( "temp_auth : " + temp_auth); This is a sample proxy service <?xml version= "1.0" encod...

CURL commands to get access token from WSO2 Identity Server

WSO2 Identity server supports all grant types those are defined in oAuth2 core specification Four grant types: Authorization Code Grant Implicit Grant Resource Owner Password Credentials Grant (password) Client Credentials Grant We cannot use curl command directly to get an access token for Authorization code grant type and Implicit grant type. I'm going through other two grant types one by one and provide the curl command to get access token. 3. Get access token using password grant type curl -k -d "grant_type=password&username=admin&password=admin" -H "Authorization: Basic ME5fbXdWRXpTVnhfalJIbDV2cmc4RHIycHZBYTp0RmZjcHVFRFM5V1d2eFFEc1ZCd0tWVGd0dE1h, Content-Type: application/x-www-form-urlencoded" https://localhost:9443/oauth2/token ME5fbXdWRXpTVnhfalJIbDV2cmc4RHIycHZBYTp0RmZjcHVFRFM5V1d2eFFEc1ZCd0tWVGd is the encoded value of 0N_mwVEzSVx_jRHl5vrg8Dr2pvAa:tFfcpuEDS9WWvxQDsVBwKVTgttMa (<client_id>:<client_secret>). these...