Factory

File BaseName.java

package tests.Factory;

public class BaseName {
    String names;
    
    public void setNames(String names) {
        this.names = names;
    }
}

File FactoryName.java

package tests.Factory;

public class FactoryName {

    public static BaseName getNameDelimiter(String name) {
        BaseName result = null;
        
        if (name.contains(",")) {
            result = new NamesCommaDelimiter();
        } else if (name.contains(" ")) {
            result = new NamesSpaceDelimiter();
        }
        
        return result;
    }
}

File NamesCommaDelimiter.java

package tests.Factory;

public class NamesCommaDelimiter extends BaseName {

    public NamesCommaDelimiter() {
        System.out.println("NamesCommaDelimiter");
    }

    public void specComma () {
        System.out.println("specComma");
    }
}

File NamesSpaceDelimiter.java

package tests.Factory;

public class NamesSpaceDelimiter extends BaseName {

    public NamesSpaceDelimiter() {
        System.out.println("NamesSpaceDelimiter");
    }
    
    public void specSpace () {
        System.out.println("specSpace");
    }

}

File main.java

package tests.Factory;

public class main {

    public static void main(String[] args) {
        System.out.println("hi");

        String name1 = "Jhon Dow";
        BaseName baseName = FactoryName.getNameDelimiter(name1);
        if (baseName.getClass().getSimpleName().equals("NamesSpaceDelimeter")) {
            ((NamesSpaceDelimiter)baseName).specSpace();
        }

        String name2 = "Marry, Jabe";
        baseName = FactoryName.getNameDelimiter(name2);
        if (baseName.getClass().getSimpleName().equals("NamesCommaDelimeter")) {
            ((NamesCommaDelimiter)baseName).specComma();
        }
    }
}