A few days ago I had the opportunity to talk about the meaningfulness of use of factory object vs. builder. So I decided to refer to this conversation and write a few sentences about the design patterns in Java.
When it comes to patterns associated with the creation of objects, it is worth mentioning:
Singelton which is used when we need ensure that only one instance of a class is created and provide a global access point to the object (the instance must be available through all the code). As a typical use case we can point loggers (logger classes), configuration classes, classes dedicated to share resources across user or application session, factory classes and so on. A special care about singelton should be taken in multithreading environments –cause of possible conflicts when access to shared resources via singleton is performed.
Builder classes are used for creating an object but letting subclasses decide which class to instantiate. Builder allows a client object to construct a complex object by specifying only its type and content, without knowledge about really performed steps od creation complex object. Client refers to the newly created object through a common interface
Factory classes are used to create objects without exposing the instantiation logic. Client class refers to the newly created object through a common interface. This pattern is used when a developer needs delegate the creation of objects derived from a common superclass to the factory to provide flexibility in adding new types of objects that must be created by the class.
Abstract factory mechanism delivers possibility to create a family of related objects without specifying a concrete classes. Only the common interfaces of newly created objects are available, implementation is hidden to the clients. The good example of this pattern is java.sql.Connection class which is the abstract factory and allows you to create Statements, PreparedStatements and so on.
Virtual constructor also known as Factory method defines an interface for creating objects, but lets subclasses to decide which type of object should be generated. The mechanism is similar to Factory pattern but leaves more flexibility, especially when factory class does not know what concrete classes will be required to create
Prototype pattern is used when we need specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. In simple words: the prototype we should understand as cloning. The pattern (prototype object) is cloned rather than created from scratch. Cloning is used when we consider creating a new object as too expensive or eventually when cloning is simple more convenient way to get new object instance.
Pool is used when a several clients needs the same, stateless, expensive to create, resources. In this situation using object pool may be more efficient than creating new instance for each new client. Typically pools are used for serving database connections.
No comments:
Post a Comment