Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 |
abstract class HumanCondition implements CreatureCondition { public final function fulfills( Creature $creature ):bool { if( $creature instanceof Human ) { return fulfillsHuman($creature); } return false; // oder Exception werfen } public abstract function fulfillsHuman(Human $human): bool; } |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Chromanoid« (04.02.2017, 16:58)
Weil z.B. Animals keine Autos haben. Deswegen braucht man den Typ. Wenn du Vorschläge hast bin ich völlig offen und präsentiere sie gern am Montag. Mich nervt das ganze auch leicht und ich hätte gern was "anständiges".Ich verstehe nicht warum die Conditions nur für eine bestimmte Klasse eine Methodendeklaration haben sollten.
Mit dem von Dir beschriebenen Konstrukt hat das ja nichts zu tun. Auch warum deine Creatures selbst eine Methode haben, mit der sie sich selbst auf eine Bedingung prüfen können verstehe ich nicht.
Sowas vielleicht?
Quellcode
1 2 3 4 5 6 7 8 9 10 abstract class HumanCondition implements CreatureCondition { public final function fulfills( Creature $creature ):bool { if( $creature instanceof Human ) { return fulfillsHuman($creature); } return false; // oder Exception werfen } public abstract function fulfillsHuman(Human $human): bool; }
Beim Schachteln der Conditions geht der spezifische Typ der Condition ja eh verloren. Die Conditions sollten also selbst wissen, ob sie auf einen bestimmten Typ anwendbar sind. Man kann ja ohne Probleme mit And eine Animal- und eine HumanCondition zusammensetzen, die per se nie erfüllbar ist.
Quellcode |
|
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 |
interface Creature { public function fulfills(CreatureCondition $condition): bool; } interface CreatureCondition { public function fulfillsCreature(Creature $creature): bool; } abstract class HumanCondition implements CreatureCondition { public final function fulfillsCreature( Creature $creature ):bool { if( $creature instanceof Human ) { return fulfillsHuman($creature); } return false; // oder Exception werfen } public abstract function fulfillsHuman(Human $human): bool; } abstract class AnimalCondition implements CreatureCondition { public final function fulfillsCreature( Creature $creature ):bool { if( $creature instanceof Animal) { return fulfillsAnimal($creature); } return false; // oder Exception werfen } public abstract function fulfillsAnimal(Animal $animal): bool; } class Human implements Creature { public function fulfills(CreatureCondition $condition): bool { return $condition.fulfillsCreature($this); } } class Animal implements Creature { public function fulfills(CreatureCondition $condition): bool { return $condition.fulfillsCreature($this); } } |
Quellcode |
|
1 2 3 4 5 |
class HasCar extends HumanCondition { public function fulfillsHuman(Human $human): bool { return count($human->$cars) > 0; } } |
Zitat
In the meantime, I hope I've made my main point clear, which is that polymorphism only makes sense when the polymorphic behavior is really a behavior of the target. When it's the behavior of the observer, you need runtime typing.
Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von »Chromanoid« (04.02.2017, 18:15)
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Runtime-Zyklen sind total in Ordnung. Dependency-Zyklen sind problematisch. Interfaces und ein Mediator sind aber absolut sauber.Mit Interfaces verschleiert man die Zyklen ja einfach nur.
Dieser Beitrag wurde bereits 4 mal editiert, zuletzt von »Chromanoid« (04.02.2017, 19:17)
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Nope. Ich wüsste nicht wieso Hinzufügen irgendeinen Einfluss auf einen bestehenden Mediator hätte. Im Gegenteil, mit Instanceof müsstest du an viel mehr existierende Stellen ran, wenn du eine Klasse entfernst oder hinzufügst. Entfernst du beim Mediator eine der Klassen, löschst du halt einfach nur den entsprechenden Mediator.müsste man immer gleich auch das ConcreteMediator-Modul anpassen, obwohl man eigentlich nur das Modul Plant hinzufügen möchte.
Quellcode |
|
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 |
<?php interface ConditionInterface { public function fulfillsHuman(Human $human): bool; public function fulfillsAnimal(Animal $animal): bool; } interface ExpressionInterface { public function fulfills(CreatureInterface $creature): bool; } interface CreatureInterface { public function fulfills(ConditionInterface $condition): bool; } // ---- abstract class HumanCondition implements ExpressionInterface, ConditionInterface { final public function fulfills(CreatureInterface $creature): bool { return $creature->fulfills($this); } final public function fulfillsAnimal(Animal $animal): bool { throw new Exception('HumanCondition erlaubt keine Animals'); } } final class Human implements CreatureInterface { public function fulfills(ConditionInterface $condition): bool { return $condition->fulfillsHuman($this); } } |
Das stimmt doch einfach nicht. Mit instanceof muss ich nur die Conditions anfassen, die auch die entsprechende Kreatur betreffen.Nope. Ich wüsste nicht wieso Hinzufügen irgendeinen Einfluss auf einen bestehenden Mediator hätte. Im Gegenteil, mit Instanceof müsstest du an viel mehr existierende Stellen ran, wenn du eine Klasse entfernst oder hinzufügst. Entfernst du beim Mediator eine der Klassen, löschst du halt einfach nur den entsprechenden Mediator.müsste man immer gleich auch das ConcreteMediator-Modul anpassen, obwohl man eigentlich nur das Modul Plant hinzufügen möchte.
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 |
creature ICreature ICreatureCondition animal IAnimal IsBreeding human IHuman HasCar |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
creature ICreature ICreatureCondition animal IAnimal IsBreeding human IHuman HasCar plant* IPlant* IsTree* |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
creature ICreature ICreatureCondition IMediator animal IAnimal IsBreeding human IHuman HasCar concretemediator MyMediator |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
creature ICreature ICreatureCondition IMediator animal IAnimal IsBreeding human IHuman HasCar plant* IPlant* IsTree* concretemediator MyMediator* <----- |
Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von »Chromanoid« (05.02.2017, 13:39)
Werbeanzeige