Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have some code:

/// <summary>
/// Represents Record Locator class
/// </summary>
public class RecordLocator : IRecordLocator
{
    /// <summary>
    /// The Record Locator string, for example: ZT8C4O
    /// </summary>
    public string Name { get; private set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="RecordLocator"/> class.
    /// </summary>
    /// <param name="recordLocator">The record locator string.</param>
    private RecordLocator(string recordLocator)
    {
        Name = recordLocator;
    }

    /// <summary>
    /// Parses the specified record locator.
    /// </summary>
    /// <param name="recordLocator">The record locator string.</param>
    /// <returns></returns>
    public static IRecordLocator Parse(string recordLocator)
    {
        if (string.IsNullOrEmpty(recordLocator))
        throw new ArgumentNullException("recordLocator");

        if (recordLocator.Length != 6)
        throw new ArgumentException("recordLocator.Length != 6");

        return new RecordLocator(recordLocator);
    }
}

/// <summary>
/// Represents Record Locator interface
/// </summary>
public interface IRecordLocator : IHideObjectMembers
{
    /// <summary>
    /// The Record Locator string, for example: ZT8C4O
    /// </summary>
    string Name { get; }
}
  1. What is this design-pattern called? When you have a class, which instantiating itself (and probably have a private constructor)? Another example of this is System.DateTime class of .NET framework.

  2. Is it OK to do it like that?

share|improve this question

closed as primarily opinion-based by MichaelT, GlenH7, Eric King, Thomas Owens Feb 13 at 19:20

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Lazy initialization? –  Robbie Dee Feb 13 at 14:32
2  
This question appears to be off-topic because it is a "name that thing" question. "Name that thing" are bad questions for the same reasons that "identify this obscure TV show, film or book by its characters or story" are bad questions: you can't Google them, they aren't practical in any way, they don't help anyone else, and allowing them opens the door for the asking of other types of marginal questions. See blog.stackoverflow.com/2012/02/lets-play-the-guessing-game –  GlenH7 Feb 13 at 15:18

4 Answers 4

up vote 10 down vote accepted

It is a static factory pattern. In this particular instance it doesn't currently provide anything that a traditional constructor couldn't(IE object pooling or multiple implementation types) so it doesn't add much value, but isn't wrong.

share|improve this answer
6  
It does add something: it makes it clear that the parameter is parsed, which could make the code clearer (or more confusing if the parameter is not parsed, but just assigned to a property). –  svick Feb 13 at 14:47

Besides "static factory" (see Sign's answer), I have also heard the term "creation method". Creation methods have the advantage that they have a specific name that conveys meaning. You can also create several methods with the same parameter signature, e.g. an Angle struct with methods static Angle FromDegrees(double degrees) and FromRadians(double radians). This is not possible with constructors.

share|improve this answer

I can't recognize any established design patterns here. However, parsing a string to build an object is good, I have seen many examples in Java(my primary language) like Integer.parseInt.

You cannot have a constructor for parsing strings because if the parsing fails, you must throw an exception(expensive) or keep an object in undefined state(bad). Parsing method can either generate a new object or return null.

share|improve this answer
2  
Returning null on failure is a bad idea. Using the TryXxx pattern (like e.g. int.TryParse) would be much better. –  svick Feb 13 at 14:53
    
@svick I don't know about any tryParse in Java. I don't think null is a bad idea, exceptions are expensive. –  Silviu Burcea Feb 13 at 14:56

No such pattern exists in GoF book. Nor I heard about this kind of pattern.

Yes it is OK to do that. But be careful, because it limits extensibility.

share|improve this answer
1  
I wouldn't be so quick to answer that something doesn't exist. –  JeffO Feb 13 at 16:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.