1
+ // Decouple an abstraction from its implementation so that the two can vary independently
2
+
3
+ /// <summary>
4
+ /// Refined abstraction method is a bridge between the abstraction and implementation by changing it's behaviour
5
+ /// </summary>
6
+
7
+
8
+ RecipeBase recipe = new RefinedRecipe ( ) ;
9
+
10
+ //set cook as John and cook
11
+ recipe . Cook = new JohnCook ( ) ;
12
+ recipe . Boil ( ) ;
13
+ recipe . Fry ( ) ;
14
+
15
+ //change cook to Branden and cook
16
+ //now you can assign a new cook to the cook independently
17
+ recipe . Cook = new BrandenCook ( ) ;
18
+ recipe . Boil ( ) ;
19
+ recipe . Fry ( ) ;
20
+
21
+ /// <summary>
22
+ /// Abstract Class
23
+ /// </summary>
24
+ public class RecipeBase
25
+ {
26
+
27
+ private Cook cook ;
28
+
29
+ public Cook Cook
30
+ {
31
+ set { cook = value ; }
32
+ }
33
+
34
+ public virtual void Boil ( )
35
+ {
36
+ cook . Boil ( ) ;
37
+ }
38
+
39
+ public virtual void Fry ( )
40
+ {
41
+ cook . Fry ( ) ;
42
+ }
43
+ }
44
+
45
+ /// <summary>
46
+ /// Refined Abstract class with which you can redefine the abstract methods (this is working as a bridge between an abstraction and implementation)
47
+ /// </summary>
48
+ public class RefinedRecipe : RecipeBase
49
+ {
50
+ public override void Boil ( )
51
+ {
52
+ Console . WriteLine ( ) ;
53
+ Console . WriteLine ( "===================Boil Start===============" ) ;
54
+ base . Boil ( ) ;
55
+ Console . WriteLine ( "===================Boil End=================" ) ;
56
+ Console . WriteLine ( ) ;
57
+ }
58
+
59
+ public override void Fry ( )
60
+ {
61
+ Console . WriteLine ( ) ;
62
+ Console . WriteLine ( "===================Fry Start=================" ) ;
63
+ base . Fry ( ) ;
64
+ Console . WriteLine ( "===================Fry End===================" ) ;
65
+ Console . WriteLine ( ) ;
66
+ }
67
+ }
68
+ public abstract class Cook
69
+ {
70
+ public abstract void Boil ( ) ;
71
+ public abstract void Fry ( ) ;
72
+ }
73
+
74
+ public class JohnCook : Cook
75
+ {
76
+ public override void Boil ( )
77
+ {
78
+ Console . WriteLine ( "John is Boiling" ) ;
79
+ }
80
+
81
+ public override void Fry ( )
82
+ {
83
+ Console . WriteLine ( "John is Frying" ) ;
84
+ }
85
+ }
86
+
87
+ public class BrandenCook : Cook
88
+ {
89
+ public override void Boil ( )
90
+ {
91
+ Console . WriteLine ( "Branden is Boiling" ) ;
92
+ }
93
+
94
+ public override void Fry ( )
95
+ {
96
+ Console . WriteLine ( "Branden is Frying" ) ;
97
+ }
98
+ }
0 commit comments