Wednesday, September 17, 2014

Active record domain model in Java with Spring and Hibernate

1. Goal.

As a developer, I want to create ability to use just entity object for performing base CRUD operation in DB, like:
myObject.save()
myObject.update()
myObject.delete()

2. Problem. 

The main problem here is obvious: transaction management. What it we are executing in one transaction:
object1.save();
object2.save();
and after that we have to rollback our transaction ? In case of anemic domain model business logic located at another(service) level:

@Transactional
pubic void doSomething(Object1 object1, Object2 object2) {
       Session session=sessionFactory.getCurrentSession();
       service1.save();
       service2.save();
       session.rollback();
}

Using anemic mode, at service level we can just mark method as @Transactional,  execute whole list of operations and after that  - commit or rollback transaction.
Using rich domain model, we have to think about transaction management.

3. Application context file.

In application context file we have to define data source bean(with connection to H2 database), session factory(with one domain class) bean and  abstract model bean - main(parent) class for our rich domain model.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd>">

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:~/test" />
        <property name="username" value="sa" />
        <property name="password" value="sa" />
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- Don't forget to list all your entity classes here -->
        <property name="annotatedClasses">
            <list>
                <value>com.demien.richdomain.model.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <bean id="AbstractModel" class="com.demien.richdomain.model.AbstractModel">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

4. Abstract model

This class could be much simpler, but because of logic related with transactions I added few more procedures: setAutoCommit, setSession, checkAndStartTransaction, checkAndCommitTransaction.

package com.demien.richdomain.model;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class AbstractModel<T, PK extends Serializable> {

    protected static SessionFactory sessionFactory;
    protected Session session;
    protected boolean autoCommit=true;

    private Class<T> cl;

    public AbstractModel() {
    }

    public AbstractModel(Class<T> cl) {
        this.cl=cl;
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void setSessionFactory(SessionFactory sessionFactory) {
        AbstractModel.sessionFactory = sessionFactory;
    }

    protected Session getCurrentSession() {

        try {
            session=getSessionFactory().getCurrentSession();
        } catch (Exception e ) {

        }
        if (session == null) {
            session = getSessionFactory().openSession();
        }
        return session;
    }

    public Session getNewSession() {
        return getSessionFactory().openSession();
    }

    protected void setSession(Session session) {
        this.session=session;
    }

    protected void setAutoCommit(boolean autoCommit) {
        this.autoCommit=autoCommit;
    }

    public void checkAndStartTransaction() {
        if (autoCommit) {
            Session session = getCurrentSession();
            session.beginTransaction();
        }
    }

    public void checkAndCommitTransaction() {
        if (autoCommit) {
            session.getTransaction().commit();
        }
    }

    public T get(PK id) {
        Session session = getCurrentSession();
        checkAndStartTransaction();
        T element = (T) session.get(cl, id);
        checkAndCommitTransaction();
        return element;
    }

    public PK save() {
        Session session = getCurrentSession();
        checkAndStartTransaction();
        PK result=(PK)session.save(this);
        checkAndCommitTransaction();
        return result;
    }

    public void update() {
        Session session = getCurrentSession();
        checkAndStartTransaction();
        session.update(this);
        checkAndCommitTransaction();
    }

    public void delete() {
        Session session = getCurrentSession();
        checkAndStartTransaction();
        session.delete(this);
        checkAndCommitTransaction();
    }

    public List<T> query(String hsql, Map<String, Object> params) {

        Session session = getCurrentSession();
        checkAndStartTransaction();

        Query query = session.createQuery(hsql);
        if (params != null) {
            for (String i : params.keySet()) {
                query.setParameter(i, params.get(i));
            }
        }

        List<T> result = null;
        if ((hsql.toUpperCase().indexOf("DELETE") == -1)
                && (hsql.toUpperCase().indexOf("UPDATE") == -1)
                && (hsql.toUpperCase().indexOf("INSERT") == -1)) {
            result = query.list();
        } else {
            query.executeUpdate();
        }
        checkAndCommitTransaction();
        return result;
    }


    public List<T> getAll() {
        return query("from "+cl.getName(), null);
    }

    public void deleteAll() {
        query("delete from "+cl.getName(),null);

    }

}

Now we can just extend from this class: all child classes will have ability to work with database. 

5.Domain class which extends AbstactModel

It's just a POJO class which extends AbstractModel.

package com.demien.richdomain.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name = "USER")
public class User extends AbstractModel<User, Integer> implements Serializable {

    private static final long serialVersionUID = -7022793569839517729L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Integer id;
    
    @Column(name = "LOGIN")
    private String login;
    
    @Column(name = "PASSWORD")
    private String password;
    

    public User() {
        super(User.class);
    }
    
    public User(String login, String password) {
        this();
        this.setLogin(login);
        this.setPassword(password);
    }
    
    public User(Integer id, String login, String password) {
        this();
        this.setId(id);
        this.setLogin(login);
        this.setPassword(password);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    @Override
    public boolean equals(Object object) {
        if (!(object instanceof User)) {
            return false;
        } else {
            User user = (User) object;
            if (user.getId().equals(this.getId())
                    && user.getLogin().equals(this.getLogin())
                    && user.getPassword().equals(this.getPassword())) {
                return true;
            }
            return false;
        }
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", login=" + login + ", password=" + password
                + "]";
    }

}

6. Test with "simple transaction"

Let's test our class with "simple transaction": just one object associated with transaction.
    @Test
    public void simpleTest() {
        User user=new User("login", "password");
        int cnt0=user.getAll().size();

        Integer id=user.save();

        int cnt1=user.getAll().size();
        Assert.assertEquals(cnt0+1, cnt1);

        User user1=user.get(id);
        Assert.assertEquals(user, user1);

        user.setLogin("login_updated");
        user.update();

        user1.get(id);
        Assert.assertEquals("login_updated", user1.getLogin());
    }

So far, so good: it's very simple and easy to work with database by our object. 


7. Tests with "complex transaction"

If we want to use several objects associated with transaction logic is becoming much more complicated. We have to turn off transaction start(setAutoCommit method) in every object and also we have to provide all transaction object with one session object(setSession method).

  
    @Test
    public void transactionRollbackTest() {
        User user1=new User("login1","password1");
        User user2=new User("login2","password2");
        int cnt0=user1.getAll().size();

        user1.setAutoCommit(false);
        user2.setAutoCommit(false);

        Session session=user1.getNewSession();
        user1.setSession(session);
        user2.setSession(session);

        session.beginTransaction();
        user1.save();
        user2.save();
        session.getTransaction().rollback();

        int cnt1=user1.getAll().size();
        Assert.assertEquals(cnt0, cnt1);

    }

    @Test
    public void transactionCommitTest() {
        User user1=new User("login1","password1");
        User user2=new User("login2","password2");
        int cnt0=user1.getAll().size();

        user1.setAutoCommit(false);
        user2.setAutoCommit(false);

        Session session=user1.getNewSession();
        user1.setSession(session);
        user2.setSession(session);

        session.beginTransaction();
        user1.save();
        user2.save();
        session.getTransaction().commit();

        int cnt1=user1.getAll().size();
        Assert.assertEquals(cnt0+2, cnt1);

    }

8. Conclusion

As for me, active record model can be used in projects with simple transactions. But if there are a lot of complex transaction it's better to use anemic model with logic at service(not domain) level.  

Source code can be downloaded from here.

Sunday, September 7, 2014

Behavioral patterns in Java - Part 2

From Wikipedia, the free encyclopedia

In software engineeringbehavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.


1. Observer

Actors:
 1. event source(observable) - class which has method for registering (adding) observers
 2. event handler(observer) - class which has method for reacting on event
Goal: event source is notifying observers objects

Main class:

public class Observer {
    
    StringBuilder log=new StringBuilder();
    
    public String getLog() {
        return log.toString();
    }
    
    public interface EventHandler {
        void onEvent(String event);
    }
    
    public class FirstHandler implements EventHandler {
        @Override
        public void onEvent(String event) {
            log.append("first:"+event+";");         
        }       
    }
    
    public class SecondHandler implements EventHandler {
        @Override
        public void onEvent(String event) {
            log.append("second:"+event+";");            
        }       
    }   
    
    public class EventSource {
        List<EventHandler> eventHandlers=new ArrayList<EventHandler>();
        
        public void addEventHandler(EventHandler handler) {
            eventHandlers.add(handler);
        }
        
        public void createEvent(String event) {
            for (EventHandler eventHandler: eventHandlers) {
                eventHandler.onEvent(event);
            }
        }
    }
    

}

Test class:

public class ObserverTest {

    @Test
    public void test() {
        Observer observer=new Observer();
        Observer.FirstHandler firstHandler=observer. new FirstHandler();
        Observer.SecondHandler secondHandler=observer. new SecondHandler();
        Observer.EventSource eventSource=observer. new EventSource();
        eventSource.addEventHandler(firstHandler);
        eventSource.addEventHandler(secondHandler);
        eventSource.createEvent("test");
        assertEquals("first:test;second:test;", observer.getLog());
        
    }

}

2. State

Actors: 
 1. set of state objects 
 2. object which can be in different states(with different assignments of state object)
 Goal: object behavior depends on it state (state object) 

Main class:

public class State {

    public interface ShapeState {
        String executeAction();
    }

    public class CreateShapeState implements ShapeState {

        @Override
        public String executeAction() {
            return "create";
        }

    }

    public class DrawShapeState implements ShapeState {

        @Override
        public String executeAction() {
            return "draw";
        }

    }

    public class Shape {
        private ShapeState state;

        public ShapeState getState() {
            return state;
        }

        public void setState(ShapeState state) {
            this.state = state;
        }

        public String executeAction() {
            return state.executeAction();
        }
    }

}

Test class:

public class StateTest {

    @Test
    public void test() {
        State state=new State();
        
        State.Shape shape=state. new Shape();
        shape.setState(state. new CreateShapeState() );
        
        assertEquals("create", shape.executeAction());
        
        shape.setState(state. new DrawShapeState() );
        
        assertEquals("draw", shape.executeAction());

    }

}

3. Strategy

Actors: 
 1. Strategy object 
 2. Context object which have Strategy object 
 Goal: context execute action using strategy object so execution is based on strategy


Main class:



public class Strategy {
    
    interface DrawStrategy {
        String draw();
    }
    
    public class FastDrawStrategy implements DrawStrategy {
        @Override
        public String draw() {
            return "fast draw";
        }       
    }

    public class SlowDrawStrategy implements DrawStrategy {
        @Override
        public String draw() {
            return "slow draw";
        }       
    }   
    
    public class Context {
        DrawStrategy strategy;
                
        public void setStrategy(DrawStrategy strategy) {
            this.strategy=strategy;
        }
        
        public String executeStrategy() {
            return strategy.draw();
        }
    }
    
}

Test class:

public class StrategyTest {

    @Test
    public void test() {
        Strategy strategy=new Strategy();
        Strategy.FastDrawStrategy fastDrawStrategy=strategy. new FastDrawStrategy();
        Strategy.SlowDrawStrategy slowDrawStrategy=strategy. new SlowDrawStrategy();
        Strategy.Context context=strategy. new Context();

        context.setStrategy(fastDrawStrategy);
        assertEquals("fast draw", context.executeStrategy());

        context.setStrategy(slowDrawStrategy);
        assertEquals("slow draw", context.executeStrategy());
        
    }

}

4. Template

Actors: 1. template class: one method of it can not be overriden - it is a TEMPLATE method 2. subclasses of template class Goal: strictly defined template of logic execution. detail of execution can be different for subclasses, by overriding non template methods

Main class:


public class Template {

    public abstract class ShapeTemplate {
        public abstract String create();

        public abstract String draw();

        public String createAndDraw() {
            return create() + "." + draw();
        }
    }

    public class FastShape extends ShapeTemplate {

        @Override
        public String create() {
            return "fast create";
        }

        @Override
        public String draw() {
            return "fast draw";
        }
    }

    public class SlowShape extends ShapeTemplate {

        @Override
        public String create() {
            return "slow create";
        }

        @Override
        public String draw() {
            return "slow draw";
        }
    }
}

Test class:

public class TemplateTest {

    @Test
    public void test() {
        Template template=new Template();
        Template.FastShape fastShape=template. new FastShape();
        Template.SlowShape slowShape=template. new SlowShape();
        //
        String result="";
        result=fastShape.createAndDraw();
        assertEquals("fast create.fast draw", result);
        result=slowShape.createAndDraw();
        assertEquals("slow create.slow draw", result);
        
    }

}

5. Visitor

 Actors:
 1. Visitor. several visit(Element) - for every subclass of Element.
 2.Element. Element has accept(Visitor) method.
 Goal: logic separation: element calls visitor's method, which calls element's method

Main class:

public class Visitor {

    public interface ShapeVisitor {
        public String visit(Circle element);
        public String visit(Square element);
    }

    public abstract class Shape {
        public abstract void accept(ShapeVisitor visitor);

        public String draw() {
            return "drawing";
        };
    }

    public class Circle extends Shape {

        @Override
        public void accept(ShapeVisitor visitor) {
            visitor.visit(this);
        }

    }

    public class Square extends Shape {

        @Override
        public void accept(ShapeVisitor visitor) {
            visitor.visit(this);
        }

    }

    public class FastShapeVisitor implements ShapeVisitor {

        @Override
        public String visit(Circle element) {
            return "fast circle "+element.draw();
        }

        @Override
        public String visit(Square element) {
            return "fast square "+element.draw();
        }
    }
}


Test class:


public class VisitorTest {

    @Test
    public void visitorTest() {
        Visitor visitor=new Visitor();
        Visitor.FastShapeVisitor fastShapeVisitor=visitor. new FastShapeVisitor();
        Visitor.Circle circle=visitor.new Circle();
        Visitor.Square square=visitor. new Square();
        String result="";
        result=fastShapeVisitor.visit(circle);
        Assert.assertEquals("fast circle drawing", result);
        result=fastShapeVisitor.visit(square);
        Assert.assertEquals("fast square drawing", result);

    }

}

Behavioral patterns in Java - Part 1

From wikipedia:
In software engineeringbehavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.
Examples of this type of design pattern include:
  • Chain of responsibility pattern: Command objects are handled or passed on to other objects by logic-containing processing objects
  • Command pattern: Command objects encapsulate an action and its parameters
  • "Externalize the Stack": Turn a recursive function into an iterative one that uses a stack[1]
  • Interpreter pattern: Implement a specialized computer language to rapidly solve a specific set of problems
  • Iterator pattern: Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation
  • Mediator pattern: Provides a unified interface to a set of interfaces in a subsystem
  • Memento pattern: Provides the ability to restore an object to its previous state (rollback)

1. Chain of responsibility

Actors: list (chain) of command objects. Command can have link to "next"(in chain) command.  

Goal: each command tries to do some action and switch execution process to next command.

Main class:


public class ChainOfResponsibility {
    
    public abstract class AbstractCommand {
        public abstract String executeInternal();
        
        AbstractCommand next;
        String name;
        
        public void setName(String name) {
            this.name=name;
        }
        
        public void setNext(AbstractCommand next) {
            this.next=next;
        }
                
        
        public String execute(String commands) {
            String result="";
            if (commands.indexOf(name)>-1) {
                result=executeInternal();
            }           
            if (next!=null) {
                result=result+"."+next.execute(commands);
            }
            return result;
        }
        
    }

    public class CreateCommand extends AbstractCommand {

        @Override
        public String executeInternal() {
            return "create";
        }       
        
    }   
    
    public class DrawCommand extends AbstractCommand {

        @Override
        public String executeInternal() {
            return "draw";
        }       
        
    }
    
    public class MoveCommand extends AbstractCommand {

        @Override
        public String executeInternal() {
            return "move";
        }       
        
    }
    
    public class Client {
        public String execute(String commands) {
            
            CreateCommand createCommand=new CreateCommand();
            createCommand.setName("create");
            
            DrawCommand drawCommand=new DrawCommand();
            drawCommand.setName("draw");
            
            MoveCommand moveCommand=new MoveCommand();
            moveCommand.setName("move");
            
            createCommand.setNext(drawCommand);
            drawCommand.setNext(moveCommand);
            
            return createCommand.execute(commands);
        }
    }
    

}

Test class:

public class ChainOfResponsibilityTest {

    @Test
    public void test() {
        ChainOfResponsibility chainOfResponsibility=new ChainOfResponsibility();
        ChainOfResponsibility.Client client=chainOfResponsibility. new Client();
        
        String result=client.execute("move, create");
        assertEquals("create..move", result);
        
    }

}

2. Command

  Actors: 
  1. executor- interface which have to be implemented by "command" object
  2. command - implementation of executor interface 
  3. receiver - "helper" object which used by command object
  Goal: implementation of "handler": in Java we can not pass function as parameter to another function, but be can pass command object and execute such a function on it.

Main class:




public class Command {

    public interface Executor {
        String execute();
    }
    
    public class Receiver {
        public String draw() {
            return "draw";
        }
        public String erase() {
            return "erase";
        }
    }
    
    public abstract class AbstractCommand implements Executor {
        protected Receiver receiver=new Receiver();
    } 
    
    public class DrawCommand extends AbstractCommand {
        
        @Override
        public String execute() {
            return receiver.draw();
        }
        
    }
    public class EraseCommand extends AbstractCommand {

        @Override
        public String execute() {
            return receiver.erase();
        }       
    }
    
    public class Client {
        public String doSomeAction(List<Executor> commands) {
            StringBuilder result=new StringBuilder();
            
            for (Executor eachCommand:commands) {
                result.append(eachCommand.execute()+".");
            }
            
            return result.toString();
        }
    }
    
}

Test class:

public class CommandTest {

    @Test
    public void test() {
        Command command =new Command();
        Command.DrawCommand draw=command. new DrawCommand();
        Command.EraseCommand erase=command. new EraseCommand();
        
        List<Command.Executor> commandList=new ArrayList<Command.Executor>();
        commandList.add(draw);
        commandList.add(draw);
        commandList.add(erase);
        
        Command.Client client=command. new Client();
        assertEquals("draw.draw.erase.", client.doSomeAction(commandList));
        
        
    }

}

3. Interpretator

Actors:
1. custom command language
2. expression written on this language
3. interpretator class which can interpret expression oin proper way and calculate it
Goal: calculating expression

Main class:
           

public class Interpretator {
    
    interface Operator {
        int calculate(int argument1, int argument2);
    }
    
    class PlusOperator implements Operator {
        
        @Override
        public int calculate(int argument1, int argument2) {            
            return argument1+argument2;
        }
        
    }
    
    class MinusOperator implements Operator {
        
        @Override
        public int calculate(int argument1, int argument2) {            
            return argument1-argument2;
        }
        
    }
    
    class Context {
        
        Map<String, Operator> operatorMap;
        
        public Context(Map<String, Operator> operatorMap) {
            if (operatorMap==null) {
                throw new RuntimeException("OperatorMap can not be null.");
            }
            this.operatorMap=operatorMap;   
        }
        
        
        public int getNearestOperatorPosition(String expression, int position) {
            int result=-1;
            for (String operator:operatorMap.keySet()) {
                int currentOperatorPosition=expression.indexOf(operator, position);
                if (result==-1 || (currentOperatorPosition!=-1 && currentOperatorPosition<result) ) { 
                    result=currentOperatorPosition;         
                }
            }
            return result;
        }
        
        public int getNearestOperandEnd(String expression, int position) {
            int operandEnd=-1;
            int nextOperatorPosition=getNearestOperatorPosition(expression, position);
            if (nextOperatorPosition==-1){
                operandEnd=expression.length();
            } else {
                operandEnd=nextOperatorPosition;
            }
            return operandEnd;
        }
        
        public int calculate(String expression) {
            if (expression==null || expression.length()==0) {
                return 0;
            }
                    
            int position=getNearestOperandEnd(expression, 0);
            String operandStringValue=expression.substring(0, position);
            int result=Integer.parseInt(operandStringValue);
            //position++;
            while (position<expression.length()) {
                String operator=expression.substring(position, position+1);
                Operator e=operatorMap.get(operator);               
                position++;
                int operandEnd=getNearestOperandEnd(expression, position);
                operandStringValue=expression.substring(position, operandEnd);
                int operand=Integer.parseInt(operandStringValue);
                result=e.calculate(result, operand);
                position=operandEnd;
            }
            
            return result;
        }
    }

}

Test class:

public class InterpretatorTest {
    
    Interpretator interpretator=new Interpretator();
    Interpretator.PlusOperator plusOperator=interpretator. new PlusOperator();
    Interpretator.MinusOperator minusOperator=interpretator. new MinusOperator();
    Interpretator.Context context;
    
    @Before
    public void init() {
        Map<String, Operator> operatorMap=new HashMap<String, Operator>();
        operatorMap.put("+", plusOperator);
        operatorMap.put("-", minusOperator);
        context=interpretator. new Context(operatorMap);
    }

    @Test
    public void getNearestOperatorPositionTest() {
        String expression="0123+34";
        int result=context.getNearestOperatorPosition(expression, 0);
        assertEquals(4, result);
        expression="012345-34";
        result=context.getNearestOperatorPosition(expression, 0);
        assertEquals(6, result);
    }
    
    @Test
    public void getNearestOperandEndTest() {
        String expression="0123+56";
        int result=context.getNearestOperandEnd(expression, 0);
        assertEquals(4, result);
        expression="0123+56";
        result=context.getNearestOperandEnd(expression, 5);
        assertEquals(7, result);
    }   
    
    @Test
    public void mainTestWithTraditionalOperators() {
            
        String expression="0123+56+8";
        int result=context.calculate(expression);
        assertEquals(123+56+8, result);
        
        expression="12+34-56";
        result=context.calculate(expression);
        assertEquals(12+34-56, result);
    }
    
    @Test
    public void mainTestWithExperimentalOperators() {
        Map<String, Operator> operatorMap=new HashMap<String, Operator>();
        operatorMap.put("P", plusOperator);
        operatorMap.put("M", minusOperator);
        context=interpretator. new Context(operatorMap);
        
        
        String expression="2P3M1";
        int result=context.calculate(expression);
        assertEquals(4, result);
        
    }   

}

4. Iterator

Actors:
   1. iterator interface which describes it operation list
   2. iterator object which implements interface
   3. iterable object - object which has method for returning it iterator
Goal: iterate through  array of elements using iterator object

Main class:

public class Iterator {

    interface SimpleIterator<T> {
        boolean hasNext();
        T getNext();
    } 
        
    class ArrayIterator<T> implements SimpleIterator<T> {
        T[] array;
        int position=-1;
        
        public ArrayIterator(T[] array) {
            this.array=array;
        }

        @Override
        public boolean hasNext() {
            if (position<array.length-1) {
                return true;
            }
            return false;
        }

        @Override
        public T getNext() {
            position++;
            return array[position];         
        }
        
    }
    
    interface Iterable<T>{
        SimpleIterator<T> getIterator();
    }
    
    class ArrayIterable<T> implements Iterable<T> {
        private T[] array;
        
        public ArrayIterable(T[] array) {
            this.array=array;
        }

        @Override
        public SimpleIterator<T> getIterator() {
            return new ArrayIterator<T>(array);
        }       
    }
        
    
}

Test class:

public class IteratorTest {

    @Test
    public void test() {
        String[] array=new String[]{"a", "b", "c"};
        Iterator iterator=new Iterator();
        Iterator.ArrayIterable<String> arrayIterable=iterator. new ArrayIterable<String>(array);
        Iterator.SimpleIterator<String> arrayIterator=arrayIterable.getIterator();
        StringBuilder result=new StringBuilder();
        while (arrayIterator.hasNext()) {
            String element=arrayIterator.getNext();
            result.append(element);
        }       
        assertEquals("abc", result.toString());             
        
    }

}

5. Mediator

Actors:
1. components
2. client
3. mediator
Goal: add some new functionality by combining existing. 
   client communicate with mediator but not with components.  
   mediator - HUB of components communication.

Main class:

public class Mediator {
    
    public class Drawer {
        public String drawShape(String shape) {
            return "drawing:"+shape;
        }
    }
    
    public class Mover {
        public String moveShape(String shape) {
            return "moving:"+shape;
        }
    }
    
    public class ShapeMediator {
        Drawer drawer=new Drawer();
        Mover mover=new Mover();
        
        public String drawAndMove(String shape) {
            return drawer.drawShape(shape)+"."+mover.moveShape(shape);
        }
    }

}

Test class:

public class MediatorTest {

    @Test
    public void test() {
        Mediator mediator=new Mediator();
        Mediator.ShapeMediator shapeMediator=mediator. new ShapeMediator();
        String result=shapeMediator.drawAndMove("square");
        assertEquals("drawing:square.moving:square", result);
    }

}

6. Memento

Actors: 
 1. memento class
 2. client
 Goal: memento object saves it state on every change. 
   so, we can get it state, for every step we want.


Main class:


public class Memento {

    public class Shape {
        private int x;
        private int y;

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public void moveTo(int x, int y) {
            this.x=x;
            this.y=y;
        }

    }

    public class ShapeMemento {
        private List<Shape> history=new ArrayList<Shape>();

        public void save(Shape shape) {
            Shape historyShape=new Shape();
            historyShape.moveTo(shape.getX(), shape.getY());
            history.add(historyShape);
        }

        public Shape getShapeForStep(int index) {
            Shape result=history.get(index);
            return result;
        }
    }

}


Test class:

public class MementoTest {

    @Test
    public void mementoTest(){
        Memento memento=new Memento();
        Memento.Shape shape=memento. new Shape();
        Memento.ShapeMemento shapeMemento=memento. new ShapeMemento();
        shape.moveTo(0,0);
        shapeMemento.save(shape);
        shape.moveTo(1,1);
        shapeMemento.save(shape);

        shape=shapeMemento.getShapeForStep(0);
        Assert.assertEquals(0, shape.getX());
        Assert.assertEquals(0, shape.getY());

        shape=shapeMemento.getShapeForStep(1);
        Assert.assertEquals(1, shape.getX());
        Assert.assertEquals(1, shape.getY());

    }
}