java - Object Allocation through new operator or some interface -
i going through java code , saw objects have been allocated using interface , interface contains methods allocate objects using new operator. unable think why have used interface instead of directly allocating objects using new operator. e.g:
animal animal = new animal(); or
animal animal = interface.allocateanimal() here interface interface having method allocateanimal nothing new animal().
so doing same thing in different way gaining here?
edit 1: interface implemented somewhere else. interface not contain implementation code, contains methods.
first of interfaces not allocating anything. establishing contracts.
what see there factory method pattern. decided implement interface , concrete implementation's method creating animal object. can check link pattern , there you'll find thorough explanation factory method.
there question here reasons using factory pattern might worth checking out. here point:
...using factory allows consumer create new objects without having know details of how they're created, or dependencies - have give information want.
there more: person have written code reading may tried refer programming interface of not technical interface keyword in java. means something else.
Comments
Post a Comment