Discussions

J2EE patterns: RAX Pattern

  1. RAX Pattern (5 messages)

    This pattern describes a factory pattern which returns an object I call a RAX which contains three lists or collections.

    The lists are described below:
    1) The remove list or collection.
    2) The add list or collection.
    3) The exist list or collection.

    The factory method parameters are:
    RAXFactory.create(Collection toSystem, Collection fromSystem)
    .
    .
    you may have to overload additional methods to accomadate Lists, Hashtables, etc.


    The toSystem, are items or objects the user has selected to be persisted to the system.

    The fromSystem, are items or objects that were previously selected.

    Objects in the collection should have an overidden equals() method to determine object equality. Note: some java objects you must overide. If you create an object you must overide the equals() method.

    example:
    Date date1 = ....today();
    Date date2 = new Date(date1.getTime());
    Date dateA = date1;
    Date dateB = date2;
    System.out.println( "Are they the same ? " + dateA.equals(dateB) );


    Are they the same ? true.

    Pattern steps:
    1) Obtain the selected object collection from the user. (toSystem)
    2) Query system to obtain previously selected objects from the system. (fromSystem)
    3) Take the toSystem and fromSystem collections as params.
    4) Fill REMOVE list with the objects that exist in the fromSystem list and not in the toSystem list.
    5) Fill ADD list with the objects that exist in the toSystem list and not in the fromSystem list.
    6) Fill EXIST list with the objects that exist in both toSystem and fromSystem list.
    7) return RAX object to caller.
     

    Senario or the use of the pattern.

    Editing previous user preferences:
    [display edit mode]
    My favorite fruit
    *Bananas
     Apples
    *Grapes
    *Oranges

    User selects
    [new selection]
    My favorite fruit
     Bananas
    *Apples
    *Grapes
    *Oranges

    Remove list : (remove from system)
    Bananas

    Add list: (add to system)
    Apples

    eXist list: (update system or leave alone)
    Grapes
    Oranges
    -------------------------------------------
    other examples:
    Credentials for ACL security.
    User preferences
    Multiple choice input interfaces.

    Gui examples
    List boxes. (multi select)
    Check boxes.
    -------------------------------------------
    Anyone want code examples please let me know.

    I've used this 10 years ago with C code. And I've decided to put it on The Server Side. If someone else has already thought of this pattern oh well... Just let me know. Everyone at one point has seen this pattern.


    -Carl P. Dea

    Threaded Messages (5)

  2. RAX Pattern (Code Example)[ Go to top ]

    I didn't get feedback maybe a code example would help.
    Below are just 2 classes Rax.java and RaxFactory.java:


    ===============================================================================

    /**
     * Title: RAX This object contains 3 lists.
     * Description: This class describes a 3 Lists 'R'emove, 'A'dd, e'X'ists.
     * @author cdea
     * @version 1.0
     */

    import java.util.*;

    public class Rax {
        private List removeList;
        private List addList;
        private List existList;

        public Rax() {
        }

        public List getRemoveList() {
            if (removeList == null) {
                removeList = new ArrayList();
            }
            return removeList;
        }

        public List getAddList() {
            if (addList == null) {
                addList = new ArrayList();
            }
            return addList;
        }

        public List getExistList() {
            if (existList == null) {
                existList = new ArrayList();
            }
            return existList;
        }

        void setRemoveList( List list) {
            removeList = list;
        }

        void setAddList( List list) {
            addList = list;
        }

            void setExistList( List list) {
            existList = list;
        }
        
        
        public String toString() {
            StringBuffer sb = new StringBuffer();
                
            // output remove list
            sb.append("Remove list: \n" );
            if (getRemoveList().size() == 0) {
                sb.append(" No elements \n");
            }
            for (int i = 0; i < getRemoveList().size(); i++) {
                Object obj = getRemoveList().get(i);
                if (obj instanceof java.util.Date) {
                    java.util.Date date = (java.util.Date) obj;
                    sb.append(" element " + i + " " + date + " " + date.getTime() + "\n");
                } else {
                    sb.append(" element " + i + getRemoveList().get(i) + "\n");
                }
            }
            
            // output add list
            sb.append("Add list: \n" );
            if (getAddList().size() == 0 ){
                sb.append(" No elements \n");
            }
            for (int i = 0; i < getAddList().size(); i++) {
                Object obj = getAddList().get(i);
                if (obj instanceof java.util.Date) {
                    java.util.Date date = (java.util.Date) obj;
                    sb.append(" element " + i + " " + date + " " + date.getTime() + "\n");
                } else {
                    sb.append(" element " + i + getAddList().get(i) + "\n");
                }
            }

            // output exist list
            sb.append("eXist list: \n" );
            if (getExistList().size() == 0) {
                sb.append(" No elements \n");
            }
            for (int i = 0; i < getExistList().size(); i++) {
                Object obj = getExistList().get(i);
                if (obj instanceof java.util.Date) {
                    java.util.Date date = (java.util.Date) obj;
                    sb.append(" element " + i + " " + date + " " + date.getTime() + "\n");
                } else {
                    sb.append(" element " + i + getExistList().get(i) + "\n");
                }
            }
            
            return sb.toString();
        
        }

    }



    ===============================================================================


    /**
     * Title: RaxFactory
     * Description: This static Class returns a RAX object.
     * @author Carl Dea
     * @version 1.0
     */

    import java.util.Arrays;
    import java.util.Collection;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;

    public class RaxFactory {
        //
        private RaxFactory() {
        }

        /**
         * Factory method which gets 2 lists and returns a RAX object.
         * @param fromSystemC a collection from The system.
         * @param toSystemC a collection from the system.
         *
         */
        public static Rax create(Collection fromSystemC, Collection toSystemC) {
            Rax rax = new Rax();
            List fromSystem = (List) fromSystemC;
            List toSystem = (List) toSystemC;
            boolean found = false;

            // fromSystem [nothing] toSystem[something] in list.
            if ( (fromSystem == null || fromSystem.size() == 0) &&
                 (toSystem != null && toSystem.size() > 0) ) {
                rax.setAddList(toSystem);
                return rax;
            }

            // fromSystem [something] toSystem[nothing] in list.
            if ( (fromSystem != null && fromSystem.size() > 0) &&
                 (toSystem == null || toSystem.size() == 0) ) {
                rax.setRemoveList(fromSystem);
                return rax;
            }

            Iterator it1 = fromSystem.iterator();

            List removeList = rax.getRemoveList();
            List addList = rax.getAddList();
            List existList = rax.getExistList();

            // check from system list
            Object oneItem = null;
            Object oneItem2 = null;

            // Deal with the 'From System List'.
            while (it1.hasNext()) {
                found = false;
                oneItem = it1.next();
                
                Iterator it2 = toSystem.iterator();
                while (it2.hasNext()) {
                    oneItem2 = it2.next();
                    
                    // found in the from list and to list already exists.
                    if ( oneItem.equals(oneItem2) ) {
                        existList.add(oneItem2);
                        found = true;
                        break;
                    }
                }

                // when not in list it is to be removed.
                if (!found) {
                    removeList.add(oneItem);
                }
            }


            // Deal with the 'To System List'.
            oneItem = null;
            oneItem2 = null;
            Iterator addIterator = toSystem.iterator();
            Iterator existIterator = null;
            while (addIterator.hasNext() ) {
                found = false;
                oneItem = addIterator.next();
                existIterator = existList.iterator();
                while (existIterator.hasNext()) {
                    oneItem2 = existIterator.next();
                    if (oneItem.equals(oneItem2)) {
                        found = true;
                        break;
                    }
                }
                // when found reset
                if (!found) {
                    addList.add(oneItem);
                }
            }

            // set lists
            rax.setRemoveList(removeList);
            rax.setAddList(addList);
            rax.setExistList(existList);

            return rax;
        }

        /**
         * Factory method which gets 2 lists and returns a RAX object.
         * @param fromSystem a collection from The system.
         * @param toSystem a collection from the system.
         *
         */
        public static Rax create(Object[] fromSystem, Object[] toSystem) {
            return create(Arrays.asList(fromSystem), Arrays.asList(toSystem));
        }


    public static void main (String[] args) {
            System.out.println(" Rax testing Strings");
            String[] fromSystem = {"Carl", "Test", "Test2"};
            String[] toSystem = {"Test", "Test3"};
            Rax rax = create(fromSystem, toSystem);
            System.out.println( " Rax String - From System List : " + fromSystem );
            System.out.println( " Rax String - To System List : " + toSystem );

            System.out.println( " Rax String - Remove List : " + rax.getRemoveList() );
            System.out.println( " Rax String - Add List : " + rax.getAddList() );
            System.out.println( " Rax String - eXist List : " + rax.getExistList() );

        }



    }


    ================================================================================


    This code could be optimized. Objects should always have their equals() method overwritten unless the object already is overwritten. Such as java.util.Date, String, Integer, etc.



    -Carl Dea
  3. good idea[ Go to top ]

    it was good idea just for small amount data
  4. good idea[ Go to top ]

    it was good idea just for small amount data
    Sorry for the very slow reply.

    Actually, this implementation is for small amount of data. But,
    anyone can provide a more robust and scalable solution, this is just a reoccuring pattern in software development. I only provided a simple implementation to demonstrate the pattern. In distributed environments I find this useful when managing many singleton objects across different application servers. The use of JMX, JMS, etc.

    I hope this becomes useful.

    There is always a better way.

    -Carl
  5. I've implemented this functionality by implementing it inside a List.

    First we had Objects with bound properites. Each Object had a state: new, modified, or old.

    The key was then to hold an internal List that held the deletions.

    Another approach that would work with out the bound Objects would be to maintain two internal Lists. One for adds and one for deletes. The problem with this approahch is that you must have some way of differentiating between the adds that create the 'original' list and actaul adds later. One way would be to use a constructor that takes a Collection to create the 'original'. I think this would only work well with immutable Objects.
  6. There are two issues that we will face in this: 1. Implementation of equals method. Every such comparing object should implement the equals method properly, else the definition of exists is lost. And thereforth, every operation goes haywire. 2. Iteration through a list is heavy. What is better is to use a map for retrieval of objects. Most of the business objects/Domain Model objects have a property/collection of properties that uniqely identify it. A suitable combination can be used as a key for this map. Another thing with regards to RAX is that it looks at iteration of collection everytime comparison is needed. Possibly, there can be a caching mechanism in the object itself for the caching/saving of the map, which will lead to lesser number of iterations.