Skip to main content

Posts

Showing posts with the label Object Oriented Design

Behavioral Patterns : Template Method

Properties: -            Abstract class defines steps to achieve some task. At some points it defers actions to subclasses.   -            Common steps are done within abstract class so code repetition is avoided. -            Subclasses defines subclass specific steps. Implementation: -            AbstractClass  defines template method in which generic algorithm is formed. At subclass specific points abstract mehods are called to defer specific tasks to subclasses. -            ConcreteClass  extends abstract class and defines abstract methods to implement tasks in a specific way to it. Java Standard Library Implementations: -            java....

Behavioral Patterns : Strategy

Properties: -            Separates implementation from algorithm (strategy).   -            The same class can be used with different algorithms. Implementation: -            Strategy  creates an abstraction for different algorithms. -            ConcreteStrategy  implements strategy interface for a specific algorithm. -            Context  object uses strategy object to do its job. Its behaviour depends on the concrete strategy implementation. Java Standard Library Implementations: -            java.util.Collections.html#sort(java.util.List, java.util.Comparator)  uses Comparator as strategy. Example Usage:     ...

Creational Patterns : Prototype

Properties: -            When cost of creating an object from scracth is high it might be good idea to create it by cloning an existing object. -            Identified by a super class declaring a clone method and implemented clone method inside class. Implementation: -            Class extends a super class which declares a clone method. -            Clone method is implemented to create a copy of the object. Java Standard Library Implementations: -            Any class implementing  java.lang.Cloneable Example Usage: // simply person clone is invoked Person newPerson  =   ( Person ) person . clone ();

Creational Patterns: Object Pool

Properties: -            When cost of creating an object from scratch is high it might be a good idea to reuse already created objects. -            Identified by a pool class that objects are retrieved from and then released back to. Implementation: -            Reused objects might be wrapped by another object. -            Pool class keeps created objects. -            Client requests a new object instance from pool when it needs a new object. After client is done with object instance it releases the object, so that the object can be reused again. Java Example Usage: -            com.mchange.v2.c3p0.ComboPooledDataSource Example Usage: // create connection ...

Behavioral Patterns : Null Object

Properties: -            Simply providing a do-nothing implementation of a particular interface. -            Concrete class does nothing, all requests are effectively ignored. Implementation: -            AbstractOperation  declares operations. -            RealOperation   implements AbstractOperation and defines operations. -            NullOperation   implements AbstractOperation and leaves operations empty. -            Client  executes operations on AbstractOperation, does not care about if implemantation is real or null. Java Standard Library Implementations: -            java....

Behavioral Patterns : Memento

Properties: -            Can be used when object state needs to be saved between interactions. Implementation: -            Originator class is the class of which state needs to be saved. It should be able to save its state into Memento objects and restore its state from Memento objects. -            Memento objects keep state of originator object. -            Caretaker is the client code which saves and restores state of originator using Memento objects. Java Standard Library Implementations: -            javax.faces.component.   StateHolder implementations -            java.sql.Connection Example Usage: try {     //assume connection is acq...