0

From day today I want to leran more about, how an application is designed/structured. So I read something about the facade pattern an got a little question about this pattern. First of all, a short introduction to my project:

My Project My project is a Multimedia Application. On the one side there is the MultimediaApllication, which got different Views (e.g Musicscreen,Videoscreen ...) ont the other side there is my IRController which allows you to control the application. The MultimediaApplication works without an IRController, because it listen to KeyEvents (e.g 'enter' pressed). The IRController is like a little own System: IRController

This enum creates a key object if a key is pressed on the ircontroller

public enum 

          /*
          Example Values
          */
          VOL_UP(new MediaVolumeUpKey()),
          VOL_DOWN(new MediaVolumeDownKey()),
          MUTE(new MediaMuteKey());


        private byte bytes[];// Repräsentiert den Key
        private Key k;

        private Keys(Key key){
        this.k = key;
        }
 public static Key getKey(byte[] bytes){
for(Keys key: values()){
if(Arrays.equals(key.getBytes(), bytes)){
return key.k;
}
}
return null;
}

public byte[] getBytes(){
return this.bytes;
}

public Key getKey(){
return this.k;
}
}

And this is the abstarct Key class. All Keys entends Key.

public abstract class Key {
private byte[] bytes;

protected Key(int...bytes){//int... als argumente für übersichtlichen Code
byte[] temp = new byte[bytes.length];
for(int i =0; i<bytes.length;i++) temp[i] = bytes[i];
this.bytes = temp;
}

public byte[] getBytes(){return bytes;} 

public void abstract call();
}


public static Key getKey(byte[] bytes){
for(Keys key: values()){
if(Arrays.equals(key.getBytes(), bytes)){
return key.k;
}
}
return null;
}

public byte[] getBytes(){
return this.bytes;
}

public Key getKey(){
return this.k;
}

Now when I change my IRController I just need to change the bytecode for the key.

The problem is know the communication between the key classes and the multimedia application. That everything is loosely coupled, I thought maybe its a good idea to build a facade around the multimedia application. So the key class can call a play() stop() pause() volUp() of the facade and the facade decide how to act when the method is called.

Can I implemnt my idea like this?Or should I better ask, have I understood the facade pattern right? Thanks for your help :)

Chris
  • 599
  • 3
  • 11
  • 24

1 Answers1

1

About

have I understood the facade pattern right?

I think - yes. Facade design pattern is more known to provide a unified interface to a set of interfaces in a subsystem. But in this case I think you can used it to define a higher-level implementation that makes the subsystem easier to use.

Hiding the implementation details is also a concept of Encapsulation. So the other users are being provided with the things they need to know/use and the actual processing is left to the one responsible for it.

Since you're also considering

change my IRController

one possible solution is the Proxy design pattern. It allows you to provide a surrogate or placeholder for another object (lets assume class Key ) to control access to it.

UPDATE:

You can consider this code from Apache Commons StringUtils:

public static String[] splitByCharacterType(final String str) {
    return splitByCharacterType(str, false);
}

private static String[] splitByCharacterType(final String str, final boolean camelCase) {
    // code...
}

As you can see the parameters are two in SplitByCharacterType(final String str, final boolean camelCase), but only one is exposed to the outer world via splitByCharacterType(final String str). You can apply it for your case of Key class design.

ekostadinov
  • 6,880
  • 3
  • 29
  • 47
  • Thanks for your answer :) But what do you mean with "...I think you can use it to define a higher-level implementation that makes the subsystem easier to use"? – Chris Oct 16 '14 at 12:06