COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

17
COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad

Transcript of COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Page 1: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

COP 3331 Object Oriented Analysis and Design

Chapter 7 – Design by Abastraction

Jean Muhammad

Page 2: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Overview

Introduction to Patterns Generic Components Abstract Coupling

Page 3: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Introduction to Patterns

Design reusable components Template method Strategy Factory Iterator

Page 4: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Introduction to Patterns

Design pattern was originally articulated by Christopher Alexander and his colleagues to describe architectural designs.

The architectural design of any building can be captured in one of 253 “patterns”.

Each pattern describes a problem that occurs over and over gain in an environment, and then describes the core solution.

Page 5: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Introduction to Patterns

Software design patterns are classified into three categories:– Creational Patterns– Structural Patterns– Behavioral Patterns

Page 6: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Introduction to Patterns

Describing design patternsPattern Name:

Category: creational, structural, or behavioral

Intent:

Also Known As:

Applicability:

Structure:

Participants:

Page 7: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Generic Components

Refactoring: Refactoring is the process of identifying recurring code and organizing/modifying the code such that it can be used more then once.

Page 8: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Generic Components

Refactoring consists of:– Identifying code segments in a program that

implement the same logic. – Capturing this logic in a generic component that is

defined once. – Restructuring the program so that every

occurence of the code segment is replaced with a reference to the generic component.

Page 9: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Generic Components

Refactoring by Inheritance

class Common{ void computeAll (. . . ){ computeStep1(); computeStep2(); computeStep3(); } }

class ComputationA class ComputationB extends Common{ extends Common { void method1(. . .){ void method2(. . .) { computeAll() computeAll()

Page 10: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Generic Components

Refactoring by Delegation To refactor the recurring code sequence in

the previous example using delegation, we introduce a helper class instead of inheritance.

Page 11: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Generic Components

class Helper{ void computeAll (. . . ){ computeStep1(); computeStep2(); computeStep3(); } }

class ComputationA class ComputationB void compute(. . .){ void compute(. . .) { helper.computeAll() helper.computeAll() } } Helper helper; Helper helper;

Page 12: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Template Method

Use of an abstract class that serves as a template for classes with shared functionality. – An abstract class contains behavior common to

all of its subclasses. – The common behavior is captured in non-abstract

methods.– Using abstract classes ensures that all

subclasses will inherit the same behavior.

Page 13: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Strategy Pattern

The strategy pattern can be considered as a variation of the Template method– The hood method and template method reside in

different classes. – The abstract methods declared in the Strategy

class are the hood methods.– The methods in the Context class that call the

hook methods are the template methods.

Page 14: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Strategy Pattern

Strategy Pattern should be used when:– Many related classes differ only in their behavior– Different variations of an algorithm are needed.– An algorithm uses data that clients should not

know about. – A class defines many behaviors, which appear as

multiple conditional statements in a method.

Page 15: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Abstract Coupling

The strategy pattern is an example of abstract coupling. A client accesss a service through an interface or an abstract class without knowing the actual concrete class that provided the service.

Page 16: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Abstract Coupling

Customer

Customer

Customer

Service Provider A

Service Provider B

Service Provider C

Example of Direct Coupling – No Abstraction

Page 17: COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.

Abstract Coupling

Service Provider A

Service Provider B

Service Provider C

CustomerStandardServiceProtocol

Abstract Coupling – Any customer calls the Standard ServiceProtocol and the correct interface is automatically used.