I just read a book on object-oriented programming patterns. It describes a Factory pattern by which you can make a call to a static factory method of an object and the object will return a new object of expected type, but it will be subclassed appropriately.
My question, can this functionality be provided by a constructor for the class? If so how? I f not why? Imagine I have a class called VillagePerson, which has subclasses PoliceOfficer, NativeAmerican, Cowboy, Biker, ConstructionWorker, and Sailor. The constructor takes a text string description of the person, parses it, and returns the specific type of person that I want. Is this only possible with static factory methods?
This is a paraphrase of Steven Sudit’s comment
Create a Proxy class on top of the
VillagePerson:VillagePerson, but does not inherit any implementation code from it._vpImplto a subclassed VillagePerson object._vpImplobject.To construct a subclassed
VillagePerson, the Proxy’s constructor could call the specific constructor of the subclass and then store it in_vpImpl.