Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Design Defects and

Restructuring
LECTURE 01
SAT, JAN 27, 2018
Intent of this Course
Understand Design Defects
Understand Defect Rectification and Code Restructuring
Understand and Implement Design Patterns
Implement Model View X Patterns (X: Controller, Presenter, View Model)
Apply Code Refactoring
Understand Test Driven Development

2
Course Outline
Design Patterns
Refactoring
Data Modeling
Lambda Expressions
Test Driven Development

3
Course Organization
2 Lectures – Morning & Afternoon
◦ Class Timings for Morning Lecture
◦ 9:30 AM – 12:30 PM
◦ 1st Period: 9:30 AM – 11:00 AM
◦ Break: 11:00 AM – 11:15 AM
◦ 2nd Period: 11:15 AM – 12:30 PM
◦ Class Timings for Afternoon Lecture
◦ 2:00 PM – 5:00 PM
◦ 1st Period: 2:00 PM – 3:30 PM
◦ Break: 3:30 PM – 3:45 PM
◦ 2nd Period: 3:45 PM – 5:00 PM

4
Course Organization
2 Lectures – Morning & Afternoon Evaluations
◦ Class Timings for Morning Lecture ◦ Quiz – 10%
◦ 9:30 AM – 12:30 PM ◦ Assignment – 25%
◦ 1st Period: 9:30 AM – 11:00 AM
◦ Class Participation – 5%
◦ Break: 11:00 AM – 11:15 AM
◦ Midterm – 20%
◦ 2nd Period: 11:15 AM – 12:30 PM
◦ Class Timings for Afternoon Lecture ◦ Final – 40%
◦ 2:00 PM – 5:00 PM
◦ 1st Period: 2:00 PM – 3:30 PM
◦ Break: 3:30 PM – 3:45 PM
◦ 2nd Period: 3:45 PM – 5:00 PM

5
Design Patterns – GoF
There are 23 design patterns mentioned in the book
These patterns are grouped as
◦ Creational Patterns
◦ Structural Patterns
◦ Behavioral Patterns

6
Design Patterns

A design pattern is a general reusable


solution to a commonly occurring
problem in software design

7
Design Patterns

A design pattern is not a finished design


that can be transformed directly into
code
It is a description or template for how to solve a problem that can be
used in many different situations

8
Design Patterns

Not all software patterns are design


patterns
For instance, algorithms solve computational problems rather than
software design problems

9
Creational Patterns
Abstract Factory
Builder
Factory Method
Prototype
Singleton

10
Abstract Factory
Intent
◦ Provide an interface for creating families of related or dependent objects without specifying their
concrete classes

Applicability
◦ A system should be independent of how its products are created, composed, and represented
◦ A system should be configured with one of multiple families of products
◦ A family of related product objects is designed to be used together, and you need to enforce this
constraint
◦ You want to provide a class library of products, and you want to reveal just their interfaces, not their
implementations

11
Abstract Factory

12
Design Defects and
Restructuring
LECTURE 02
SAT, FEB 03, 2018
Builder
Intent
◦ Separate the construction of a complex object from its representation so that the same construction
process can create different representations

Applicability
◦ The algorithm for creating a complex object should be independent of the parts that make up the object
and how they are assembled
◦ The construction process must allow different representations for the object that is constructed

2
Builder

3
Design Defects and
Restructuring
LECTURE 03
SAT, FEB 10, 2018
Singleton
Intent
◦ Ensure a class only has one instance, and provide a global point of access to it

Applicability
◦ There must be exactly one instance of a class, and it must be accessible to clients from a well-known
access point
◦ When the sole instance should be extensible by sub-classing, and clients should be able to use an
extended instance without modifying their code

2
Singleton

3
Factory Method
Intent
◦ Define an interface for creating an object, but let subclasses decide which class to instantiate
◦ Factory Method lets a class defer instantiation to subclasses

Applicability
◦ A lass a ’t a ti ipate the lass of o je ts it ust reate
◦ A class wants its subclasses to specify the objects it creates
◦ Classes delegate responsibility to one of several helper subclasses, and you want to localize the
knowledge of which helper subclass is the delegate

4
Factory Method

5
Design Defects and
Restructuring
LECTURE 04
SAT, FEB 17, 2018
Prototype
Intent
◦ Specify the kinds of objects to create using a prototypical instance, and create new objects by copying
this prototype

Applicability
◦ When the classes to instantiate are specified at run-time, for example, by dynamic loading; or
◦ To avoid building a class hierarchy of factories that parallels the class hierarchy of products; or
◦ When instances of a class can have one of only a few different combinations of state
◦ It may be more convenient to install a corresponding number of prototypes and clone them rather than instantiating the class
manually, each time with the appropriate state

2
Prototype

3
Object Dependency
There are number of ways an object can be used
It depends on
◦ which class is creating an object
◦ when the object is being created, and
◦ where it is being created

A ge eral relatio ship uses ay e defi ed etwee two lasses, where o e lass is
dependent on another class
We need to understand 3 factors
◦ Scope
◦ Creator class
◦ Location and event of creation

4
Object Dependency
Scope
◦ The object is defined at class level
◦ The object is defined at a method level

Creation
◦ The class is creating the object where it is defined
◦ Another class is creating the object and the reference is provided to the class where the object is
defined

Location of creation
◦ Constructor
◦ Within a method

5
Object Dependency Examples
class ClassA
{
private ClassB classB = new ClassB();
}

6
Object Dependency Examples
class ClassA
{
private ClassB classB;

public ClassA()
{
classB = new ClassB();
}
}

7
Object Dependency Examples
class ClassA
{
private ClassB classB;

public ClassA()
{
}

public void processClassB()


{
classB = new ClassB();
}
}

8
Object Dependency Examples
class ClassA
{
private ClassB classB;

public ClassA()
{
classB = Factory.GetClassB();
}
}

9
Object Dependency Examples
class ClassA
{
private ClassB classB;

public ClassA()
{
}

public void processClassB()


{
classB = Factory.GetClassB();
}
}

10
Object Dependency Examples
class ClassA
{
private ClassB classB;

public ClassA(ClassB objClassB)


{
classB = objClassB;
}
}

11
Object Dependency Examples
class ClassA
{
private ClassB classB;

public ClassA()
{
}

public void processClassB(ClassB objClassB)


{
classB = objClassB;
}
}

12
Object Dependency Examples
class ClassA
{
public ClassA()
{
}

public void processClassB()


{
ClassB classB;

classB = new ClassB();


}
}

13
Object Dependency Examples
class ClassA
{
public ClassA()
{
}

public void processClassB()


{
ClassB classB;

classB = Factory.GetClassB();
}
}

14
Object Dependency Examples
class ClassA
{
public ClassA()
{
}

public void processClassB(ClassB objClassB)


{
ClassB classB;

classB = objClassB;
}
}

15
Software Design
Abstract
Client
Server

Server Extension
or Implementation
Structural Patterns
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy

17
Adapter
Intent
◦ Convert the interface of a class into another interface clients expect
◦ Adapter lets classes work together that could not otherwise because of incompatible interfaces

Applicability
◦ You want to use an existing class, and its interface does not match the one you need
◦ You want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes
that do not necessarily have compatible interfaces
◦ You need to use several existing subclasses, but it is impractical to adapt their interface by sub-classing
every one
◦ An object adapter can adapt the interface of its parent class

18
Adapter

19
Bridge
Intent
◦ Decouple an abstraction from its implementation so that the two can vary independently

Applicability
◦ You want to avoid a permanent binding between an abstraction and its implementation
◦ Both the abstractions and their implementations should be extensible by sub-classing
◦ Changes in the implementation of an abstraction should have no impact on clients

20
Bridge

21
Composite
Intent
◦ Compose objects into tree structures to represent part-whole hierarchies
◦ Composite lets clients treat individual objects and compositions of objects uniformly

Applicability
◦ You want to represent part-whole hierarchies of objects
◦ You want clients to be able to ignore the difference between compositions of objects and individual
objects
◦ Clients will treat all objects in the composite structure uniformly

22
Composite

23

You might also like