Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6619463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:58:18+00:00 2026-05-25T20:58:18+00:00

It seems like I see a lot of class methods in Objective C that

  • 0

It seems like I see a lot of class methods in Objective C that are like +(NSString*)stringWithString:(NSString *)string or +(NSArray)arrayWithArray:(NSArray *)array, etc. I am just starting to consider design patterns and to me these methods look like little factories that produce specific implementations of strings or arrays based on the paramaters provided (stringWith string , string ByAppendingString). In essence this looks a lot like the factory method with parameters demonstrated in the book Design Patterns. Is there a better way to do this? Should I be creating interfaces that mix these class methods and instance methods, or create Factory only objects that do not have any instance methods? I am confused.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:58:19+00:00Added an answer on May 25, 2026 at 8:58 pm

    While Apple claims that these methods are factory methods, I argue that they’re not the same as the Factory Method pattern in Design Patterns. The DP Factory Method pattern uses an abstract Creator class, with concrete subclasses that generate concrete Product classes. The methods you’re describing (which are often called “convenience constructors”) are almost never implemented that way. Apple’s definition of a factory method is “a class method that, as a convenience to clients, creates an instance of a class. A factory method combines allocation and initialization in one step and returns an autoreleased instance of the class.” That’s why I believe the term “convenience constructor” is more appropriate and less confusing that “factory method.”

    NSString is a class cluster, which has some passing similarities to the Factory Method pattern, in that you can receive different concrete classes from the same method call. But class clusters are different than Factory Method in that the superclass knows about all the subclasses, and it is the superclass that callers interact with. In the Factory Method pattern, callers interact with subclasses of the Creator class.

    So to your question: you should create convenience methods when it is convenient to the caller. It supplements:

    Foo *foo = [[[Foo alloc] initWithSomething:something] autorelease];
    

    with

    Foo *foo = [Foo fooWithSomething:something];
    

    My experience building these is that 90% of the time you shouldn’t. It’s not worth the extra code. You should wait until you see that initWithSomething: is being called a lot with an autorelease right after it, and then you add a convenience constructor to make things more convenient. Things like stringWith... are in this class. They’re called a lot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Typical scenario: a class that a lot of people have worked on. I'd like
First of all I must say that this may seem like a lot of
I noticed in a custom base class that we use for a lot of
Well.. it seems like enum classes in Java are a lot more versatile than
Seems like MySQL Bug; Request: SELECT * FROM table WHERE ( id LIKE '%тест
Seems like a simple enough question but I can't seem to find the answer.
Seems like a simple problem: I have an SVN repo inside our firewall. I
Seems like the subtraction is triggering some kind of issue and the resulting value
Seems like as really simple thing to do, but I just can't track it
Seems like cuke doesn't show the full error message (at least when problem occurs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.