Witam,
kawałek kodu w książce wygląda tak:
class Safe {
private string safeCombination = "12345";
private Jewels content = new Jewels();
public Jewels Open(string combination) {
if (combination == safeCombination)
return content;
return null;
}
public void PickLock(Locksmith lockpicker) {
lockpicker.WriteDownCombination(safeCombination);
}
}
class Locksmith {
public void OpenSafe(Safe safe, Owner owner) {
safe.PickLock(this);
Jewels safeContents = safe.Open(writtenDownCombination);
ReturnContents(safeContents, owner);
}
private string writtenDownCombination = null;
public void WriteDownCombination(string combination) {
writtenDownCombination = combination;
}
public virtual void ReturnContents(Jewels safeContents, Owner owner) {
owner.ReceiveContent(safeContents);
}
}
class JewelThief : Locksmith {
private Jewels stolenJewels = null;
public override void ReturnContents(Jewels safeContents, Owner owner) {
stolenJewels = safeContents;
}
}
Natomiast moim zdaniem powinno wyglądać to tak:
class Safe {
private string safeCombination = "12345";
private Jewels content = new Jewels();
public Jewels Open(string combination) {
if (combination == this.safeCombination)
return content;
return null;
}
public string PickLock(Locksmith lockpicker) {
return this.safeCombination;
}
}
class Locksmith {
public Jewels OpenSafe(Safe safe) {
string safeCode = safe.PickLock(this);
return safe.Open(safeCode);
}
public virtual void ReturnContent(Safe safe, Owner owner) {
Jewels safeJewels = OpenSafe(safe);
owner.ReceiveContent(safeJewels);
}
}
class JewelThief : Locksmith {
Jewels stolenJewels = null;
public override void ReturnContent(Safe safe, Owner owner) {
Jewels safeJewels = OpenSafe(safe);
this.stolenJewels = safeJewels;
}
}
- Dziwi mnie to, że czasem używamy
.this
a czasem nie. W książce jestreturn contents
w klasieSafe
. Przecież dotyczy to pola tego obiektu, więc dlaczego nie ma tamthis
? - Czy pole
writtenDownCombination
w klasieLocksmith
jest potrzebne? Przecież i tak nie mamy do niego dostępu. Ja pobieram kod do sejfu ale zapisuje w lokalnym stringu. Nie widzę potrzeby robienia z tego pola. - Czy potrzebna jest metoda
WriteDownCombination
w klasieLocksmith
? Ja po prostu zwracam string wPickLock
. Nie rozumiem po co pisać:
public void WriteDownCombination(string combination) {
this.writtenDownCombination = combination;
}
zamiast:
this.writtenDownCombination = PickLock(this); // wtedy PickLock zwróci string
Tutaj te dwa obiekty się ze sobą komunikują, ale moim zdaniem jest to takie mieszanie się z kodem. Locksmith
wywołuje metodę PickLock
która wywołuje metodę Locksmitha
która ustawia writeDownCombination
. Serio jest to potrzebne? Czy po prostu PickLock
nie może zwracać stringa który ustawia writeDownCombination
?
4. W książce metoda OpenSafe
otwiera sejf i oddaje zawartość właścicielowi. Czy metoda nie powinna robić jednej rzeczy? Ja zrobiłem to tak, że zamiast void
metoda zwraca zawartość sejfu, a zawartość zwracam właścicielowi wywołując metodę ReturnContent
i w parametrze podając sejf. Tak wydaje mi się najbardziej logicznie.
Czytam tę książkę i sam już nie wiem, czy rozumuję prawidłowo, czy jednak podejście książkowe jest lepsze