Dawn

January 3, 2017 Leave a comment

I do love this moment, so peaceful. Alhamdulillah. ūüôā

Dawn

Categories: Ren's Notes Tags: ,

Lebaran

July 6, 2016 Leave a comment

Alhamdulillah. ^^

Lebaran

Lebaran

Categories: Ren's Notes Tags:

Installing Maven On Windows

April 14, 2016 Leave a comment

Below are steps to install Apache Maven on Windows.

  • Download Apache Maven zip file in here
  • Unzip it in desired folder, assume you unzip Maven zip file in here E:\Installer\maven\apache-maven-3.3.9
  • Add MAVEN_HOME variable in user variable that point Maven folder

MAVEN_HOME

  • Update PATH, append it with MAVEN_HOME – %MAVEN_HOME%\bin

Update PATH

  • Verify it it with mvn -version in Windows command

Maven Verification

 

Thank you.

 

Iterator Pattern

March 22, 2016 Leave a comment

Iterator pattern is used to provide a way to access the elements of an aggregate object sequentially without exposing its internal structure.

Iterator Pattern

Iterator Pattern

Here is example.

SubjectType.java

public enum SubjectType {
     MANDATORY, OPTIONAL, ALL
}

Subject.java

 public class Subject {

    private String name;
    private String lecturer;
    private SubjectType type;
    
    public Subject(){
        
    }
    public Subject(String name, String lecturer, SubjectType type) {
        super();
        this.name = name;
        this.lecturer = lecturer;
        this.type = type;
    }
    public String getName() {
        return name;
    }
    public String getLecturer() {
        return lecturer;
    }
    public SubjectType getType() {
        return type;
    }
    @Override
    public String toString() {
        return "Subject [name=" + name + ", lecturer=" + lecturer + ", type=" + type + "]";
    }

}
 

SubjectCollection.java

public interface SubjectCollection {
    public void addSubject(Subject subject);
    public void removeSubject(Subject subjet);
    public SubjectIterator iterator(SubjectType type);
}

SubjectIterator.java

 public interface SubjectIterator {
   public Boolean hasNext();
   public Subject next();
   public void remove();
} 

SubjectCollectionImpl.java

public class SubjectCollectionImpl implements SubjectCollection {

    private List<Subject> subjects;

    public SubjectCollectionImpl() {
        subjects = new ArrayList<Subject>();
    }

    public void addSubject(Subject subject) {
        subjects.add(subject);
    }

    public void removeSubject(Subject subject) {
        subjects.remove(subject);
    }

    public SubjectIterator iterator(SubjectType type) {
        return new Iterator(type, subjects);
    }

    private class Iterator implements SubjectIterator {

        private SubjectType type;
        private int index;
        private List<Subject> subjects;

        private Iterator(SubjectType type, List<Subject> subjects) {
            this.type = type;
            this.subjects = subjects;
        }

        public Boolean hasNext() {
            while (index < subjects.size()) {
                Subject s = subjects.get(index);
                if (s.getType().equals(type)) {
                    return true;
                } else {
                    index++;
                }
            }
            return false;
        }

        public Subject next() {
            Subject s = subjects.get(index);
            index++;
            return s;
        }

        public void remove() {
            subjects.remove(index);
            index++;
        }

    }
}

App.java

public class App 
{
    public static void main( String[] args )
    {
    	SubjectCollection subjectCollection = populateSubject();
    	SubjectIterator iterator = subjectCollection.iterator(SubjectType.ALL);
    	while(iterator.hasNext()){
    		Subject s = iterator.next();
    		System.out.println(s.toString());
    	}
    	System.out.println("-----------------------");
    	iterator = subjectCollection.iterator(SubjectType.MANDATORY);
    	while(iterator.hasNext()){
    		Subject s = iterator.next();
    		System.out.println(s.toString());
    	}
    	System.out.println("-----------------------");
    	iterator = subjectCollection.iterator(SubjectType.OPTIONAL);
    	while(iterator.hasNext()){
    		Subject s = iterator.next();
    		iterator.remove();
    		System.out.println(s.toString());
    	}
    }
    
    public static SubjectCollection populateSubject(){
    	SubjectCollection subjectCollection = new SubjectCollectionImpl();
    	
    	subjectCollection.addSubject((new Subject("Accounting", "John", SubjectType.OPTIONAL)));
    	subjectCollection.addSubject((new Subject("Art", "Smith", SubjectType.OPTIONAL)));
    	subjectCollection.addSubject((new Subject("Chemistry", "Lennon", SubjectType.ALL)));
    	subjectCollection.addSubject((new Subject("Civil", "Alan", SubjectType.ALL)));
    	subjectCollection.addSubject((new Subject("Economics", "Mike", SubjectType.MANDATORY)));
    	subjectCollection.addSubject((new Subject("Education", "Joe", SubjectType.MANDATORY)));
    	
    	return subjectCollection;
    }
}

Result.

Iterator Result

Iterator Result

Categories: Java Tags: , ,

Observer Pattern

February 20, 2016 Leave a comment

Observer pattern is one to many dependency between object so when object changes state  so all of it dependents are notified. In observer pattern the object that watch on the state of another object are called Observer and the object that is being watched is called Subject / Observable.

Observer Pattern

Observer Pattern

Here is example.

Observer.java

public interface Observer {
 public void update();
}

Dress.java

public class Dress implements Observable {

 private List<Observer> observers;
 private int stock;

 public Dress() {
 observers = new ArrayList<Observer>();
 }

 public void addObserver(Observer observer) {
 observers.add(observer);
 }

 public void notifyObserver() {
 if (getStock() < 1) {
 for (Observer observer : observers) {
 observer.update();
 }
 }
 }

 public int getStock() {
 return stock;
 }

 public void setStock(int stock) {
 this.stock = stock;
 notifyObserver();
 }
}

Observable.java

public interface Observable {
 public void addObserver(Observer observer);
 public void notifyObserver();
}

Buyer.java

public class Buyer implements Observer {

 private String name;
 private String job;

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public String getJob() {
 return job;
 }

 public void setJob(String job) {
 this.job = job;
 }

 public void update() {
 System.out.println("Name " + name + " Job " + job + " get notifcation");
 }

}

Supplier.java

 public class Supplier implements Observer {
 private String name;
 private String address;

 public String getName() {
 return name;
 }

 public void setName(String name) {
 this.name = name;
 }

 public String getAddress() {
 return address;
 }

 public void setAddress(String address) {
 this.address = address;
 }

 public void update() {
 System.out.println("Supplier " + name + " Address " + address + " get notifcation");
 }
} 
Categories: Java Tags: , ,

Bridge Pattern

January 18, 2016 Leave a comment

Bridge pattern is used to decouple an abstraction from its implementation so that two independent each other. In other words, by moving the set of abstract operations to an interface, so that an abstraction will depend on an implementation of the interface.

Here is example.

Bridge Pattern

Bridge Pattern

 

  • AbstractPrinter as Abstraction to define abstraction to interface and hold reference object of type implementor.
  • ConcretePrinter as RefinedAbstraction to extend interface defined by abstraction.
  • IPrinterDocument as Implementator to define the interface for implementation classes.
  • WordPrinterDocument and PdfPrinterDocument as implementation classes.

 

AbstractPrinter.java

public abstract class AbstractPrinter {

protected IPrinterDocument printerDocument;

public AbstractPrinter(IPrintDocument printerDocument){
this.printerDocument = printerDocument;
}

public abstract void printDocument();
}

ConcretePrinter.java

public class ConcretePrinter extends AbstractPrinter {

public ConcretePrinter(IPrinterDocument printerDocument) {
super(printerDocument);
}

@Override
public void printDocument() {
printerDocument.printDocument();
}
}

IPrinterDocument.java

public interface IPrintDocument {
 public void printDocument();
}

PdfPrinterDocument.java

public class PdfPrinterDocument implements IPrinterDocument {
 
 public void printDocument() {
 System.out.println("Pdf Document is being printed");
 }
}

WorderPrintDocument.java

public class WordPrinterDocument implements IPrinterDocument {
 
 public void printDocument() {
 System.out.println("Word Document is being printed");
 }
}
Categories: Java Tags: , ,

Adapter Pattern

December 20, 2015 Leave a comment

Adapter pattern work as middle-man between two incompatible interface. This involve a class that has responbility to join functionality incompatible interface, that class is called adapter.

 

Adapter Design Pattern

Adapter Design Pattern

 

Target interface is interface that client used, so client only know about that interaface to interact with. Adaptee is the existing interface that needs adapting in order for our client to interact with it, it can be third party library, etc. The adapter translate the Adaptee to the target interface. Here is example.

NumberSorter.java

import java.util.Collections;
import java.util.List;

public class NumberSorter {
 public List<Integer> sort(List<Integer> numbers) {
 Collections.sort(numbers);
 return numbers;
 }
}

Sorter.java

public interface Sorter {
 public int[] sort(int[] numbers);
}

SortListAdapter.java

import java.util.ArrayList;
import java.util.List;

public class SortListAdapter implements Sorter {

 private List<Integer> listNumbers;
 private int[] arrayNumbers;
 private NumberSorter numberSorter;

 public int[] sort(int[] numbers) {
 numberSorter = new NumberSorter();
 return convertListToArray(numberSorter.sort(convertArrayToList(numbers)));
 }

 private List<Integer> convertArrayToList(int[] numbers) {
 listNumbers = new ArrayList<Integer>();
 for (int i = 0; i < numbers.length; i++) {
 listNumbers.add(numbers[i]);
 }
 return listNumbers;
 }

 private int[] convertListToArray(List<Integer> numbers) {
 arrayNumbers = new int[numbers.size()];
 for (int i = 0; i < numbers.size(); i++) {
 arrayNumbers[i] = numbers.get(i);
 }
 return arrayNumbers;
 }
}

Client.java

public class Client implements Sorter {

 private SortListAdapter adapter;

 public int[] sort(int[] numbers) {
 adapter = new SortListAdapter();
 return adapter.sort(numbers);
 }

}