Shaaf’s Blog
msgbartop
Another bit in the wall
msgbarbottom

29 Oct 08 Implementing the adapter

Typically when implementing an interface you would have to implement all the methods that exist in that interface. A very good example is the MouseListener in the java Swing. When you need to implement more then one method where as typically you might be catching only one of them. Saying that you would also find a Mouse Adapter provided as well. Some of us use that often. And that is part of the Adapter pattern. It makes life easier for me sometimes.

Adapter a structural pattern will let you adapt to a different environment. The joining between different environment is called Adapter. Thus basically giving others the interface that they expect or vice versa when your program becomes the client.

For example the following class expects that the implementing class should be implementing all three methods.

1
2
3
4
5
6
7
8
9
public interface RecordListener {
 
public void eventPrePerformed(RecordEvent recordEvent);
 
public void eventPerformed(RecordEvent recordEvent);}
 
public void eventPostPerformed(RecordEvent recordEvent);
 
}

So lets say our implementing class is a rude one and only wants to implement one method. What do you do as an API designer. hmmm

Thats where we step in with the Adapter.

1
2
3
4
5
6
7
8
9
10
11
12
13
public abstract class RecordAdapter implements RecordListener {
 
public void eventPrePerformed(RecordEvent recordEvent) {}
public void eventPerformed(RecordEvent recordEvent) {}
public void eventPostPerformed(RecordEvent recordEvent) {}
 
}
 
public MyAdapterImpl extends RecordAdapter{
 
public void eventPerformed(RecordEvent recordEvent){}
 
}

Now the only thing left to do is use the adapter. And override any method that you might need .

1
2
3
4
5
6
7
8
9
public MyClientClass {
 
public MyClientClass(){
 
this.addRecordListener(new MyAdapterImpl());
 
}
 
}

Tags: , , , , , , , , , , ,

24 Oct 08 wasprofile -create -delete

Sometimes you require to do things silently, without any questions asked and “Just Do It” attitude is required.

I often find my self with this problem.

If you want to delete or create a Websphere profile from your command line try the following. (I have tried on RSA only)

# deleteing a profile
wasprofile -delete -profileName MyProfile

You should get the following message on deletion

INSTCONFSUCCESS: Success: The profile no longer exists.

Creating a websphere profile

wasprofile -create -profileName MyProfile -profilePath \
[PROFILE PATH] -templatePath \
[RSA HOME]runtimes\base_v6\profileTemplates\default \
-nodeName [NODE NAME] -cellName [CELL NAME] -hostName [HOSTNAME].

Tags: , , , , , , , , , , ,

10 Oct 08 Abstract Factory pattern

Factories have been a key pattern in building applications, its fascinatingly simple, effective and to the point. When starting to learn a design oriented approach to applications or API, I would always recommend a factory pattern as one of the key starting notes of highlight in your design.

So today I am talking about the Abstract Factory pattern. Its not an “abstract” class or object that you call a pattern. But its a Factory of facotries and that is what exactly makes it so much wordingly abstract. Having “abstract” classes is there but just some other side of the coin.

When should I use an Abstract Factory:

  • Independence of how products are created, composed or represented
  • Should be configurable with one of the multiple families or products
  • You need enforcable constraints for the products used as a group
  • You need to reveal only the interfaces of products and not thier implementation as part of a bigger picture.

So lets begin with the fun.

This is how I plan to implement it:
Has A:
Product has a Specification
Factory has a Product
FactoryManager has FactoryConstants
FactoryManager has ComputerFactory

Is A:
BFactory is a ComputerFactory
AFactory is a ComputerFactory

Not shown.
ProductA is a Product
ProductB is a Product

Diagram:

AbstractFactory

AbstractFactory

Creating a simple factory that returns products.

1
2
3
4
5
6
7
8
9
public abstract class ComputerFactory {
 
 public abstract String getName();
 
 public abstract Product[] getProducts();
 
 public abstract Product getProduct(int ProductID);
 
}

Implementation of the ComputerFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class AFactory extends ComputerFactory {
 
public String getName(){
return "A";
}
 
public Product[] getProducts(){
return null;
}
 
public Product getProduct(int productID){
switch(productID){
case 1:
return new ProductA();
 
case 2:
return new ProductB();
 
default:
throw new IllegalArgumentException("Sorry you hit the wrong factory, we closed down in 1600 BC");
}
}
}

A register base for factories. Refer to the main method for use later in this post.

1
2
3
4
5
6
public interface FactoryConstants {
 
 public int A = 1;
 public int B = 2;
 
}

The main Entrant class. the Factory Manager that will give the ComputerFactory resultant. Its assumed to be a Singleton as it registers as a Creator in the system (assumption).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class FactoryManager{
 
 private static FactoryManager factoryManager = null;
 
 private FactoryManager(){
 
 }
 
 public static FactoryManager getInstance(){
  if(factoryManager != null){
   return factoryManager;
  }
  else return factoryManager = new FactoryManager();
 }
 
 public ComputerFactory getFactory(int factory) throws IllegalArgumentException{
 
  switch(factory){
   case FactoryConstants.A:
   return new IBMFactory();
 
   case FactoryConstants.B:
   return new SUNFactory();
 
   default:
   throw new IllegalArgumentException("Sorry you hit the wrong factory, we closed down in 1600 BC");
  } 
 }
}

A main method to test the AbstractFactory

1
2
3
4
5
6
7
 public static void main(String args[]){
 
  System.out.println(FactoryManager.getInstance().getFactory(FactoryConstants.A).getName());
  System.out.println(FactoryManager.getInstance().getFactory(FactoryConstants.B).getName());
  System.out.println(FactoryManager.getInstance().getFactory(3).getName());
 
 }

You can find the complete code listing here:
AbstractFactory source

Tags: , , , , , , , , , , ,