File tree Expand file tree Collapse file tree 11 files changed +59
-12
lines changed
StructuralPatterns/Bridge Expand file tree Collapse file tree 11 files changed +59
-12
lines changed Original file line number Diff line number Diff line change @@ -33,7 +33,7 @@ public boolean isLastStudent() {
3333
3434 @ Override
3535 public Student currentStudent () {
36- if ( index < students .size ()) {
36+ for (; index < students .size (); index ++) {
3737 if (students .get (index ).getType ().equals ("College" )){
3838 return students .get (index );
3939 }
Original file line number Diff line number Diff line change @@ -33,7 +33,7 @@ public boolean isLastStudent() {
3333
3434 @ Override
3535 public Student currentStudent () {
36- if ( index < students .size ()) {
36+ for (; index < students .size (); index ++) {
3737 if (students .get (index ).getType ().equals ("High School" )){
3838 return students .get (index );
3939 }
Original file line number Diff line number Diff line change @@ -8,12 +8,15 @@ public static void main(String[] args){
88 school .addStudent (new Student ("Tom" , "High School" ));
99 school .addStudent (new Student ("Jack" , "High School" ));
1010 school .addStudent (new Student ("Maor" , "College" ));
11+
1112 Iterator highSchoolIterator = school .createIterator ("High School" );
12- System .out .println ("First Student: " + highSchoolIterator .currentStudent ().getName ());
13+ System .out .println ("First High School Student: " + highSchoolIterator .currentStudent ().getName ());
1314 while (!highSchoolIterator .isLastStudent ()){
1415 System .out .println ("High School Student: " + highSchoolIterator .nextStudent ().getName ());
1516 }
17+
1618 Iterator collegeIterator = school .createIterator ("College" );
19+ System .out .println ("First College Student: " + collegeIterator .currentStudent ().getName ());
1720 while (!collegeIterator .isLastStudent ()){
1821 System .out .println ("College Student: " + collegeIterator .nextStudent ().getName ());
1922 }
Original file line number Diff line number Diff line change 66public class School {
77 private List <Student > students = new ArrayList <>();
88
9- public List <Student > getStudents (){return students ;}
9+ public List <Student > getStudents (){ return students ; }
1010
11- public void addStudent (Student student ){students .add (student );}
11+ public void addStudent (Student student ){ students .add (student ); }
1212
13- public void removeStudent (Student student ){students .remove (student );}
13+ public void removeStudent (Student student ){ students .remove (student ); }
1414
1515 public Iterator createIterator (String type ) {
1616 if (type .equals ("High School" )){
Original file line number Diff line number Diff line change @@ -12,14 +12,14 @@ public static void main(String[] args) {
1212 memento = originator .createMemento ();
1313 caretaker .addMemento (memento );
1414 originator .setState ("Elephant" );
15- System .out .println ("Originator Current State: " + originator .getState ());
15+ System .out .println ("Originator Current State: " + originator .getState ());
1616 System .out .println ("Originator restoring to previous state..." );
1717 memento = caretaker .getMemento (1 );
1818 originator .setMemento (memento );
19- System .out .println ("Originator Current State: " + originator .getState ());
19+ System .out .println ("Originator Current State: " + originator .getState ());
2020 System .out .println ("Again restoring to previous state..." );
2121 memento = caretaker .getMemento (0 );
2222 originator .setMemento (memento );
23- System .out .println ("Originator Current State: " + originator .getState ());
23+ System .out .println ("Originator Current State: " + originator .getState ());
2424 }
2525}
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .StateDP ;
2+
3+ public class AlertStateContext {
4+ private MobileAlertState currentState ;
5+ public AlertStateContext () {
6+ currentState = new Vibration (); }
7+ public void setState (MobileAlertState state ) {
8+ currentState = state ; }
9+ public void alert () { currentState .alert (this ); }
10+ }
11+
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .StateDP ;
2+
3+ public class Main {
4+ public static void main (String [] args ){
5+ AlertStateContext stateContext = new AlertStateContext ();
6+ stateContext .alert ();
7+ stateContext .alert ();
8+ stateContext .setState (new Silent ());
9+ stateContext .alert ();
10+ stateContext .alert ();
11+ stateContext .alert ();
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .StateDP ;
2+
3+ public interface MobileAlertState {
4+ void alert (AlertStateContext ctx );
5+ }
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .StateDP ;
2+
3+ public class Silent implements MobileAlertState {
4+ @ Override
5+ public void alert (AlertStateContext ctx ) {
6+ System .out .println ("silent..." );
7+ }
8+ }
Original file line number Diff line number Diff line change 1+ package BehavioralPatterns .StateDP ;
2+
3+ public class Vibration implements MobileAlertState {
4+ @ Override
5+ public void alert (AlertStateContext ctx ) {
6+ System .out .println ("vibration..." );
7+ }
8+ }
You can’t perform that action at this time.
0 commit comments