}
This kind of method disobeyed the ISP (Interface Segregation Principle) of principle of a core in object-oriented design, originally Door concept itself in the definition in Door the behavior method of some behavior methods and " of warning apparatus of another concept " is mixed in together. A problem that such causing is those rely on Door merely because,the module of this concept meets " of " warning apparatus the change of this concept (for instance: The parameter of modification Alarm method) and change, conversely still.
Solution 2:
Since Open, Close and Alarm belong to two different ideas, should define them in the abstraction that represents these two notions respectively according to ISP principle kind in. Definition means has: These two concepts use definition of Abstract Class means; Two concepts use Interface means definition; A concept uses definition of Abstract Class means, another concept uses Interface means definition.
Apparent, do not support as a result of Java language multiple and successive, it is unworkable that so two concepts use Abstract Class means to define. Two kinds of means are feasible from the back, but mirror instead however to their choice reasonable. We come one by one analysis, specification.
If two concepts use Interface means to define, so give two issues with respect to report: 1, we may not understand clear problem domain, is AlarmDoor in is the concept Door or warning apparatus after all substantially? 2, if we do not have a problem to the understanding of problem domain, for instance: We discover through the analysis to problem domain AlarmDoor is mixed substantially in the concept Door is consistent, our design intent can not announce correctly when we are coming true so, because be on the definition of these two concepts (all use Interface means to define) report does not give afore-mentioned meanings.
If we the understanding to problem domain is: AlarmDoor is in the concept is Door substantially, at the same time it has have the function that call the police. How should we come does the report that will design, implementation come to make clear give our meaning? Had said in front, abstract Class expresses a kind of successive concern in Java language, and successive relation is "is-a" relation substantially. So to Door this concept, we should use Abstarct Class means to define. Additional, alarmDoor has warning function again, explain it can be finished again call the police the behavior of the definition in the concept, call the police so the concept can be defined through Interface means. Following place show:
Abstract Class Door{Abstract Void Open();Abstract Void Close() ;
}Interface Alarm{Void Alarm();
}Class Alarm Door Extends Door Implements Alarm{Void Open(){…}Void Close(){…}Void Alarm(){…}
}
The report that means of this kind of implementation basically can make clear gives us the understanding to problem domain, announce correctly our design intent. Abstract Class is actually denotive it is "is-a" relation, interface is denotive it is "like-a" relation, everybody can serve as a basis when the choice, of course this is to build what going up to the understanding of problem domain, for instance: If we think AlarmDoor is in,the concept is warning apparatus substantially, have the function of Door again at the same time, means of so afore-mentioned definition is about conversely.
Previous 1 2 3 45 Next