File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package designPattern .Decorator ;
2+
3+ public abstract class DecorateIcecream implements Icecream {
4+ Icecream icecream ;
5+ }
6+
7+ class Chocolate extends DecorateIcecream {
8+ public Chocolate (Icecream icecream ) {
9+ this .icecream = icecream ;
10+ }
11+
12+ @ Override
13+ public String description () {
14+ return "Chocolate" ;
15+ }
16+
17+ @ Override
18+ public Double cost () {
19+ return this .icecream .cost () + 5.0 ;
20+ }
21+ }
22+
23+ class Peanuts extends DecorateIcecream {
24+ public Peanuts (Icecream icecream ) {
25+ this .icecream = icecream ;
26+ }
27+
28+ @ Override
29+ public String description () {
30+ return "Peanuts" ;
31+ }
32+
33+ @ Override
34+ public Double cost () {
35+ return this .icecream .cost () + 3.0 ;
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package designPattern .Decorator ;
2+
3+ public interface Icecream {
4+ public String description ();
5+
6+ public Double cost ();
7+ }
8+
9+ class Vanilla implements Icecream {
10+
11+ @ Override
12+ public String description () {
13+ return "Vanila Icecream!" ;
14+ }
15+
16+ @ Override
17+ public Double cost () {
18+ return 15.0 ;
19+ }
20+ }
21+
22+ class Strawberry implements Icecream {
23+
24+ @ Override
25+ public String description () {
26+ return "Strawberry Icecream!" ;
27+ }
28+
29+ @ Override
30+ public Double cost () {
31+ return 20.0 ;
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package designPattern .Decorator ;
2+
3+ public class TestDecorator {
4+
5+ public static void main (String [] args ) {
6+ Icecream vanilla = new Vanilla ();
7+ System .out .println ("Simple vanilla cost: " + vanilla .cost ());
8+
9+ Icecream vanillaWithPeanuts = new Peanuts (vanilla );
10+ System .out .println ("Vanilla with peanuts cost: " + vanillaWithPeanuts .cost ());
11+
12+ Icecream vanillaWithChocolate = new Chocolate (vanilla );
13+ System .out .println ("Vanilla with chocolate cost: " + vanillaWithChocolate .cost ());
14+
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 1+ package designPattern .Observer ;
2+
3+ public interface Observer {
4+ public void update (String message );
5+ public void display ();
6+ }
Original file line number Diff line number Diff line change 1+ package designPattern .Observer ;
2+
3+ public class Student implements Observer {
4+
5+ private String message ;
6+ public Student (StudentData data ) {
7+ data .registerObserver (this );
8+ }
9+
10+ public void update (String message ) {
11+ this .message = message ;
12+ display ();
13+ }
14+ public void display () {
15+ System .out .println (this .message );
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ package designPattern .Observer ;
2+
3+ import java .util .ArrayList ;
4+
5+ public class StudentData {
6+ private String message ;
7+ private ArrayList <Observer > observers ;
8+
9+ public StudentData () {
10+ observers = new ArrayList <>();
11+ }
12+ public void registerObserver (Observer o ) {
13+ observers .add (o );
14+ }
15+ public void removeObserver (Observer o ) {
16+ observers .remove (o );
17+ }
18+
19+ public void notifyObservers () {
20+ for (int i = 0 ; i < observers .size (); i ++) {
21+ Observer obs = (Observer ) observers .get (i );
22+ obs .update (message );
23+ }
24+ }
25+
26+ public void setMessage (String msg ) {
27+ this .message = msg ;
28+ }
29+
30+ public String getMessage () {
31+ return this .message ;
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package designPattern .Observer ;
2+
3+ public class TestObserver {
4+
5+ public static void main (String [] args ) {
6+ StudentData data = new StudentData ();
7+ Observer std = new Student (data );
8+ String msg = "Hello World" ;
9+ data .setMessage (msg );
10+
11+ if (!data .getMessage ().equals ("" )) {
12+ data .notifyObservers ();
13+ }
14+
15+ data .removeObserver (std );
16+ if (!data .getMessage ().equals ("" )) {
17+ data .notifyObservers ();
18+ }
19+
20+ }
21+
22+ }
Original file line number Diff line number Diff line change 1+ package designPattern .Singleton ;
2+
3+ public class Settings {
4+ private static Settings firstObject = null ;
5+ private String font = "calibri" ;
6+ private String color = "black" ;
7+
8+ private Settings () {
9+ }
10+
11+ public static Settings getObject () {
12+ if (firstObject == null ) {
13+ firstObject = new Settings ();
14+ }
15+ return firstObject ;
16+ }
17+
18+ public void setFont (String fontName ) {
19+ this .font = fontName ;
20+ }
21+
22+ public void setColor (String colorName ) {
23+ this .color = colorName ;
24+ }
25+
26+ public String getFont () {
27+ return this .font ;
28+ }
29+
30+ public String getColor () {
31+ return this .color ;
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package designPattern .Singleton ;
2+
3+ public class TestSetting {
4+
5+ public static void main (String [] args ) {
6+ Settings setting = Settings .getObject ();
7+ System .out .println ("Setting Object Color: " + setting .getColor ());
8+ System .out .println ("Font: " + setting .getFont ());
9+ Settings set1 =Settings .getObject ();
10+ set1 .setColor ("Red" );
11+ System .out .println ("Set Object Color: " + set1 .getColor ());
12+ System .out .println ("Font: " + set1 .getFont ());
13+ System .out .println ("Setting Object Color: " + setting .getColor ());
14+ }
15+ }
You can’t perform that action at this time.
0 commit comments