File tree 16 files changed +242
-0
lines changed 16 files changed +242
-0
lines changed Original file line number Diff line number Diff line change
1
+ # observer
2
+
3
+ This is repository of http://androidcode.pl blog. It shows uses of Observer in Android. It is a part of Design Patterns - Observer post in the blog.
Original file line number Diff line number Diff line change
1
+ public class Main {
2
+
3
+ public static void main () {
4
+ ActionHouse house = new ActionHouse ();
5
+ Auctions auctions = new Auctions ();
6
+ Bids bids = new Bids ();
7
+
8
+ //register
9
+ house .registerObserver (auctions );
10
+ house .registerBids (bids );
11
+
12
+ //new action coming from server and runs ActionHouse
13
+ house .newBid (bidEvent ); //update Bids and Auctions modules
14
+ house .newAuction (auctionEvent ); //update Auctions module
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ public class Auction {
2
+
3
+ //body
4
+ }
Original file line number Diff line number Diff line change
1
+ public class Bid {
2
+
3
+ //body
4
+ }
Original file line number Diff line number Diff line change
1
+ public class NewAuctionEvent {
2
+
3
+ private Auction auction ;
4
+ //other fields
5
+
6
+ public Auction getAuction () {
7
+ return auction ;
8
+ }
9
+
10
+ //other methods
11
+ }
Original file line number Diff line number Diff line change
1
+ public class NewBidEvent {
2
+
3
+ private Bid bid ;
4
+ //other fields
5
+
6
+ public Bid getBid () {
7
+ return bid ;
8
+ }
9
+
10
+ //other methods
11
+ }
Original file line number Diff line number Diff line change
1
+ public class AuctionHouse implements Observable {
2
+
3
+ private List <Observer > observers ;
4
+ //other fields
5
+
6
+ public AuctionHouse () {
7
+ observers = new ArrayList ();
8
+ }
9
+
10
+ @ Override
11
+ public void registerObserver (Observer observer ) {
12
+ observers .add (observer );
13
+ }
14
+
15
+ @ Override
16
+ public void unregisterObserver (Observer observer ) {
17
+ observers .remove (observer );
18
+ }
19
+
20
+ @ Override
21
+ public void notifyObservers () {
22
+ for (Observer observer : observers )
23
+ observer .update (this , null );
24
+ }
25
+
26
+ @ Override
27
+ public void notifyObservers (Object args ) {
28
+ for (Observer observer : observers )
29
+ observer .update (this , args );
30
+ }
31
+
32
+ public void newBid (NewBidEvent event ) {
33
+ //check event details and add if is OK
34
+ Bid bid = event .getBid ();
35
+ notifyObservers (bid );
36
+ }
37
+
38
+ public void newAuction (NewAuctionEvent auction ) {
39
+ //check event details and add if is OK
40
+ Auction auction = event .getAuction ();
41
+ notifyObservers (auction );
42
+ }
43
+
44
+ //others methods
45
+ }
Original file line number Diff line number Diff line change
1
+ public interface Observable {
2
+ public void registerObserver (Observer observer );
3
+ public void unregisterObserver (Observer observer );
4
+ public void notifyObservers (Object args );
5
+ public void notifyObservers ();
6
+ }
Original file line number Diff line number Diff line change
1
+ public class Auctions implements Observer {
2
+
3
+ private List <Auction > auctions ;
4
+ //other fields
5
+
6
+ public Auction (List <Auction > auctions ) {
7
+ this .auctions = auctions ;
8
+ }
9
+
10
+ @ Override
11
+ public void update (Observable observable , Object args ) {
12
+ if (observable instanceof AuctionManager ) {
13
+ if (args instanceof Auction ) {
14
+ Auction auction = (Auction ) args ;
15
+ auctions .add (auction );
16
+ //refresh UI list
17
+ }
18
+ else if (args == instanceof Bid ) {
19
+ Bid bid = (Bid ) args ;
20
+ int auctionIndex = auctions .indexOf (bid .getAuction ());
21
+ if (auctionIndex != -1 ) {
22
+ auctions .get (auctionIndex ).addBid (bid );
23
+ //refresh UI list
24
+ }
25
+ }
26
+ }
27
+ }
28
+
29
+ //other methods
30
+ }
Original file line number Diff line number Diff line change
1
+ public class Bids implements Observer {
2
+
3
+ private Auction auction ;
4
+ private List <Bid > bids ;
5
+ //other fields
6
+
7
+ public Bids (Auction auction ) {
8
+ this .auction = auction ;
9
+ this .bids = auction .getBids ();
10
+ }
11
+
12
+ @ Override
13
+ public void update (Observable observable , Object args ) {
14
+ if (observable instanceof AuctionManager ) {
15
+ if (args instanceof Bid ) {
16
+ Bid bid = (Bid ) args ;
17
+ if (bid .getAuction () == auction ) {
18
+ bids .add (bid );
19
+ //refresh UI list
20
+ }
21
+ }
22
+ }
23
+ }
24
+
25
+ //other methods
26
+ }
Original file line number Diff line number Diff line change
1
+ public interface Observer {
2
+
3
+ public void update (Observable observable , Object args );
4
+ }
Original file line number Diff line number Diff line change
1
+ public class ConcreteObservable implements Observable {
2
+
3
+ private List <Observer > observers ;
4
+
5
+ //other fields
6
+
7
+ public ConcreteObservable () {
8
+ observers = new ArrayList ();
9
+ }
10
+
11
+ @ Override
12
+ public void registerObserver (Observer observer ) {
13
+ observers .add (observer );
14
+ }
15
+
16
+ @ Override
17
+ public void unregisterObserver (Observer observer ) {
18
+ observers .remove (observer );
19
+ }
20
+
21
+ @ Override
22
+ public void notifyObservers () {
23
+ for (Observer observer : observers )
24
+ observer .update (this , null );
25
+ }
26
+
27
+ @ Override
28
+ public void notifyObservers (Object object ) {
29
+ for (Observer observer : observers )
30
+ observer .update (this , object );
31
+ }
32
+
33
+ public void method1 () {
34
+ //do some work
35
+ notifyObservers ();
36
+ }
37
+
38
+ public void method2 () {
39
+ //do some work
40
+ Event event = new Event (); //args wrapped in event
41
+ notifyObservers (event );
42
+ }
43
+
44
+ //others methods
45
+ }
Original file line number Diff line number Diff line change
1
+ public interface Observable {
2
+ public void registerObserver (Observer observer );
3
+ public void unregisterObserver (Observer observer );
4
+ public void notifyObservers (Object args );
5
+ public void notifyObservers ();
6
+ }
Original file line number Diff line number Diff line change
1
+ public interface Observer {
2
+
3
+ public void update (Observable observable , Object args );
4
+ }
Original file line number Diff line number Diff line change
1
+ public class Observer1 implements Observer {
2
+
3
+ @ Override
4
+ public void update (Observable observable , Object object ) {
5
+ if (observable instanceof ConcreteObservable ) {
6
+ //do some work
7
+ }
8
+ else {
9
+ //do some work
10
+ }
11
+ }
12
+
13
+ //other methods
14
+ }
Original file line number Diff line number Diff line change
1
+ public class Observer2 implements Observer {
2
+
3
+ @ Override
4
+ public void update (Observable observable , Object object ) {
5
+ if (observable instanceof ConcreteObservable ) {
6
+ if (object instanceof Event ) {
7
+ //do some work
8
+ }
9
+ }
10
+ }
11
+
12
+ //other methods
13
+ }
You can’t perform that action at this time.
0 commit comments