Does a create() method exist as part of a pattern? [closed]

Is there something special(i.e. a design pattern being related) to a method named create()?

Background

The case is, I often make some instances of objects (of the same class/prototype). These objects do not always already objects reflect a state in which they are complete yet. Potentially some initialization needs yet to be done to make them more completly initialized (which cannot be done in the constructor function/method) since for some reasons the information to do so might existing already. An alternative making of the objects once this information is available is made difficult since the objects already made are made available via references to other objects (even though some functionality is not yet possible because of the some missing initalization).

In order to have a way to complete the initialization of the object I would like to call a method .create([...]) on the objects which should be a way for a second step completion of some of the objects initialization.

My interest is if related to this background is if this is a legitimate/reasonable course of action (i.e. to have some objects that may have different states of readyness; which are made into a “complete” state via a method – I thought to name that .create([...]))?

Specifically I would of course like to know if there are for instance already patterns with standard method names for the described task (two-step initialization of objects) and also if the method name .create[...] chosen in worst case conflicts with some existing pattern and would confuse readers of my code.

3

From what I have both read and experienced, it is not a good practice to have more than one single step in initialization of an object. This is not good because

  1. You need to babysit all such objects, so that the required initialization steps are completed before the objects are used
  2. You would have to expect anyone using your code to also properly babysit their objects.

You can easily see that step 1 is quite error-prone, as one might forget some of the steps, or have a hard time to track whether a certain step completed. And, of course, you should not allow the users of your code to break it easily, meaning you should spare them step 2.

However, there are known exceptions to the above preachings which have made it to the development world and became widespread and proven practices. The most important thing is to not allow programmatically for your objects to be used before being properly initialized. This would imply that you have certain encapsulation mechanisms over the initialization lifecycle. Here are possible approaches I can think of, and the respective use cases in real life:

  • Factory classes

    A factory class, (or simply a factory), is a separate class that exposes a single method (create if you wish), that is used to instantiate your working object. It must return a fully initialized and ready-to-use instance of your class, therefore all the initialization steps happen inside of that method. For instance this code (used C# as language):

    public class FooFactory
    {
        public Foo Create()
        {
            Foo foo = new Foo();
            foo.configure(...);
            foo.init(...);
            foo.complete(...);
            return foo;
        }
    }
    

    This is considered a design pattern, see wikipedia article. In most cases the creation method is called Create or CreateFoo but you can choose whatever suits you.

  • Dependency Injection (DI for short)

    This requires you to get familiar with a dependency injection framework and employ it in your project. The DI framework will take care for passing completely initialized instances of your objects and some implementations support post-constructor initialization steps that are execute before the object is ready for use. This acts in a similar way to the above factory pattern, but the advantages of dependency injection. An example is the popular Spring framework (primarily addressing Java development, has also a .NET port), which allows you to specify both init and destroy methods on your class. The init method can be any method of the class you select, so you can easily fit your intention with the create method to work with that.

    <beans ...>
      <bean class="com.mycompany.Foo" init-method="create">
        ...
      </bean>
    </beans>
    

    See this short article for some reference.

    An alternative to the above approach is to rather specify a static class and method that would initialize your Foo class. For the below example, the createFoo is a static method of the FooFactory class and returns an instance of Foo.

    <beans ...>
      <bean class="com.mycompany.FooFactory" 
            factory-method="com.mycompany.FooFactory.createFoo" />
    </beans> 
    

The above list is not exhausting, there are other solutions that might seem appropriate in certain scenarios.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật