Skip to content

Commit 7702db1

Browse files
committed
🎉 initial commit
0 parents  commit 7702db1

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

com/Jobservers.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.patternObsTest;
2+
3+
/**
4+
* Created by sdmg15 on 08/03/17.
5+
*/
6+
public interface Jobservers {
7+
8+
String update(Jsubject subj);
9+
10+
11+
}

com/Jsubject.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.patternObsTest;
2+
3+
/**
4+
* Created by sdmg15 on 08/03/17.
5+
*/
6+
public interface Jsubject {
7+
8+
void attach(Jobservers[] obs);
9+
10+
void detach(int obs);
11+
12+
void noty();
13+
}

com/Observer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.patternObsTest;
2+
3+
/**
4+
* Created by sdmg15 on 08/03/17.
5+
*/
6+
public class Observer implements Jobservers {
7+
8+
public String update(Jsubject j){
9+
return "Someone wants to set the value of the attribue : length.";
10+
}
11+
}

com/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Java Designs Pattern
2+
3+
Here's is an implementation of some Designs Pattern from scratch.
4+
Code source files are available in the package `com.patternObsTest`.
5+
6+
__What is a Design Pattern ?__
7+
> a software design pattern is a general reusable solution to a commonly occurring problem within a given context in s software design --- Wikipedia
8+
9+
# Pattern Observer
10+
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
11+
12+
```java
13+
14+
// importing classes in the package
15+
import com.patternObsTest.*;
16+
17+
public class test{
18+
public static void main(String[] args[]){
19+
20+
Subject s = new Subject();
21+
Observer o = new Observer();
22+
Observer o2 = new Observer();
23+
Observer[] arr = {o,o2};
24+
25+
s.attach(arr);
26+
27+
/*now we set the value of the attribute length contains in the Subject.
28+
NOTE: the param is an enum (valLEngth)
29+
*/
30+
31+
s.setLength(valLength.Val2);
32+
//Now look at your console to see the result :p
33+
34+
}
35+
36+
}
37+
38+

com/Subject.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Created by sdmg15 on 08/03/17.
3+
*/
4+
package com.patternObsTest;
5+
6+
public class Subject implements Jsubject {
7+
8+
public Jobservers[] tab;
9+
10+
private int length;
11+
12+
public void setLength(valLEngth val){
13+
this.length = val.getVal();
14+
this.noty();
15+
}
16+
17+
public int getLength(){ return this.length;}
18+
19+
@Override
20+
public void attach(Jobservers[] obs){
21+
22+
this.tab = obs;
23+
}
24+
25+
public void detach(int i){
26+
this.tab[i]= new Observer();
27+
}
28+
29+
public void noty(){
30+
31+
for(int i = 0; i < this.tab.length; i++){
32+
System.out.println(this.tab[i].update(this));
33+
}
34+
35+
}
36+
}

com/valLength.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.patternObsTest;
2+
3+
/**
4+
* Created by sdmg15 on 17/03/17.
5+
*/
6+
public enum valLength {
7+
8+
Val1(24),
9+
Val2(23),
10+
val3(45);
11+
public int val;
12+
13+
valLength(int val){
14+
this.val = val;
15+
}
16+
17+
public int getVal(){
18+
return this.val;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return "I'm an Enum !";
24+
}
25+
}

0 commit comments

Comments
 (0)