SlideShare a Scribd company logo
Implementation of Composite
Design Pattern in Android View
and Widgets
by
Somenath Mukhopadhyay
som.mukhopadhyay@gmail.com
The Intent of the composite design pattern is stated in the GoF book as
"Compose Objects into tree structures to represent part-whole hierarchies.
Composite lets clients treat individual objects and compositions of objects
uniformly".
To explain it in a simpler fashion, let me give the same example as given
in the GoF book. Suppose there is an object called Picture, a graphics
object. This picture may consist of other pictures recursively as well as
primitive objects like line, rectangle objects etc. All of these part objects
which make the whole picture conform to the same Graphic interface.
Hence to the client, a part object appears same as the whole picture
consisted of other part objects. To draw a whole object, the client simply
traverses through the whole picture and draws different parts.
The class diagram of the composite design pattern will look like the
following:
The following participants take part in this design pattern:
Component 1. it declares the interface for the objects ( part as well as whole)
2. helps in managing the objects (adding, removing)
Leaf 1. represents leaf objects which don't have any children
2. these are the primitive objects
Composite 1. defines behavior for components having children
2. stores the children
Client 1. takes help of the Component interface to manipulate different objects
Its all about the theoretical side of the Composite Design Pattern. Now let
us try to dissect the Android View and the Widget folders (which are
available at basecorejavaandroid) to see how this design pattern has
been implemented there.
In Android, the View class works as the Component class. However, the
child management part (add component, remove component) has been
moved to the Composite class which is the ViewGroup class. Actually the
Add and Remove of a component has been declared in an interface called
ViewManager and the ViewGroup implements that interface. Also the
interface for a Composite object is declared as ViewParent interface and
the ViewGroup (the Composite object) implements that as well.
The leaf classes like Button, ImageView etc are deduced either by
directly subclassing the View (Component) or from the subclasses of the
View (for example, the Button class is derived from TextView class which
in turn is directly derived fron the View class). The Composite Class
(ViewGroup) is deduced by directly subclassing the View and by
implementing the two interfaces namely ViewParent (which defines the
interface of a composite object) and ViewManager (which defines the
interface from adding and removing components).
As expected the getParent function which is needed to get the Parent of a
component is put in the View class (the Component).
The Composite object (ViewGroup) has an array to hold its children.
The simplified version of the Android view and widget’s class diagram is as
follows-

Now let us consider the class diagram as presented in the beginning of
this discussion. There is a function called Operation. In Android
implementation, the onDraw function in the Component (View) plays this
role. The DispatchDraw function (which is called when the Component is
to be drawn) in the composite (ViewGroup) object actually traverses
through the list of the objects and calls draw on each of the child object.
This way we can say that Android View and Widgets are some sort of
implementation of the Composite Design Pattern.

More Related Content

DOC
Hats tutorial custom widget
PPTX
Android MapView and MapActivity
PDF
Day 8: Dealing with Lists and ListViews
PPTX
Activity & Shared Preference
PPT
View groups containers
PPTX
Android share preferences
PPTX
Android Tutorials : Basic widgets
PDF
Power Apps and Office365 Groups
Hats tutorial custom widget
Android MapView and MapActivity
Day 8: Dealing with Lists and ListViews
Activity & Shared Preference
View groups containers
Android share preferences
Android Tutorials : Basic widgets
Power Apps and Office365 Groups

Similar to Implementation of composite design pattern in android view and widgets (20)

DOCX
Android views and layouts-chapter4
PDF
Android UI Fundamentals part 1
PDF
04 user interfaces
PPTX
Unit 2 part for information technology1 4.pptx
PDF
Mobile Application Development -Lecture 07 & 08.pdf
DOCX
How to create ui using droid draw
PDF
The battle between the states (all about flutter stateless & stateful widgets...
PPTX
Android apps development
PPTX
Building a simple user interface lesson2
PPT
Composite pattern
PPTX
Android Training (Android UI)
PDF
Fragments In Android
PDF
Android Application Development - Level 1
PPTX
DOCX
Android xml-based layouts-chapter5
PPTX
Lecture 2 Styling and Layout in React Native.pptx
PPT
Visualizing STEP Files
PPTX
UNIT5newpart2pptx__2024_11_13_09_52_11 (1).pptx
PPT
Introduction to Rational Rose
Android views and layouts-chapter4
Android UI Fundamentals part 1
04 user interfaces
Unit 2 part for information technology1 4.pptx
Mobile Application Development -Lecture 07 & 08.pdf
How to create ui using droid draw
The battle between the states (all about flutter stateless & stateful widgets...
Android apps development
Building a simple user interface lesson2
Composite pattern
Android Training (Android UI)
Fragments In Android
Android Application Development - Level 1
Android xml-based layouts-chapter5
Lecture 2 Styling and Layout in React Native.pptx
Visualizing STEP Files
UNIT5newpart2pptx__2024_11_13_09_52_11 (1).pptx
Introduction to Rational Rose
Ad

More from Somenath Mukhopadhyay (20)

PDF
Significance of private inheritance in C++...
PDF
Arranging the words of a text lexicographically trie
PDF
Generic asynchronous HTTP utility for android
PDF
Copy on write
PDF
Java concurrency model - The Future Task
PDF
Memory layout in C++ vis a-vis polymorphism and padding bits
PDF
Developing an Android REST client to determine POI using asynctask and integr...
PDF
Observer pattern
PDF
Uml training
PDF
How to create your own background for google docs
PDF
The Designing of a Software System from scratch with the help of OOAD & UML -...
PDF
Structural Relationship between Content Resolver and Content Provider of Andr...
PDF
Flow of events during Media Player creation in Android
PDF
Implementation of a state machine for a longrunning background task in androi...
PDF
Tackling circular dependency in Java
PDF
Exception Handling in the C++ Constructor
PDF
Active object of Symbian in the lights of client server architecture
PDF
Android services internals
PDF
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
PDF
Composite Pattern
Significance of private inheritance in C++...
Arranging the words of a text lexicographically trie
Generic asynchronous HTTP utility for android
Copy on write
Java concurrency model - The Future Task
Memory layout in C++ vis a-vis polymorphism and padding bits
Developing an Android REST client to determine POI using asynctask and integr...
Observer pattern
Uml training
How to create your own background for google docs
The Designing of a Software System from scratch with the help of OOAD & UML -...
Structural Relationship between Content Resolver and Content Provider of Andr...
Flow of events during Media Player creation in Android
Implementation of a state machine for a longrunning background task in androi...
Tackling circular dependency in Java
Exception Handling in the C++ Constructor
Active object of Symbian in the lights of client server architecture
Android services internals
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
Composite Pattern
Ad

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Approach and Philosophy of On baking technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation theory and applications.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Empathic Computing: Creating Shared Understanding
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Network Security Unit 5.pdf for BCA BBA.
Approach and Philosophy of On baking technology
Programs and apps: productivity, graphics, security and other tools
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Understanding_Digital_Forensics_Presentation.pptx
MYSQL Presentation for SQL database connectivity
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The AUB Centre for AI in Media Proposal.docx
Building Integrated photovoltaic BIPV_UPV.pdf
Electronic commerce courselecture one. Pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation theory and applications.pdf
Chapter 3 Spatial Domain Image Processing.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Diabetes mellitus diagnosis method based random forest with bat algorithm
Dropbox Q2 2025 Financial Results & Investor Presentation
Empathic Computing: Creating Shared Understanding
The Rise and Fall of 3GPP – Time for a Sabbatical?
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

Implementation of composite design pattern in android view and widgets

  • 1. Implementation of Composite Design Pattern in Android View and Widgets by Somenath Mukhopadhyay som.mukhopadhyay@gmail.com The Intent of the composite design pattern is stated in the GoF book as "Compose Objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly". To explain it in a simpler fashion, let me give the same example as given in the GoF book. Suppose there is an object called Picture, a graphics object. This picture may consist of other pictures recursively as well as primitive objects like line, rectangle objects etc. All of these part objects which make the whole picture conform to the same Graphic interface. Hence to the client, a part object appears same as the whole picture consisted of other part objects. To draw a whole object, the client simply traverses through the whole picture and draws different parts. The class diagram of the composite design pattern will look like the following:
  • 2. The following participants take part in this design pattern: Component 1. it declares the interface for the objects ( part as well as whole) 2. helps in managing the objects (adding, removing) Leaf 1. represents leaf objects which don't have any children 2. these are the primitive objects Composite 1. defines behavior for components having children 2. stores the children Client 1. takes help of the Component interface to manipulate different objects Its all about the theoretical side of the Composite Design Pattern. Now let us try to dissect the Android View and the Widget folders (which are available at basecorejavaandroid) to see how this design pattern has been implemented there. In Android, the View class works as the Component class. However, the child management part (add component, remove component) has been
  • 3. moved to the Composite class which is the ViewGroup class. Actually the Add and Remove of a component has been declared in an interface called ViewManager and the ViewGroup implements that interface. Also the interface for a Composite object is declared as ViewParent interface and the ViewGroup (the Composite object) implements that as well. The leaf classes like Button, ImageView etc are deduced either by directly subclassing the View (Component) or from the subclasses of the View (for example, the Button class is derived from TextView class which in turn is directly derived fron the View class). The Composite Class (ViewGroup) is deduced by directly subclassing the View and by implementing the two interfaces namely ViewParent (which defines the interface of a composite object) and ViewManager (which defines the interface from adding and removing components). As expected the getParent function which is needed to get the Parent of a component is put in the View class (the Component). The Composite object (ViewGroup) has an array to hold its children. The simplified version of the Android view and widget’s class diagram is as follows- Now let us consider the class diagram as presented in the beginning of this discussion. There is a function called Operation. In Android implementation, the onDraw function in the Component (View) plays this role. The DispatchDraw function (which is called when the Component is to be drawn) in the composite (ViewGroup) object actually traverses through the list of the objects and calls draw on each of the child object.
  • 4. This way we can say that Android View and Widgets are some sort of implementation of the Composite Design Pattern.