items tagged with designpattern
Abstract factory
Written By: Administrator
Section: Java
Category: Design Patterns Creational patterns
2005-01-20 21:31:29
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Written By: Administrator
Section: Java
Category: Design Patterns Creational patterns
2005-01-20 21:31:29
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Source Code
/**
* Abstract factory declares an interface for operations that create abstract
* product objects.
*
* @role __Factory
*/
public interface AbstractFactory {
/**
* Creates abstract product
*/
ProductA createProductA(); /**
* Creates abstract product
*/
ProductB createProductB();}
/**
* Abstract factory declares an interface for operations that create abstract
* product objects.
*
* @role __Factory
*/
public interface AbstractFactory {
/**
* Creates abstract product
*/
ProductA createProductA(); /**
* Creates abstract product
*/
ProductB createProductB();}
/**
* Abstract product - an interface for a type of Product object.
*
* @role __Product
* @see AbstractFactory
*/
public interface ProductA {
/*
* add product method declarations here
*/
}
/**
* Concrete Factory implements operations of AbstractFactory to create Concrete
* product objects.
*/
public class MyFactory implements AbstractFactory {
/**
* Creates concrete product ConcreteProduct1
*/
public ProductA createProductA() {
return new ConcreteProduct1(); }
/**
* Creates concrete product ConcreteProduct2
*/
public ProductB createProductB() {
return new ConcreteProduct2(); }
}
/**
* Concrete product defines a product object to be created by the corresponding
* concrete factory.
*
* @see MyFactory
*/
public class ConcreteProduct1 implements ProductA {
}
/**
* Concrete product defines a product object to be created by the corresponding
* concrete factory.
*
* @see MyFactory
*/
public class ConcreteProduct2 implements ProductB {
}
Adapter
Written By: Administrator
Section: Java
Category: Design Patterns Structural patterns
2005-01-20 21:33:01
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Written By: Administrator
Section: Java
Category: Design Patterns Structural patterns
2005-01-20 21:33:01
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Source Code
/**
* Defines an existing interface that needs adapting
*
* @role __Adaptee
*/
public class Adaptee {
/* Some adaptee-specific behavior */
public void specificRequest() {
// some adaptee specific stuff is going here
}
}
/**
* This class adapts the interface of Adaptee to the Target interface
*/
public class Adapter extends Target {
/** reference to the object being adapted */
private Adaptee adaptee;
/**
* @param adaptMe
* class to adapt whis this adapter
*/
public Adapter(Adaptee adaptMe) {
this.adaptee = adaptMe;
}
/**
* Implementation of target method that uses adaptee to perform task
*/
public void request() {
adaptee.specificRequest();
}
}
/**
* This class defines domain-specific interface used by client
*
* @role __Target
*/
public abstract class Target {
/** This method is called by client when he needs some domain-specific stuff */
public abstract void request();
}
Bridge
Written By: Administrator
Section: Java
Category: Design Patterns Structural patterns
2005-01-20 21:15:05
Decouple an abstraction from its implementation so that the two can vary independently.
from the gang of the four

Written By: Administrator
Section: Java
Category: Design Patterns Structural patterns
2005-01-20 21:15:05
Decouple an abstraction from its implementation so that the two can vary independently.
from the gang of the four

Source Code
/**
* Defines Abstraction interface. Stores reference to implementation.
*
* @role __Abstraction
*/
public abstract class Abstraction {
/** Reference to actual implementation */
private Implementor impl;
/**
* @return implementation-in-action.
*/
protected Implementor getImplementor() {
return impl;
}
/**
* This sample operation delegates call to particular implementation
*/
public void someOperation() {
getImplementor().someOperationImpl();
}
}
/**
* Concrete implementation
*/
public class ConcreteImplementorA extends Implementor {
/** @see patterns.gof.bridge.Implementor#someOperationImpl() */
public void someOperationImpl() {
// provide implementation here
}
}
/**
* Concrete implementation
*/
public class ConcreteImplementorB extends Implementor {
/** @see patterns.gof.bridge.Implementor#someOperationImpl() */
public void someOperationImpl() {
// provide implementation here
}
}
/**
* Defines interface for implementation classes. Is not oblidged to provide
* one-to-one correspondence to interface of Abstraction.
*
* @role __Implementor
*/
public abstract class Implementor {
/** Implement this method to provide implementation-specific behavior */
public abstract void someOperationImpl();
}
Builder
Written By: Administrator
Section: Java
Category: Design Patterns Creational patterns
2005-01-20 21:16:55
Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Written By: Administrator
Section: Java
Category: Design Patterns Creational patterns
2005-01-20 21:16:55
Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Source Code
/** * The interfaceProductdefines interface to create parts of the * Product. */ public interface Builder { /** * Construct part of the complex Product. */ public void buildPart(); /** * Construct the Product. * * @return the constructed product */ public Product getProduct(); } /** * TheConcreteBuilderis the product of a concrete builder. * */ public class ConcreteBuilder implements Product { } /** * TheConcreteBuilderBuildercreates and assembles parts of * the Product. * */ public class ConcreteBuilderBuilder implements Builder { /** * Reference to Product being constructed */ private ConcreteBuilder concreteBuilder; /** * Construct part of the complex Product. */ public void buildPart() { // put your code here } /** * Construct the Product. * * @return the constructed product */ public Product getProduct() { return concreteBuilder; } } /** * TheConcreteBuilderClientinitialized the Director with a * Concrete Bulder to create the Product and gets result from the Builder. * */ public class ConcreteBuilderClient { /** * Use the Builder to create the Product. */ public void createProduct() { ConcreteBuilderBuilder builder = new ConcreteBuilderBuilder(); new Director(builder).construct(); Product product = builder.getProduct(); } } /** * The classDirectormanages Product creation using Builder. */ public class Director { /** * Reference to Builder currently used */ private Builder builder; /** * Create a newDirectorinstance. * * @param builder the builder which will create the product */ public Director(Builder builder) { this.builder = builder; } /** * Construct the Product using the Builder. */ public void construct() { builder.buildPart(); } } /** * The interfaceProductdefines a complex object that is * constructed part-by-part with Builder. */ public interface Product { }
Chain of responsability
Written By: Administrator
Section: Java
Category: Design Patterns Behavioral patterns
2005-01-20 21:29:50
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Written By: Administrator
Section: Java
Category: Design Patterns Behavioral patterns
2005-01-20 21:29:50
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Source code
/**
* Handles request it is responsible to.
*/
public class ConcreteHandler extends Handler {
/**
* Handle request or delegate it.
*/
public void handleRequest() {
boolean canProcessThisRequest = false; if (canProcessThisRequest) {
// handle request if possible
} else {
// pass it to the next in chain
super.handleRequest(); }
}
}
/**
* Defines interface for request handling
* @role __Handler
*/
public class Handler {
private Handler successor; /** Default request handling */
public void handleRequest() {
if (successor != null) {
successor.handleRequest(); }
}
public Handler getSuccessor() {
return this.successor; }
public void setSuccessor(Handler successor) {
this.successor = successor; }
}
There are 23 items tagged with designpattern. You can view all our tags in the Tag Cloud

















