-
Notifications
You must be signed in to change notification settings - Fork 2
/
Interfaces.cs
115 lines (101 loc) · 4.06 KB
/
Interfaces.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
namespace SampleConApp
{
//Interfaces are similar to abstract classes where the intention is to have only abstract methods which could behave like a contract to the developer who implements the interface. The Class that implements the methods of the interface gaurentees that the methods are implemented by the class. All interfaces are abstract classes, but all abstract classes are not interfaces, as abstract classes can have normal methods.
//Interfaces ensure that there is a clean seperation b/w the declaration(Planning) and implementation(Enforcing). Interface specifies the contract and the implementor class will define the contract.
//Interfaces are the foundations for many of the real time frameworks that are working efficiently and popularly in the software industry. This includes Collections Frameworks, Database Frameworks, MVC Frameworks and many more. For all these interfaces are base level components that will be implemented by the upper classes providing customizations to the components.
interface ISimple
{
void SimpleFunc();
//interface methods are public(Only public). There will no access specifiers. U cannot have fields in the interfaces, but U can have properties. interface methods cannot have a body.
//PS: According to C#8.0, interface have a concept called Default methods which can be implemented by the interface.
}
interface IExample
{
void ExampleFunc();
}
interface IParty
{
void InviteFrieds();
void OrderCake();
void ServeFood();
void ReturnGifts();
}
class BirthdayParty : IParty
{
public void InviteFrieds()
{
Console.WriteLine("Send whatsApp message to all");
}
public void OrderCake()
{
Console.WriteLine("Order cake from Cakes and Slices Shop");
}
public void ReturnGifts()
{
Console.WriteLine("Thanks for coming");
}
public void ServeFood()
{
Console.WriteLine("Order Food from Pizza Hut") ;
}
}
class PromotionParty : IParty
{
public void InviteFrieds()
{
Console.WriteLine("Send Emails to all");
}
public void OrderCake()
{
Console.WriteLine("Get Cake from the Cafeteria");
}
public void ReturnGifts()
{
throw new NotImplementedException("Sorry, No gifts");
}
public void ServeFood()
{
Console.WriteLine("Serve Cool Drinks and Chips along with Cake");
}
}
class EventManager
{
public static IParty CreateParty(string arg)
{
if (arg == "Birthday")
return new BirthdayParty();
else
return new PromotionParty();
}
}
class SimpleExample : IExample, ISimple
{
//All methods of the interface must be implemented as public in the implementor class. The method signature must not be changed in the implementation. If any of the methods are not implemented, then the methods should be still declared with abstract keyword and the class is also made as abstract.....
public void ExampleFunc()
{
Console.WriteLine("Example Func");
}
public void SimpleFunc()
{
Console.WriteLine("Simple Func");
}
}
class InterfaceDemo
{
static void Main(string[] args)
{
//IExample ex = new SimpleExample();
//ex.ExampleFunc();
//ISimple sim = new SimpleExample();
//sim.SimpleFunc();
Console.WriteLine("What kind of party U want");
string answer = Console.ReadLine();
IParty party = EventManager.CreateParty(answer);
party.InviteFrieds();
party.OrderCake();
party.ServeFood();
party.ReturnGifts();
}
}
}