c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I832 materiais10.248 seguidores
Pré-visualização50 páginas
I will define a purse in the form of an interface:
public interface IPurse
{
bool Put(IBankNote note);
bool IsEmpty();
bool IsFull();
bool Has(IBankNote note);
IBankNote Pay(int value);
int Value();
}
The interface defines that a purse must have six methods and hence the properties that a purse should 
have and what you can do with a purse, but the interface defines nothing about how the purse will be 
implemented. It requires a class that implements the interface:
public class Purse : IPurse
{
private IBankNote[] list;
private int count;
public Purse(int size)
{
list = new IBankNote[size];
}
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
127 
Interfaces
public bool Put(IBankNote note)
{
if (count >= list.Length) return false;
list[count++] = note;
return true;
}
public bool IsEmpty()
{
return count == 0;
}
public bool IsFull()
{
return count == list.Length;
}
public bool Has(IBankNote note)
{
for (int i = 0; i < count; ++i) if (list[i].Equals(note)) return true;
return false;
}
public IBankNote Pay(int value)
{
for (int i = 0; i < count; ++i)
if (list[i].Value == value)
{
IBankNote note = list[i];
list[i] = list[--count];
return note;
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
128 
Interfaces
return null;
}
public int Value()
{
int sum = 0;
for (int i = 0; i < count; ++i) sum += list[i].Value;
return sum;
}
}
Explanation
Note first that the class implements the interface IPurse. Note then that there is defined an array with 
name list to the bank notes of the purse, and the type of the array is IBankNote, that is an interface. The 
array is created in the constructor that has a parameter that tells how many notes the purse must have 
room for. There is also a variable count, which keeps track of how many notes there are in the purse. 
After the purse is created it is empty.
Most of the methods are simple. The method Put() must leave a note in the purse, but it can only do 
it if there is room. Therefore it must test, if it is. If there is not space, the method returns false without 
doing anything. Otherwise, it put the note in the purse and returns true. The two methods IsEmpty() and 
IsFull() is quite trivial. The method Has() go through the notes in the purse with a for loop to check if 
you have the bank note, which is queried. Note that this is a requirement that the note class implements 
the method Equals() correctly with value semantic. It\u2019s something that the abstract class BankNote solves.
The method where there is most to note is the method Pay(). Basically it consists of a loop that runs 
through the notes in the purse to find a note with the correct value. If you find a note, there are two 
things to do: the note must be removed from the purse, and the method must return the note. The first 
thing is solved by putting the note on the last place onto the place where the note should be removed. 
Note that this requires that you first save a copy of the note to be returned. Also note that the variable 
count is counted down with the one. If the purse does not have a bank note with the desired value, 
there is a problem because that the method should return something. It has been solved by letting the 
method returns a null reference. It is a solution that can be discussed much, but conversely a solution 
that is widely used, and at least gives the user the ability to test whether the purses had the desired note.
It is important to note that both the interface IPurse and the implementation Purse know nothing about 
the actual banknote classes, but only knows the interface IBankNote. It means that you with no problems 
can add new banknotes classes and without the need to modify the code for Purse.
The last method requires no special comment, and consists merely of a simple pass through the notes 
to calculate the total value.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
129 
Interfaces
The program
There remains then the program itself, which should create a purse, put banknotes into it and then 
spends some money:
class Program
{
static Random rand = new Random();
static void Main(string[] args)
{
IPurse purse = new Purse(10);
Init(purse);
Console.WriteLine(purse.Value());
for (int i = 0; i < 20; ++i) Buy(purse, Create());
Console.WriteLine(purse.Value());
}
static void Buy(IPurse purse, IBankNote note)
{
if (purse.Has(note))
{
purse.Pay(note.Value);
Console.WriteLine(&quot;Bought for {0} crones&quot;, note.Value);
}
else
Console.WriteLine(&quot;Does not have a &quot; + note);
}
static void Init(IPurse purse)
{
while (!purse.IsFull()) purse.Put(Create());
}
static IBankNote Create()
{
switch (rand.Next(15))
{
case 0: return new BankNote1000();
case 1:
case 2: return new BankNote500();
case 3:
case 4:
case 5: return new BankNote200();
case 6:
case 7:
case 8:
case 9: return new BankNote100();
default: return new BankNote50();
}
}
}
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
130 
Interfaces
Explanation
Note first the method Init(), which fills the purse with bank notes.
Notes are created by the method Create() which creates a random bank note. Note that this is the only 
place in the entire program, where the concrete bank note classes appear \u2013 all else is a bank note only 
known as the interface IBankNote. In fact, one could usefully move the code for the method Create() to its 
own factory class, so you got an even better separation of the program and the specific bank note classes.
The method Buy() simulates that you pay with a particular banknote. This method asks where the purse 
has a particular note, and if it\u2019s true the method \u201cbuy for the amount\u201d. Otherwise it may say \u201csorry\u201d. 
Please note that this method only knows the purse through the interface IPurse, and the method will 
work even if a purse was implemented in a different way, as long as it implements the interface.
The program is as follows:
static void Main(string[] args)
{
Pung pung = new ArrPung(10);
Init(pung);
Console.WriteLine(pung.Ialt());
for (int i = 0; i < 20; ++i) Koeb(pung, Create());
Console.WriteLine(pung.Ialt());
}
and below is an example of a test of the program.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
131 
Interfaces
Comment
There are basically two objectives for interfaces. In the first place this is a good programming principle 
is to define a subject in the form of an interface, that is, for example IPurse above. It is said that one 
should program to an interface. In this way, the definition and implementation is separated, and the 
important thing is that the code that uses the subject only knows it through the interface and thus is 
completely independent of the implementation. This ensures that you can change the implementation 
without the need to modify the code that use the subject. Programming to an interface may therefore 
help to ensure that you get a code that is easier to modify. The above program is \u201calmost\u201d independent 
of the implementation of the interface IPurse and there is also only a place where the implementation 
is clear, namely, where the Main() creates the purse:
IPurse pung = new Purse(10);
Another objective of an interface is to allow for on the design level to work with multiple inheritance. 
As mentioned above, a class can only have a single base class \u2013 a class inheritance only from one class. 
It is called sometimes for linear inheritance. You can\u2019t have a design as shown below:
A B
C
where a class C inherits