First lets understand what Design Pattern means .In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.
Intent of Factory Pattern - Define an interface for creating an object but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses
Now lets jump directly to code by creating basic Person class...
Next we will create Shopper class extending Person class...
Now lets create another class named Employee...
After creating classes we will define function named userFactory which will be used to instantiate classes Employee and Shopper..
Now lets use userFactory function to create instances of Employee and Shopper classes...
Hope you like the post. See you again on next post...
Comments
Post a Comment