Development Best Practice: Java Collections

In order to encapsulate business logic and also from the other side letting other objects outside of our class to trace the collection of items related to your business logic, best practice is to use the Collection as an internal variable inside the class and provide a outside class which basically wraps the Collection, and then that wrapper class should implement Iterable interface. which means in simple words, just implementing the iterator () method. please see below example:

public class MyWrapperClass  implements Iterable{

 private final List<MyBeanObject> listOfBeans= new ArrayList&lt;&gt;();
 public void add(MyBeanObject p){
//TODO: implement add using Collection ...
}
 public void replace(MyBeanObject p) {
        // TODO : implement using collection reference...
}

 public void remove(MyBeanObject p) {
       // TODO: implement remove using collection specific implementation...
 }

@Override
public Iterator iterator() {
    return listOfBeans.iterator();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s