Fundamental_Design_Patterns_6

Fundamental_Design_Patterns_6 - Design Patterns Fundamental...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Fundamental Design Patterns Design Patterns • Delegation • Interface • Abstract Superclass • Interface and Abstract Class • Immutable • Marker Interface • Proxy Delegation • Delegation is a more general way for extending a class’s behavior that involves a class calling another class’s methods rather than inheriting them. • Inheritance is useful for capturing “ is-a- kind-of ” relationships because of the static nature. • Delegation is useful for capturing “ is-a- role-played-by ” relationships. The Potential Difficulties of Inheritance Person CrewMemeber TicketAgent Passenger A Solution Using Inheritance Person CrewMemeber TicketAgent Passenger CrewMemeberAndPassenger TicketAgentAndPassenger A Solution Using Delegation Person CrewMemeber TicketAgent Passenger 0..1 0..1 0..1 1 1 1 Uses Uses Uses
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Common Form of Delegation An Example of Delegation class FlightSegment{ ….. LuggageCompartment luggage; ….. void checkLuggage(Luggage piece) throws LuggageException{ luggage.checkLuggage(piece); } } Code Example class LuggageCompartment { // The pieces of luggage in this LuggageCompartment private Luggage[] luggage = new Luggage[10]; void checkLuggage() throws LuggageException{ for(int i = 0; i<luggage.length; i++){ luggage[i].check(); } } } Code Example class LuggageCompartment { // The pieces of luggage in this LuggageCompartment
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

Fundamental_Design_Patterns_6 - Design Patterns Fundamental...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online