Factory Pattern

Factory pattern is one of most used design pattern in Java, and it defines an interface to create the object without exposing the creation logic to the client, and has internal logic to decide which class to instantiate.

Factory Pattern

TrackingAgent interface

Concrete Tracking Agents

Factory Class
An application usually needs only one instance of the Factory class. This means that it is best to implement it as a Singleton or a class with static method.

Client

Output
DHL agent tracks 576954654
UPS agent tracks 1Z657576954654
FedEx agent tracks 679676576

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditShare on StumbleUponEmail this to someoneShare on TumblrDigg this

One thought on “Factory Pattern

  1. Pingback: Software Design Patterns

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">