31
The Object-Oriented Design of the Expression Tree Processing App Douglas C. Schmidt

The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

The Object-Oriented Design

of the Expression Tree

Processing App

Douglas C. Schmidt

Page 2: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

Learning Objectives in This Lesson• Understand the OO design of the expression tree processing app.

en.wikipedia.org/wiki/Unified_Modeling_Language has more on OOD notations.

Expression_Tree

Composite_BinaryNode

Composite_NegateNode

Composite

Add_Node

Composite

Multiply_Node

Composite

Divide_Node

Composite

Subtract_Node

Leaf_NodeComposite

Unary_Node

Component_Node

Page 3: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

Lesson IntroductionDouglas C. Schmidt

Page 4: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

Lesson Introduction• Object-oriented design (OOD) is a method of planning a system of interacting

objects to solve software problem(s).

en.wikipedia.org/wiki/Object-oriented_design has more information on OO design.

Object-OrientedDesign

Polymorphism

Abstraction

Encapsulation

Extensibility

Page 5: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Object-oriented design (OOD) is a method of planning a system of interactingobjects to solve software problem(s).

• OOD employs “hierarchical data abstraction.”

• Components are designed based on stable class & object roles & relationships

• Rather than functions corresponding to actions

C++ Iterator

Pre_OrderIterator

In_OrderIterator

Post_OrderIterator

Level_OrderIterator

en.wikipedia.org/wiki/Liskov_substitution_principle has more information.

Lesson Introduction

Page 6: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Object-oriented design (OOD) is a method of planning a system of interactingobjects to solve software problem(s).

• OOD employs “hierarchical data abstraction.”

• It also associates actions with specific objects and/or classes of objects.

• Emphasize high cohesion & low coupling

Input_Handler

handle_input()

prompt_user()

receive_input()

make_command()

execute_command()

Verbose_Mode

Input_Handler

prompt_user()

make_command()

Succinct_Mode

Input_Handler

prompt_user()

make_command()

en.wikipedia.org/wiki/Low-Coupling_/_High-Cohesion_pattern has more information.

Lesson Introduction

Page 7: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

www.dre.vanderbilt.edu/~schmidt/patterns-frameworks.html has more information.

Post_OrderIterator

Bridge Expression_Tree

C++ Stack

In_OrderIterator

Level_OrderIterator

C++ Queue

<< create >>

C++ Iterator

Strategy

Iterator

Pre_OrderIterator

Component_Node

CompositeUnary_Node

Leaf_Node

CompositeBinary_Node …

Composite

Lesson Introduction• Object-oriented design (OOD) is a method of planning a system of interacting

objects to solve software problem(s).

• OOD employs “hierarchical data abstraction.”

• It also associates actions with specific objects and/or classes of objects.

• Well-designed OO programs group classes & objects viapatterns & combine them to form frameworks.

Page 8: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

OO Design of Expression Tree Processing App

Douglas C. Schmidt

Page 9: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Create an OO design based on modeling classes & objects in “expression tree” domain.

OO Design of Expression Tree Processing App

×

+−

5 3 4

Page 10: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Conduct scope, commonality, & variability analysis to determine stable APIs & variable extension points.

OO Design of Expression Tree Processing App

See www.dre.Vanderbilt.edu/~schmidt/PDF/Commonality_Variability.pdf

×

+−

5 3 4

Page 11: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Conduct scope, commonality, & variability analysis to determine stable APIs & variable extension points.

• Model a tree as a collection of nodes.

OO Design of Expression Tree Processing App

×

+−

5 3 4

Binary Nodes

Unary Node

Leaf Nodes

(Integers)

Page 12: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Conduct scope, commonality, & variability analysis to determine stable APIs & variable extension points.

• Model a tree as a collection of nodes.

OO Design of Expression Tree Processing App

Note the different types of nodes in a tree.

×

+−

5 3 4

Binary Nodes

Unary Node

Leaf Nodes

(Integers)

Page 13: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Conduct scope, commonality, & variability analysis to determine stable APIs & variable extension points.

• Model a tree as a collection of nodes.

• Represent nodes as class hierarchy, capturing properties of each node.

• e.g., the “arities” (binary & unary nodes)

Expression_Tree

Composite_BinaryNode

Composite_NegateNode

Composite

Add_Node

Composite

Multiply_Node

Composite

Divide_Node

Composite

Subtract_Node

Leaf_NodeComposite

Unary_Node

Component_Node

OO Design of Expression Tree Processing App

See en.wikipedia.org/wiki/Arity

Page 14: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

en.wikipedia.org/wiki/Design_Patterns has information on the “Gang of Four” (GoF) book.

Post_OrderIterator

Bridge Expression_Tree Component_Node

C++ Stack

In_OrderIterator

Level_OrderIterator

C++ Queue

<< create >>

C++ Iterator

Visitor

Visitor

PrintVisitor

<< accept >>

Evaluation_Visitory

Strategy

Iterator

Pre_OrderIterator

CompositeUnary_Node

Leaf_Node

CompositeBinary_Node …

Composite

OO Design of Expression Tree Processing App

Page 15: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

See www.dre.vanderbilt.edu/~schmidt/frameworks.html

OO Design of Expression Tree Processing App

Page 16: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

• Frameworks exhibit three characteristics that differentiate them from other forms of systematic reuse. Application-Specific Functionality

OO Design of Expression Tree Processing App

www.dre.vanderbilt.edu/~schmidt/reuse-lessons.html has info on systematic reuse.

Page 17: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

• Frameworks exhibit three characteristics that differentiate them from other forms of systematic reuse.

1. Inversion of control (IoC)

See en.wikipedia.org/wiki/Inversion_of_control

Application-Specific Functionality

OO Design of Expression Tree Processing App

Page 18: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

• Frameworks exhibit three characteristics that differentiate them from other forms of systematic reuse.

1. Inversion of control (IoC)

• The framework controls the main execution thread

Application-Specific Functionality

OO Design of Expression Tree Processing App

Page 19: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

• Frameworks exhibit three characteristics that differentiate them from other forms of systematic reuse.

1. Inversion of control (IoC)

• The framework controls the main execution thread

• Decides how/when to run app code via callbacks

Application-Specific Functionality

OO Design of Expression Tree Processing App

See en.wikipedia.org/wiki/Callback_(computer_programming)

Page 20: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

• Frameworks exhibit three characteristics that differentiate them from other forms of systematic reuse.

1. Inversion of control (IoC)

• The framework controls the main execution thread

• Decides how/when to run app code via callbacks

• e.g., an Android looper dispatches a handler, which then dispatches a runnable

Application-Specific Functionality

OO Design of Expression Tree Processing App

See blog.mindorks.com/android-core-looper-handler-and-handlerthread-bd54d69fe91a

Page 21: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

• Frameworks exhibit three characteristics that differentiate them from other forms of systematic reuse.

1. Inversion of control (IoC)

• The framework controls the main execution thread

• Decides how/when to run app code via callbacks

• IoC is often called “The Hollywood Principle”

Application-Specific Functionality

OO Design of Expression Tree Processing App

See www.dre.vanderbilt.edu/~schmidt/Coursera/articles/hollywood-principle.txt

Page 22: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

• Frameworks exhibit three characteristics that differentiate them from other forms of systematic reuse.

1. Inversion of control (IoC)

2. Domain-specific structure & functionality

See en.wikipedia.org/wiki/Domain-driven_design

Mobile AppsSocial

Media

StockTrading

GUIs

NetworkingDatabases

Application-Specific Functionality

OO Design of Expression Tree Processing App

Page 23: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

• Frameworks exhibit three characteristics that differentiate them from other forms of systematic reuse.

1. Inversion of control (IoC)

2. Domain-specific structure & functionality

• e.g., capabilities that can be reused in 1+ domain(s) Mobile

AppsSocial Media

StockTrading

GUIs

NetworkingDatabases

Application-Specific Functionality

OO Design of Expression Tree Processing App

Infrastructure

domains

Application

domains

Page 24: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

• Frameworks exhibit three characteristics that differentiate them from other forms of systematic reuse.

1. Inversion of control (IoC)

2. Domain-specific structure & functionality

• e.g., capabilities that can be reused in 1+ domain(s) Mobile

AppsSocial Media

StockTrading

GUIs

NetworkingDatabases

OO Design of Expression Tree Processing App

Application-specific functionality can systematically reuse framework components.

Application-Specific Functionality

Page 25: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

• Frameworks exhibit three characteristics that differentiate them from other forms of systematic reuse.

1. Inversion of control (IoC)

2. Domain-specific structure & functionality

3. Semi-complete applicationsMobile AppsSocial

Media

StockTrading

GUIs

NetworkingDatabases

Application-Specific Functionality

OO Design of Expression Tree Processing App

Page 26: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

• Frameworks exhibit three characteristics that differentiate them from other forms of systematic reuse.

1. Inversion of control (IoC)

2. Domain-specific structure & functionality

3. Semi-complete applications

• Hook methods plug app logic into the framework

Mobile AppsSocial

Media

StockTrading

GUIs

NetworkingDatabases

Application-Specific Functionality

OO Design of Expression Tree Processing App

See codebetter.com/davelaribee/2008/06/16/hook-methods

Page 27: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Apply “Gang of Four” (GoF) patterns to guide the development of a framework of extensible classes.

• A framework is an integrated set of software components that collaborate to provide a reusable architecture for a family of related applications.

• Frameworks exhibit three characteristics that differentiate them from other forms of systematic reuse.

1. Inversion of control (IoC)

2. Domain-specific structure & functionality

3. Semi-complete applications

• Hook methods plug app logic into the framework

• Mediate interactions among common abstract & variantconcrete classes/interfaces

Mobile AppsSocial

Media

StockTrading

GUIs

NetworkingDatabases

Application-Specific Functionality

OO Design of Expression Tree Processing App

e.g., Java Runnable is an abstract interface providing basis for concrete variants.

Page 28: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Integrate pattern-oriented language & library features with frameworks.

• Both an app-specific framework…

Expression_Tree tree = ...;

Visitor print_visitor = ...;

for (auto iter = tree.begin(order);

iter != tree.end(order);

++iter)

(*iter).accept(print_visitor);

Factory Method, Bridge, Composite, Iterator, Strategy, & Visitor patterns

OO Design of Expression Tree Processing App

This app-specific framework exhibits high pattern density!

Page 29: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Integrate pattern-oriented language & library features with frameworks.

• Both an app-specific framework… & off-the-shelf frameworks…

See developer.android.com & en.wikipedia.org/wiki/Standard_Template_Library

OO Design of Expression Tree Processing App

Page 30: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction

• Complexity resides in (stable) structure & APIs, rather than (variable) algorithms.

Post_OrderIterator

Bridge Expression_Tree Component_Node

C++ Stack

In_OrderIterator

Level_OrderIterator

C++ Queue

<< create >>

C++ Iterator

Visitor

Visitor

PrintVisitor

<< accept >>

Evaluation_Visitory

Strategy

Iterator

Pre_OrderIterator

CompositeUnary_Node

Leaf_Node

CompositeBinary_Node …

Composite

OO Design of Expression Tree Processing App

See www.drdobbs.com/windows/software-complexity-bringing-order-to-ch/199901062

Page 31: The Object-Oriented Design of the Expression Tree ...schmidt/cs251/2020-PDFs/expression-tree … · Composite Unary_Node Leaf_Node Composite Binary_Node … Composite Lesson Introduction