0

Hi I am reading some code in a project which I just start to work on and see some code like this, which I could not understand.

public class A implements Ignite {
    protected Ignite ignite;
    .......
    protected void checkIgnite() {
        ......
    }

    @Override 
    public <K, V> IgniteCache<K, V> createCache(CacheConfiguration<K, V> cacheCfg) {
        checkIgnite(); 
        return ignite.createCache(cacheCfg);
    } 

}

I don't understand the meaning of this class.

First, this class A is to implements the interface Ignite.Then why pass the interface Ignite into this class?

Second, for the method createCache in class A, it return ignite.createCache(cacheCfg), then what exactly is the implementation of this method?

Thank you!

TryMyBest
  • 333
  • 5
  • 11
  • You read code in your projects and don't understand this? I though this is your project? then look for *wrapper classes* resp. *delegate pattern* which might add additional functionalities to existing ones. – UninformedUser Jun 20 '17 at 14:02
  • I did not write this class. I am reading this class and try to understand what that mean. – TryMyBest Jun 20 '17 at 14:06
  • Seems like decorator pattern. Something similar is done in Java's IO API. For example: Take a look at `java.io.BufferedReader`. – anacron Jun 20 '17 at 14:07
  • @anacron Oh thanks, that's really helpful. – TryMyBest Jun 20 '17 at 14:10

3 Answers3

2

Your A class is both a Ignite instance and holds an Ignite instance as a field.
It is a wrapper class conforming to a specific interface.

It provides a way to reuse a Ignite instance (that may be an instance of another class than A) while overriding some behaviors of it.

For example here :

@Override 
public <K, V> IgniteCache<K, V> createCache(CacheConfiguration<K, V> cacheCfg) {
    checkIgnite(); 
    return ignite.createCache(cacheCfg);
} 

The createCache() implementation relies on the createCache() of the wrapped Ignite instance but modifies its behavior by invoking before the checkIgnite()method of the A class.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

Looks like someone tried to build a default method for an interface before Java 8. When A has a constructor with an Ignite argument then you can do this:

IgniteCache<> cache = new A( someIgnite ).createCache( ... );

In Java 8, you would move createCache() directly as a default method to Ignite.

California
  • 36
  • 2
0

If I understand correctly, you ask about IgniteSpringBean class. This is wrapper for integration with Spring. This class encapsulate logic for properly creation and destroy Ignite instance. Also it check that bean was properly configured on calls of methods.