I have to create an object X using properties of object Y (both are of same type) in 4-5 different ways i.e. depending upon situation, those properties of Y can be used to initialize X in different ways. One way to do is , create an object X using default constructor and then set its properties, but it has a disadvantage that if some problem occurs then we have an object in inconsistent state. Another way to do is create different constructors for all the cases with dummy parameters, which sounds really bad.
Is there any good design pattern i can use here ?
I have to create an object X using properties of object Y (both are
Share
If both objects are of the same type, you can use factory methods:
If types are different, you can make factory methods static and pass Y as a parameter.