Observer Pattern (use Observable)
This commit is contained in:
@@ -1,4 +0,0 @@
|
|||||||
package AbstractFactory.abst;
|
|
||||||
|
|
||||||
public interface Body {
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package AbstractFactory.abst;
|
|
||||||
|
|
||||||
public interface Wheel {
|
|
||||||
}
|
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
package AbstractFactory;
|
package AbstractFactory.example01;
|
||||||
|
|
||||||
import AbstractFactory.abst.BikeFactory;
|
import AbstractFactory.example01.abst.BikeFactory;
|
||||||
import AbstractFactory.abst.Body;
|
import AbstractFactory.example01.abst.Body;
|
||||||
import AbstractFactory.abst.Wheel;
|
import AbstractFactory.example01.abst.Wheel;
|
||||||
import AbstractFactory.gt.GtBikeFactory;
|
import AbstractFactory.example01.gt.GtBikeFactory;
|
||||||
import AbstractFactory.sam.SamFactory;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package AbstractFactory.abst;
|
package AbstractFactory.example01.abst;
|
||||||
|
|
||||||
public interface BikeFactory {
|
public interface BikeFactory {
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package AbstractFactory.example01.abst;
|
||||||
|
|
||||||
|
public interface Body {
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package AbstractFactory.example01.abst;
|
||||||
|
|
||||||
|
public interface Wheel {
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package AbstractFactory.gt;
|
package AbstractFactory.example01.gt;
|
||||||
|
|
||||||
import AbstractFactory.abst.BikeFactory;
|
import AbstractFactory.example01.abst.BikeFactory;
|
||||||
import AbstractFactory.abst.Body;
|
import AbstractFactory.example01.abst.Body;
|
||||||
import AbstractFactory.abst.Wheel;
|
import AbstractFactory.example01.abst.Wheel;
|
||||||
|
|
||||||
public class GtBikeFactory implements BikeFactory {
|
public class GtBikeFactory implements BikeFactory {
|
||||||
@Override
|
@Override
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package AbstractFactory.example01.gt;
|
||||||
|
|
||||||
|
import AbstractFactory.example01.abst.Body;
|
||||||
|
|
||||||
|
public class GtBody implements Body {
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package AbstractFactory.example01.gt;
|
||||||
|
|
||||||
|
import AbstractFactory.example01.abst.Wheel;
|
||||||
|
|
||||||
|
public class GtWheel implements Wheel {
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package AbstractFactory.example01.sam;
|
||||||
|
|
||||||
|
import AbstractFactory.example01.abst.Body;
|
||||||
|
|
||||||
|
public class SamBody implements Body {
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package AbstractFactory.sam;
|
package AbstractFactory.example01.sam;
|
||||||
|
|
||||||
import AbstractFactory.abst.BikeFactory;
|
import AbstractFactory.example01.abst.BikeFactory;
|
||||||
import AbstractFactory.abst.Body;
|
import AbstractFactory.example01.abst.Body;
|
||||||
import AbstractFactory.abst.Wheel;
|
import AbstractFactory.example01.abst.Wheel;
|
||||||
|
|
||||||
public class SamFactory implements BikeFactory {
|
public class SamFactory implements BikeFactory {
|
||||||
@Override
|
@Override
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package AbstractFactory.example01.sam;
|
||||||
|
|
||||||
|
import AbstractFactory.example01.abst.Wheel;
|
||||||
|
|
||||||
|
public class SamWheel implements Wheel {
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package AbstractFactory2;
|
package AbstractFactory.example02;
|
||||||
|
|
||||||
import AbstractFactory2.abst.Button;
|
import AbstractFactory.example02.abst.Button;
|
||||||
import AbstractFactory2.abst.GuiFactory;
|
import AbstractFactory.example02.abst.GuiFactory;
|
||||||
import AbstractFactory2.abst.TextArea;
|
import AbstractFactory.example02.abst.TextArea;
|
||||||
import AbstractFactory2.concrete.FactoryInstance;
|
import AbstractFactory.example02.concrete.FactoryInstance;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package AbstractFactory2.abst;
|
package AbstractFactory.example02.abst;
|
||||||
|
|
||||||
public interface Button {
|
public interface Button {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package AbstractFactory2.abst;
|
package AbstractFactory.example02.abst;
|
||||||
|
|
||||||
public interface GuiFactory {
|
public interface GuiFactory {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package AbstractFactory2.abst;
|
package AbstractFactory.example02.abst;
|
||||||
|
|
||||||
public interface TextArea {
|
public interface TextArea {
|
||||||
String getText();
|
String getText();
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package AbstractFactory2.concrete;
|
package AbstractFactory.example02.concrete;
|
||||||
|
|
||||||
import AbstractFactory2.abst.Button;
|
import AbstractFactory.example02.abst.Button;
|
||||||
import AbstractFactory2.abst.GuiFactory;
|
import AbstractFactory.example02.abst.GuiFactory;
|
||||||
import AbstractFactory2.abst.TextArea;
|
import AbstractFactory.example02.abst.TextArea;
|
||||||
|
|
||||||
public class FactoryInstance {
|
public class FactoryInstance {
|
||||||
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package AbstractFactory.gt;
|
|
||||||
|
|
||||||
import AbstractFactory.abst.Body;
|
|
||||||
|
|
||||||
public class GtBody implements Body {
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package AbstractFactory.gt;
|
|
||||||
|
|
||||||
import AbstractFactory.abst.Wheel;
|
|
||||||
|
|
||||||
public class GtWheel implements Wheel {
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package AbstractFactory.sam;
|
|
||||||
|
|
||||||
import AbstractFactory.abst.Body;
|
|
||||||
|
|
||||||
public class SamBody implements Body {
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package AbstractFactory.sam;
|
|
||||||
|
|
||||||
import AbstractFactory.abst.Wheel;
|
|
||||||
|
|
||||||
public class SamWheel implements Wheel {
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package ChainOfResponsibility;
|
package ChainOfResponsibility.example01;
|
||||||
|
|
||||||
abstract public class Calculator {
|
abstract public class Calculator {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package ChainOfResponsibility;
|
package ChainOfResponsibility.example01;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package ChainOfResponsibility;
|
package ChainOfResponsibility.example01;
|
||||||
|
|
||||||
public class PlusCalculator extends Calculator {
|
public class PlusCalculator extends Calculator {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package ChainOfResponsibility;
|
package ChainOfResponsibility.example01;
|
||||||
|
|
||||||
public class Request {
|
public class Request {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package ChainOfResponsibility;
|
package ChainOfResponsibility.example01;
|
||||||
|
|
||||||
public class SubCalculator extends Calculator {
|
public class SubCalculator extends Calculator {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package ChainOfResponsibility2;
|
package ChainOfResponsibility.example02;
|
||||||
|
|
||||||
public class Application {
|
public class Application {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package ChainOfResponsibility2;
|
package ChainOfResponsibility.example02;
|
||||||
|
|
||||||
public class Armor implements Defense{
|
public class Armor implements Defense{
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package ChainOfResponsibility2;
|
package ChainOfResponsibility.example02;
|
||||||
|
|
||||||
public class Attack {
|
public class Attack {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package ChainOfResponsibility2;
|
package ChainOfResponsibility.example02;
|
||||||
|
|
||||||
public interface Defense {
|
public interface Defense {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package Observer;
|
package Observer.example01;
|
||||||
|
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package Observer;
|
package Observer.example01;
|
||||||
|
|
||||||
public class Button {
|
public class Button {
|
||||||
|
|
||||||
21
design-pattern/src/Observer/example02/Application.java
Normal file
21
design-pattern/src/Observer/example02/Application.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package Observer.example02;
|
||||||
|
|
||||||
|
import java.util.Observable;
|
||||||
|
import java.util.Observer;
|
||||||
|
|
||||||
|
public class Application {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Button button = new Button();
|
||||||
|
|
||||||
|
button.addObserver(new Observer() {
|
||||||
|
@Override
|
||||||
|
public void update(Observable o, Object arg) {
|
||||||
|
System.out.println(o + " is Clicked");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
button.onClick();
|
||||||
|
button.onClick();
|
||||||
|
button.onClick();
|
||||||
|
}
|
||||||
|
}
|
||||||
11
design-pattern/src/Observer/example02/Button.java
Normal file
11
design-pattern/src/Observer/example02/Button.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package Observer.example02;
|
||||||
|
|
||||||
|
import java.util.Observable;
|
||||||
|
|
||||||
|
public class Button extends Observable {
|
||||||
|
|
||||||
|
public void onClick() {
|
||||||
|
setChanged();
|
||||||
|
notifyObservers();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user