60 likes | 216 Views
Linear Recursive Structures(LRS). Based on paper by D.Nyguen. Recursive Definition. List is an Empty list Or a NonEmptyList with a head Object and a tail which is a List.
E N D
Linear Recursive Structures(LRS) Based on paper by D.Nyguen B.Ramamurthy
Recursive Definition • List is an Empty list • Or a NonEmptyList with a head Object and a tail which is a List. • Empty list is typically represented by “null” giving rise to the famous NullPointerException. In this paper, EmptyList is represented by a NullObject (Pattern). B.Ramamurthy
head head tail tail Object Object List List NonEmptyList List: head tail head tail List: Object Object List (can be empty) head tail head tail List: Object Object B.Ramamurthy
State-pattern based approach Add an Object Remove An object Add an Object NonEmpty Empty Remove an Object singleton Represents all Possible nonEmpty states B.Ramamurthy
List operations (add, remove) • List operations depend on the state of the list. • Define a class hierarchy for state and polymorphically delegate the operations to the different types of states. • Using State pattern: an abstract class specifies operations that are state dependent. A concrete class is defined for each discrete/functionally distinguishable state. B.Ramamurthy