Abstract Factory

File Constants.java

package tests.AbstractFactory;

public class Constants {
    public static final String YAHOO = "YAHOO";
    public static final String GOOGLE = "GOOGLE";
}

File XMLDocument.java

package tests.AbstractFactory;

import java.net.URL;

public abstract class XMLDocument {
    protected void getDocument(URL url) {
        System.out.println("getDocument");
    }
    
    protected abstract void parse();
}

File XMLFromGoogle.java

package tests.AbstractFactory;

public class XMLFromGoogle extends XMLDocument {
    public static final String url = "http://google.com";

    @Override
    protected void parse() {
        System.out.println("XMLFromGoogle parse");
    }
    
}

File XMLFromYahoo.java

package tests.AbstractFactory;

public class XMLFromYahoo extends XMLDocument{
    public static final String url = "http://yahoo.com";

    @Override
    protected void parse() {
        System.out.println("XMLFromYahoo parse");
    }
    
}
    

File XMLFactory.java

package tests.AbstractFactory;

public class XMLFactory {
    public static XMLDocument getXMLFrom(String from){
        XMLDocument doc = null;
        
        if (from.equals(Constants.GOOGLE)) {
            doc = new XMLFromGoogle();
        } else if (from.equals(Constants.YAHOO)) {
            doc = new XMLFromYahoo();
        }
        
        return doc;
    }
}

File main.java

package tests.AbstractFactory;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class main {
    public static void main(String[] args) {
        try {
            XMLDocument xml = XMLFactory.getXMLFrom(Constants.GOOGLE);
            xml.getDocument(new URL(XMLFromGoogle.url));
            xml.parse();
        } catch (MalformedURLException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}