Showing posts with label pattern. Show all posts
Showing posts with label pattern. Show all posts

Structural Design Patterns


In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities.
Examples of Structural Patterns include:
  • Adapter pattern: 'adapts' one interface for a class into one that a client expects
    • Retrofit Interface Pattern[1][2]: An adapter used as a new interface for multiple classes at the same time.
    • Adapter pipeline: Use multiple adapters for debugging purposes.[3]
  • Aggregate pattern: a version of the Composite pattern with methods for aggregation of children
  • Bridge pattern: decouple an abstraction from its implementation so that the two can vary independently
    • Tombstone: An intermediate "lookup" object contains the real location of an object.[4]
  • Composite pattern: a tree structure of objects where every object has the same interface
  • Decorator pattern: add additional functionality to a class at runtime where subclassing would result in an exponential rise of new classes
  • Extensibility pattern: aka. Framework - hide complex code behind a simple interface
  • Facade pattern: create a simplified interface of an existing interface to ease usage for common tasks
  • Flyweight pattern: a high quantity of objects share a common properties object to save space
  • Proxy pattern: a class functioning as an interface to another thing
  • Pipes and filters: a chain of processes where the output of each process is the input of the next
  • Private class data pattern: restrict accessor/mutator access

Service and Extension Pattern

In component-base system, we always try to decouple components from each other. Sometime, decoupling process becomes a little bit expensive. In this article, I will talk about a simple approach that can be used for decoupling components, service and extension point pattern.

Why OOP inheritance should not be used?

Service and extension point pattern is similar to general OOP inheritance, but unlike OOP inheritance, it doesn't tight the components with each other. You usually use this pattern when you have a service-provider scenario. For example, suppose component “A” and “B” are in a small HR application called HRTest, component "A" is a module that displays employee information, and component "B" manages data source providers for the application. Data providers can come from different resources like database, web services, or even normal files. As you can conclude, "A" requires at least one provider in order to display employee information to the end user, and B provides the implementation for A's requirements. Using normal inheritance doesn't give a flexible solution because providers may only be exist at runtime, also more than one components can provide the "A" service interface. For instance, "B" and "C". In addition, for some reasons, "C" may be removed at a certain execution point.


Class A{
.
.
.
public void getData(number empID){
ServiceProvider[] service = getService();
}

public ServiceProvider[] getServices(){
getProviderB(); // Get B data provider
getProviderC(); //Get C data provider
.
.
.
}
}


As the preceding example shown, "A" is tightly dependent on "B" and "C." Because, when a new provider such as "D" is added or an old one is removed, "A" should be adjusted.

In such above scenario, service and extension pattern can be used to solve this issue in the flexible manner.

Service and Extension Pattern:

Service and Extension Pattern is commonly used approach in RCP platforms like NetBeans and Eclipse for sharing objects and providing implementation for extension points. Extension point (sometimes is called Service Interface) is a simple interface that a component can define in order to be implemented by other component, which is called Implementation provider or simply Provider. An extension point can have more than one provider at runtime and both providers and service modules should not know each other. To clear the misconception between them, the figure below shows a general structure of the pattern


As the figure demonstrated, Component “A” defines an extension point which “B”, “C” and “D” implement at runtime. Note, because extension point is used by all modules, it should be shared. Therefore, it is highly recommended to encapsulate it in a separate component creating a new abstract layer between them.

What is next?

In the next post, we will implement this pattern using NetBeans Lookup API.

Thanks for your reading.

IoC Pattern

Yesterday, I took a glance at IoC pattern. I found implicitly interesting article about this pattern and the most interesting article is from java.net as well as martinfowler.com . I hope you guys like it.

Event-Listener Pattern

In lossy-couple environment, notification system is essential, because it allows two or more lossy-couple components to communicate and notify each other when an event is triggered. There are several patterns to implement notification system in Java and one of them is event-listener pattern.

In Java, building event-listener components is not easy as Flex or C#. It requires a lot of boilerplate code to implement it. In this article, I will talk about building simple event listener in Java. First I will demonstrate the general algorithm for event listeners then I will implement it in a simple, humble example.

Event-Listener Pattern:

Event-Listener pattern is a simple pattern that can be implemented easily in Java bean class. Actually, it is part of JavaBean conventions. It has been used mainly in J2SE platform, particularly in GUI libraries. This pattern consists of two main parts (Source and Listener):

  1. Source: the source is the Java class for maintaining the event listeners which has previously been registered - It is like a small database . Therefore, the source class has method for registering (add) and unregistering (remove) listeners to the small database. The source also has ability to fire an event.
  2. Listener: As the name refers to, the listener is the Java component that interests in a specific event. When an event is triggered, all listeners interested in the event will get a notification and will execute their own event handler codes. Listeners register for events using the methods defined by the sources of those events.

What is share between these two parts is listener event message! The message is usually interface used to determine event data-type.

Sample:

Now, we are going to write a simple implementation for the pattern, so let's start with the source part.

First of all, we need a simple bean class. I will name it “SourceClass”:

class SourceClass {

}


Then we need a data structure to store the listeners (Remember, the small database). For simplicity, I will use Vector
private Vector listeners = new Vector();
Now, we need two methods, one of them for registering and the other unregistering, they should have JavaBean conventional style. The method used to register a listener in the vector must use the prefix “add”, and the other one, must start with the prefix “remove”. Both of them should follow by the event type and must end with the postfix “Listener”. In addition, they should have an argument with event data-type
public void addTestEventListener(TestEvent l) {
listeners.addElement(l);
}

public void removeTestEventListener(TestEvent l) {
listeners.removeElement(l);
}
As the preceding example depicts, the two methods manage the listeners which interest in "TestEvent" Event.

For simplicity, a simple fire method will be added to notify all registered listeners

public void fire() {
Vector temp = (Vector) listeners.clone();

for (int i=0; i > l.size(); i++) {
TestEventListener listeners = (TestEventListener) temp.elementAt(i);
listener.eventHandler ();
}
}

Now, let's define our event, which simply extends EventListener interface in the Java util package
public interface TestEvent extends java.util.EventListener {

void eventHandler();
}
So as you see, the event is nothing than a simple, share interface between the source and listeners.

Finally, we need a simple listener which listens to our TestEvent. Remember, listener can be any class you want. The only thing is required to become a listener is creating a SourceClass and adding the event (TestEvent).
.
.
.
SourceClass c = new SourceClass();
TestEvent e = new TestEvent(){
void eventHandler(){
//Do something
}
}
c.addTestEventListener(e);

c.fire();
As the codes shown above, SourceClass and TestEvent objects have been created. of course, we have to resolve all abstract methods in TestEventListener interface and the last not the least, we add the later object to the source.

when the source fires the
TestEvent, listeners gets a notification and will execute their event handler code.

Conclusion:

Event-Listener Pattern provides simple notification system for Java components. The pattern has two main parts (Source and Listener). The source stores all listeners, and the listeners listen to particular event, which is simply the message or notification that transports from source to listeners.

Further Reading: