SlideShare a Scribd company logo
Andy Bulka Technical Director Austhink Software www.austhink.com March 2008 Refactoring to patterns…
Simple Composite pattern Class with a “children” collection Children are of type Node (i.e. you point to yourself)
Classic Composite pattern (GOF) Distinguish between Composites and Leaves Benefit of “classic” is that you can distinguish  leafs  and override display() or doubleClick() or whatever for leafs.
The Problem Code We have two classes that loop through their children –  duplicate code The children attribute is named differently in both classes –  inconsistent Both classes are “composites”
The Solution Create a common base class which has the generic “composite” looping behaviour That’s where the name of the refactoring comes from “Extract Composite”
Original Problem code class  Node (object): def __init__(self, name): self.name = name def  toPlainTextString (self): return self.name class  FormTag (Node): def __init__(self): self.allNodesVector = [] def  toPlainTextString (self): result = "" for node in self.allNodesVector: result += node.toPlainTextString() return result class  LinkTag (Node): def __init__(self): self.linkData = [] def  toPlainTextString (self): result = "" for node in self.linkData: result += node.toPlainTextString() return result f = FormTag() f.allNodesVector.append(Node("a")) f.allNodesVector.append(Node("b")) f.allNodesVector.append(Node("c")) l = LinkTag() l.linkData += [Node("x"), Node("y"), Node("z")] print f.toPlainTextString() print l.toPlainTextString() abc xyz output
Step 1 # Create a Composite Class - compile class Composite(Node): pass
Step 2 Make each child container (a class in the hierarchy that contains duplicate child-handling code) a subclass of your composite – compile class Composite(Node): pass class FormTag(Composite): … . class LinkTag(Composite): … .
Step 3 For each method with duplicated looping code 1. move & rename the child reference field UP to the composite using "Pull Up Field" 2. Move the method UP to the composite using "Pull Up Method" 3. Pull up any relevant constructor code too.
Everything moved to Composite class  Composite (Node): def __init__(self): self.children = [] def toPlainTextString(self): result = "" for node in self.children: result += node.toPlainTextString() return result class FormTag(Composite): pass class LinkTag(Composite): pass
Step 4 Check interfaces so that  client code  using the old composites still works. f = FormTag() f. children .append(Node("a")) f. children .append(Node("b")) f. children .append(Node("c")) l = LinkTag() l. children  += [Node("x"), Node("y"), Node("z")] print f.toPlainTextString() print l.toPlainTextString() f = FormTag() f. allNodesVector .append(Node("a")) f. allNodesVector .append(Node("b")) f. allNodesVector .append(Node("c")) l = LinkTag() l. linkData  += [Node("x"), Node("y"), Node("z")] print f.toPlainTextString() print l.toPlainTextString()
New UML We have inserted a new class called  Composite  above the LinkTag and FormTag, which does the common looping work.
Before and After

More Related Content

What's hot (20)

PPT
Abstract class
Hoang Nguyen
 
PPTX
Access Modifiers in C# ,Inheritance and Encapsulation
Abid Kohistani
 
PPTX
Java Inheritance
VINOTH R
 
PPTX
Overloading and overriding in vb.net
suraj pandey
 
PPTX
Friend function & friend class
Abhishek Wadhwa
 
PPTX
Class Diagram | OOP and Design Patterns by Oum Saokosal
OUM SAOKOSAL
 
PPTX
Inheritance in java
yash jain
 
PPT
Java: Inheritance
Tareq Hasan
 
PPTX
Inheritance in Java
Tamanna Akter
 
PDF
Inheritance used in java
TharuniDiddekunta
 
PPTX
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
PPTX
OOPS Characteristics (With Examples in PHP)
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Lecture 9 access modifiers and packages
manish kumar
 
PPTX
Exception Handling in C#
Abid Kohistani
 
PPS
Inheritance chepter 7
kamal kotecha
 
PPTX
Dynamic method dispatch
yugandhar vadlamudi
 
PDF
Java unit2
Abhishek Khune
 
PPSX
Seminar on java
shathika
 
PPTX
Inheritance
Sapna Sharma
 
PPTX
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
Abstract class
Hoang Nguyen
 
Access Modifiers in C# ,Inheritance and Encapsulation
Abid Kohistani
 
Java Inheritance
VINOTH R
 
Overloading and overriding in vb.net
suraj pandey
 
Friend function & friend class
Abhishek Wadhwa
 
Class Diagram | OOP and Design Patterns by Oum Saokosal
OUM SAOKOSAL
 
Inheritance in java
yash jain
 
Java: Inheritance
Tareq Hasan
 
Inheritance in Java
Tamanna Akter
 
Inheritance used in java
TharuniDiddekunta
 
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
OOPS Characteristics (With Examples in PHP)
baabtra.com - No. 1 supplier of quality freshers
 
Lecture 9 access modifiers and packages
manish kumar
 
Exception Handling in C#
Abid Kohistani
 
Inheritance chepter 7
kamal kotecha
 
Dynamic method dispatch
yugandhar vadlamudi
 
Java unit2
Abhishek Khune
 
Seminar on java
shathika
 
Inheritance
Sapna Sharma
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 

Similar to Extract Composite Talk Andy (11)

PPT
Composite Pattern
melbournepatterns
 
PDF
Python 2.5 reference card (2009)
gekiaruj
 
ODP
Patterns in Python
dn
 
PDF
CSMR06a.ppt
Ptidej Team
 
PPT
Composite pattern
Shakil Ahmed
 
PPTX
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
mru761077
 
PPT
Functional techniques in Ruby
erockendude
 
PPT
Functional techniques in Ruby
erockendude
 
PDF
Dont Drive on the Railroad Tracks
Eugene Wallingford
 
PPTX
Modularization of Legacy Features by Relocation and Reconceptualization: How ...
Andrzej Olszak
 
PDF
A limited guide to intermediate and advanced Ruby
Vysakh Sreenivasan
 
Composite Pattern
melbournepatterns
 
Python 2.5 reference card (2009)
gekiaruj
 
Patterns in Python
dn
 
CSMR06a.ppt
Ptidej Team
 
Composite pattern
Shakil Ahmed
 
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
mru761077
 
Functional techniques in Ruby
erockendude
 
Functional techniques in Ruby
erockendude
 
Dont Drive on the Railroad Tracks
Eugene Wallingford
 
Modularization of Legacy Features by Relocation and Reconceptualization: How ...
Andrzej Olszak
 
A limited guide to intermediate and advanced Ruby
Vysakh Sreenivasan
 
Ad

More from melbournepatterns (20)

PDF
An Introduction to
melbournepatterns
 
PPT
State Pattern from GoF
melbournepatterns
 
PDF
Iterator Pattern
melbournepatterns
 
PDF
Iterator
melbournepatterns
 
PPT
Concurrency Patterns
melbournepatterns
 
PPTX
Continuous Integration, Fast Builds and Flot
melbournepatterns
 
PPTX
Command Pattern
melbournepatterns
 
PPTX
Code Contracts API In .Net
melbournepatterns
 
PPTX
LINQ/PLINQ
melbournepatterns
 
PDF
Gpu Cuda
melbournepatterns
 
PPTX
Facade Pattern
melbournepatterns
 
PPT
Phani Kumar - Decorator Pattern
melbournepatterns
 
PPT
Composite Pattern
melbournepatterns
 
PPT
Adapter Design Pattern
melbournepatterns
 
PPT
Prototype Design Pattern
melbournepatterns
 
PPT
Factory Method Design Pattern
melbournepatterns
 
PPT
Abstract Factory Design Pattern
melbournepatterns
 
PPT
A Little Lisp
melbournepatterns
 
PPT
State Pattern in Flex
melbournepatterns
 
PPT
Active Object
melbournepatterns
 
An Introduction to
melbournepatterns
 
State Pattern from GoF
melbournepatterns
 
Iterator Pattern
melbournepatterns
 
Concurrency Patterns
melbournepatterns
 
Continuous Integration, Fast Builds and Flot
melbournepatterns
 
Command Pattern
melbournepatterns
 
Code Contracts API In .Net
melbournepatterns
 
LINQ/PLINQ
melbournepatterns
 
Facade Pattern
melbournepatterns
 
Phani Kumar - Decorator Pattern
melbournepatterns
 
Composite Pattern
melbournepatterns
 
Adapter Design Pattern
melbournepatterns
 
Prototype Design Pattern
melbournepatterns
 
Factory Method Design Pattern
melbournepatterns
 
Abstract Factory Design Pattern
melbournepatterns
 
A Little Lisp
melbournepatterns
 
State Pattern in Flex
melbournepatterns
 
Active Object
melbournepatterns
 
Ad

Recently uploaded (20)

PPTX
How Essar Transforms the Planet while Investing in People Over Profit
essarcase
 
PDF
On-Grid Solar System for Home A Complete Guide
Contendre Solar
 
PPTX
IP Leaks Can Derail Years Of Innovation In Seconds
Home
 
PDF
Chembond Chemicals Limited Presentation 2025
Chembond Chemicals Limited
 
PDF
Dr. Enrique Segura Ense Group - A Philanthropist And Entrepreneur
Dr. Enrique Segura Ense Group
 
PPTX
Unlocking the Power of Process Mining: Driving Efficiency Through Data
RUPAL AGARWAL
 
PDF
Easypromo AI Review: Revolutionizing Digital Promotions with Artificial Intel...
Larry888358
 
PPTX
Drive Operational Excellence with Proven Continuous Improvement Strategies
Group50 Consulting
 
PDF
LEWIONICS SCO Company Profile UAE JULY 2025
Natalie Lewes
 
PDF
BCG's Guide to Cost and Growth 24pages file
Wipro Unza Vietnam Company Limited
 
PDF
Blind Spots in Business: Unearthing Hidden Challenges in Today's Organizations
Crimson Business Consulting
 
PPTX
Revolutionizing Shopping: Voice Commerce in Retail and eCommerce
RUPAL AGARWAL
 
PDF
547229304-CBS-2021businesscasebook2o.pdf
CngNguynngHng
 
PPTX
_IIML_Optimizing Energy Efficiency in Industrial Operations with GenAI_Team I...
rafinrowshan
 
PPTX
2025 July - ABM for B2B in Hubspot - Demand Gen HUG.pptx
mjenkins13
 
PPTX
Cruise API Provider | Amadeus Cruise API | Cruise Software
anusharajraj21
 
PDF
Steve Milne Equestrian - A Master Horse Trainer
Steve Milne Equestrian
 
PDF
Improving Urban Traffic Monitoring with Aerial Image Annotation Services
SunTec India
 
PDF
ETT OUTLET One Token Endless Possibilities PDF
ettoutllet
 
PDF
Boardsi - A Staunch Supporter Of Veterans
Boardsi
 
How Essar Transforms the Planet while Investing in People Over Profit
essarcase
 
On-Grid Solar System for Home A Complete Guide
Contendre Solar
 
IP Leaks Can Derail Years Of Innovation In Seconds
Home
 
Chembond Chemicals Limited Presentation 2025
Chembond Chemicals Limited
 
Dr. Enrique Segura Ense Group - A Philanthropist And Entrepreneur
Dr. Enrique Segura Ense Group
 
Unlocking the Power of Process Mining: Driving Efficiency Through Data
RUPAL AGARWAL
 
Easypromo AI Review: Revolutionizing Digital Promotions with Artificial Intel...
Larry888358
 
Drive Operational Excellence with Proven Continuous Improvement Strategies
Group50 Consulting
 
LEWIONICS SCO Company Profile UAE JULY 2025
Natalie Lewes
 
BCG's Guide to Cost and Growth 24pages file
Wipro Unza Vietnam Company Limited
 
Blind Spots in Business: Unearthing Hidden Challenges in Today's Organizations
Crimson Business Consulting
 
Revolutionizing Shopping: Voice Commerce in Retail and eCommerce
RUPAL AGARWAL
 
547229304-CBS-2021businesscasebook2o.pdf
CngNguynngHng
 
_IIML_Optimizing Energy Efficiency in Industrial Operations with GenAI_Team I...
rafinrowshan
 
2025 July - ABM for B2B in Hubspot - Demand Gen HUG.pptx
mjenkins13
 
Cruise API Provider | Amadeus Cruise API | Cruise Software
anusharajraj21
 
Steve Milne Equestrian - A Master Horse Trainer
Steve Milne Equestrian
 
Improving Urban Traffic Monitoring with Aerial Image Annotation Services
SunTec India
 
ETT OUTLET One Token Endless Possibilities PDF
ettoutllet
 
Boardsi - A Staunch Supporter Of Veterans
Boardsi
 

Extract Composite Talk Andy

  • 1. Andy Bulka Technical Director Austhink Software www.austhink.com March 2008 Refactoring to patterns…
  • 2. Simple Composite pattern Class with a “children” collection Children are of type Node (i.e. you point to yourself)
  • 3. Classic Composite pattern (GOF) Distinguish between Composites and Leaves Benefit of “classic” is that you can distinguish leafs and override display() or doubleClick() or whatever for leafs.
  • 4. The Problem Code We have two classes that loop through their children – duplicate code The children attribute is named differently in both classes – inconsistent Both classes are “composites”
  • 5. The Solution Create a common base class which has the generic “composite” looping behaviour That’s where the name of the refactoring comes from “Extract Composite”
  • 6. Original Problem code class Node (object): def __init__(self, name): self.name = name def toPlainTextString (self): return self.name class FormTag (Node): def __init__(self): self.allNodesVector = [] def toPlainTextString (self): result = "" for node in self.allNodesVector: result += node.toPlainTextString() return result class LinkTag (Node): def __init__(self): self.linkData = [] def toPlainTextString (self): result = "" for node in self.linkData: result += node.toPlainTextString() return result f = FormTag() f.allNodesVector.append(Node("a")) f.allNodesVector.append(Node("b")) f.allNodesVector.append(Node("c")) l = LinkTag() l.linkData += [Node("x"), Node("y"), Node("z")] print f.toPlainTextString() print l.toPlainTextString() abc xyz output
  • 7. Step 1 # Create a Composite Class - compile class Composite(Node): pass
  • 8. Step 2 Make each child container (a class in the hierarchy that contains duplicate child-handling code) a subclass of your composite – compile class Composite(Node): pass class FormTag(Composite): … . class LinkTag(Composite): … .
  • 9. Step 3 For each method with duplicated looping code 1. move & rename the child reference field UP to the composite using "Pull Up Field" 2. Move the method UP to the composite using "Pull Up Method" 3. Pull up any relevant constructor code too.
  • 10. Everything moved to Composite class Composite (Node): def __init__(self): self.children = [] def toPlainTextString(self): result = "" for node in self.children: result += node.toPlainTextString() return result class FormTag(Composite): pass class LinkTag(Composite): pass
  • 11. Step 4 Check interfaces so that client code using the old composites still works. f = FormTag() f. children .append(Node("a")) f. children .append(Node("b")) f. children .append(Node("c")) l = LinkTag() l. children += [Node("x"), Node("y"), Node("z")] print f.toPlainTextString() print l.toPlainTextString() f = FormTag() f. allNodesVector .append(Node("a")) f. allNodesVector .append(Node("b")) f. allNodesVector .append(Node("c")) l = LinkTag() l. linkData += [Node("x"), Node("y"), Node("z")] print f.toPlainTextString() print l.toPlainTextString()
  • 12. New UML We have inserted a new class called Composite above the LinkTag and FormTag, which does the common looping work.