开发者

Need Help Naming Class

开发者 https://www.devze.com 2023-03-27 05:39 出处:网络
I have a class which analyzes a string and if the string is not null or an empty string, it creates a new String or new Date using the object depending on the type of subclass.If the string is null or

I have a class which analyzes a string and if the string is not null or an empty string, it creates a new String or new Date using the object depending on the type of subclass. If the string is null or empty it returns an empty string. Currently I call this class converter however I feel this name is misleading, is there a better name anyone can think of for what this class is doing? I want something intuitive that will make my code more readable. Thanks.

public abstract class Converter {
    Object returnObject;

    public Converter() {

    }

    public Object convert(String value)
    {
        if(!this.is开发者_StackOverflowEmpty(value))
        {
            this.setReturnObject(value);    
        }else
        {
            this.returnObject = "";
        }
        return this.getReturnObject();
    }

    protected boolean isEmpty(String value)
    {
        return (value != null && value.equalsIgnoreCase(""));
    }

    protected abstract void setReturnObject(String value);

    protected Object getReturnObject(){
        return this.returnObject;
    }
}

public class NumberConverter extends Converter {
    public NumberConverter() {
    }

    protected void setReturnObject(String value) {
        this.returnObject = new Number(Integer.parseInt(value));
    }
}


You can use either EntityMapper or EntityTransformer.


The 'Entity' prefix is not appropriate because the class is not taking an entity, but a String.

I also would not choose transformer (or converter) because the original value does not change (being an immutable String kind of rules that out).

I would go for StringMapper or maybe StringParser (as suggested by Burleigh Bear).

On a side note, you could use generics to specify the mapped/parsed types and make this code a bit more typesafe.


i guess abstract class doesnt contain any method definations inside it...!! better check that thing first..!!

and as far as naming is concerned i would give agree with sathwick. Thanks. :)


Since you're taking a string and parsing it, I'd say "Parser".

0

精彩评论

暂无评论...
验证码 换一张
取 消