Created
November 10, 2018 03:25
-
-
Save adammb86/973adc1c6a5180c9bb181bed3cf9cb4d to your computer and use it in GitHub Desktop.
Design Pattern - Observer - Sample Case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(Observer) Langkah pembuatan : | |
1. Subject.java | |
2. Abstract Observer.java | |
3. BinaryObserver.java, OctalObserver.java, dan HexaObserver.java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BinaryObserver extends Observer { | |
public BinaryObserver(Subject subject) { | |
super(subject); | |
this.subject.attach(this); | |
} | |
@Override | |
public void update() { | |
System.out.println("Hex String : "+Integer.toBinaryString(subject.getState())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HexaObserver extends Observer { | |
public HexaObserver(Subject subject) { | |
super(subject); | |
this.subject.attach(this); | |
} | |
@Override | |
public void update() { | |
System.out.println("Hex String : "+Integer.toHexString(subject.getState()).toUpperCase()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Observer { | |
protected Subject subject; | |
public Observer(Subject subject) { | |
this.subject = subject; | |
} | |
public abstract void update(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ObserverPatternDemo { | |
public static void main(String[] args) { | |
Subject subject = new Subject(); | |
new HexaObserver(subject); | |
new OctalObserver(subject); | |
new BinaryObserver(subject); | |
System.out.println("First State change 15"); | |
subject.setState(15); | |
System.out.println("Second State change 15"); | |
subject.setState(10); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OctalObserver extends Observer { | |
public OctalObserver(Subject subject) { | |
super(subject); | |
this.subject.attach(this); | |
} | |
@Override | |
public void update() { | |
System.out.println("Octal String : "+Integer.toOctalString(subject.getState())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class Subject { | |
private List<Observer> observers = new ArrayList<Observer>(); | |
private int state; | |
public int getState() { | |
return state; | |
} | |
public void setState(int state) { | |
this.state = state; | |
notifyAllObservers(); | |
} | |
public void attach(Observer observer){ | |
observers.add(observer); | |
} | |
public void notifyAllObservers(){ | |
for (Observer observer : observers) { | |
observer.update(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment