Design Pattern Basics - v01
Design Pattern Basics - v01
Course objectives
private SingletonClass() {
// Optional Code
}
public static synchronized SingletonClass getSingletonObject() {
if (singletonObject == null) {
singletonObject = new SingletonClass();
}
return singletonObject;
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}
Creational patterns (3 of 16)
Use:
Singletons can be used to create a Connection Pool object or a Logger Object.
Factory Pattern
If we have a super class and n sub-classes, and based on data provided, we
have to return the object of one of the sub-classes, we use a factory pattern.
Depending on the client input, the Factory pattern may create and return an
object which is one of the several sub-classes in consideration. But the reverse
is not always true. Any classes that creates and returns objects should not be
considered as Factory classes.
An example of Factory class is as follows :
In the example that we are looking at for explaining the Factory Design Pattern
is we are creating an object of Dog and Cat without calling the Dog or Cat class.
Creational patterns (4 of 16
Now if you see this class, here we will be passing an argument to the
getMammalObject function based on the argument passed we return the
Mammals object of the class.
Creational patterns (5 of 16)
Here we have created an abstract class Mammals. This class will be used by
the Dog and Cat class.
Creational patterns (6 of 16)
Here we have created Cat class, this class extends from Mammals class so it is
understandable that Cat needs to have the implementation for doWalking()
method.
Creational patterns (7 of 16)
Here we have created Dog class, this class extends from Mammals class so it is
understandable that Dog needs to have the implementation for doWalking()
method.
Creational patterns (8 of 16)
Here if you see that we want to create an object for Dog class and for that we
are not directly loading the Dog class. Instead we are instantiated the object for
MammalsFactory class.
Use:
Factory pattern is used in Data Access Object.
Creational patterns (9 of 16)
// Client
public class Client{
public static void main(String args[]){
AbstractFactory pf=FactoryMaker.getFactory("a");
AbstractProductA product=pf.createProductA();
//more function calls on product
}
}
Use:
An Abstract Factory pattern is used to create a Data Access Object.
Structural patterns (1 of 24)
PrintableList.java(Target):
import java.util.ArrayList;
AdapterDesignPatternMain.java:
import java.util.ArrayList;
public class AdapterDesignPatternMain {
}
}
Structural patterns (7 of 24)
Decorator Pattern
To extend or modify the behavior of an instance at runtime decorator design
pattern is used. Inheritance is used to extend the abilities of a class. Unlike
inheritance, you can choose any single object of a class and modify its behavior
leaving the other instances unmodified. In implementing the decorator pattern
you construct a wrapper around an object by extending its behavior. The
wrapper will do its job before or after and delegate the call to the wrapped
instance. Basically, we wrap the original object through decorator object.
Decorator design pattern is based on abstract classes and we derive concrete
implementation from that classes.
Structural patterns (8 of 24)
}
Structural patterns (10 of 24)
// Concrete Component
public Rupee() {
description = "indian rupees";
}
}
Structural patterns (11 of 24)
public Dollar () {
description = "Dollar;
}
return value;
}
Structural patterns (12 of 24)
// Decorator
}
Structural patterns (13 of 24)
// Concrete Decorator
public class USDDecorator extends Decorator{
Currency currency;
}
Structural patterns (14 of 24)
}
Structural patterns (15 of 24)
//check currency
public class CurrencyCheck {
//adding decorators
Bridge Pattern
The intent for bridge design pattern is to decouple an abstraction from its
implementation so that the two can vary independently. In the bridge pattern, we
separate an abstraction and its implementation and develop separate
inheritance structures for both the abstraction and the implementor. The
abstraction is an interface or abstract class, and the implementor is likewise an
interface or abstract class. The abstraction contains a reference to the
implementor. Children of the abstraction are referred to as refined abstractions,
and children of the implementor are concrete implementors. Since we can
change the reference to the implementor in the abstraction, we are able to
change the abstraction's implementor at runtime. Changes to the implementor
do not affect client code.
Structural patterns (17 of 24)
Refined Abstraction
extends the interface defined by Abstraction
Implementor
defines the interface for implementation classes
ConcreteImplementor
implements the Implementor interface
Structural patterns (18 of 24)
Shape.java(Abstraction):
Color color;
Shape(Color color)
{
this.color=color;
}
abstract public void colorIt();
}
Structural patterns (20 of 24)
Rectangle.java(RefinedAbstraction):
Rectangle(Color color) {
super(color);
}
Circle.java(RefinedAbstraction):
Circle(Color color) {
super(color);
}
Color.java(Implementor):
RedColor.java(ConcreteImplementor):
BlueColor.java(ConcreteImplementor):
BridgeDesignPatternMain.java:
//Command Interface
public interface Command
{
public void execute();
}
Behavioral patterns (4 of 15)
//Concrete Command
public class LightOnCommand implementsCommand
{
//reference to the light
Light light;
}
Behavioral patterns (5 of 15)
//Concrete Command
public class LightOffCommand implementsCommand
{
//reference to the light
Light light;
}
Behavioral patterns (6 of 15)
//Receiver
public class Light
{
private boolean on;
}
Behavioral patterns (7 of 15)
//Invoker
public class RemoteControl
{
private Command command;
}
Behavioral patterns (8 of 15)
//Client
public class Client
{
public static void main(String[] args)
{
RemoteControl control = new RemoteControl();
//switch on
control.setCommand(lightsOn);
control.pressButton();
//switch off
control.setCommand(lightsOff);
control.pressButton();
}
Behavioral patterns (9 of 15)
Iterator Pattern
Iterator pattern provides a way to access the elements of an aggregate object
without exposing its underlying representation. The iterator pattern allows for the
traversal through the elements in a grouping of objects via a standardized
interface. An Iterator interface defines the actions that can be performed. These
actions include being able to traverse the objects and also obtain the objects.
This is widely used java.util.Iterator interface which is used to iterate through
things such as Java collections. We can write our own iterator by implementing
java.util.Iterator. This interface features the hasNext(), next(), and remove()
methods. When writing an iterator for a class, it is very common for the iterator
class to be an inner class of the class that we'd like to iterate through.
Behavioral patterns (10 of 15)
Iterator it = list.iterator();
while(it.hasNext())
{
String s = it.next();
}
}
Behavioral patterns (12 of 15)
Observer Pattern
Observer Pattern defines a one-to-many dependency between objects so that
when one object changes state, all its dependents are notified and updated
automatically. The object, which is being watched is called the subject. The
objects, which are watching the state changes are called observers or listeners.
@Override
public void update(Observable o, Object arg) {
}
Behavioral patterns (15 of 15)
When the data changes, we want to notify all observers of this object. To do
this, we just need to call the notifyObservers method when we want an update
sent out
//send a notification
dataStore.notifyObservers();
Course summary