design pattern for c# part 1

19
Design Patterns The core of Software architecture Creational Patterns- Part-1 By: Shahzad Sarwar

Upload: shahzad

Post on 06-May-2015

5.887 views

Category:

Technology


0 download

DESCRIPTION

This is series of presentations covering Design patterns Creational Patterns Part -1 Abstract Factory , Builder , Factory Method , Prototype , Singleton Structural Patterns Part-2 ( Later ) Adapter , Bridge , Composite , Decorator , Facade , Flyweight , Proxy Behavioral Patterns Part-3 ( Later ) Chain of Resp , Command , Interpreter , Iterator ,Mediator , Memento , Observer , State , Strategy , Template Method , Visitor AJAX Design Patterns Part-4 (Later) AJAX Level I , AJAX Level II AJAX Message Patterns (Text Message , HTML Message , XML Message , XSL Message , JavaScript Msg , JSON Message , Custom Message ) Software AntiPatterns Part-5 (Later) Software Refactoring Part-6 (Later)

TRANSCRIPT

Page 1: Design Pattern For C# Part 1

Design Patterns The core of Software architecture

Creational Patterns- Part-1

By: Shahzad Sarwar

Page 2: Design Pattern For C# Part 1

Agenda - Future Architecture TracksThis is series of presentations covering Design patterns• Creational Patterns Part -1

Abstract Factory , Builder  , Factory Method  , Prototype  , Singleton

• Structural Patterns Part-2 ( Later ) Adapter ,  Bridge , Composite , Decorator , Facade , Flyweight  , Proxy

• Behavioral Patterns Part-3 ( Later )

Chain of Resp  , Command  , Interpreter , Iterator 

  ,Mediator , Memento , Observer , State , Strategy , 

Template Method , Visitor

Page 3: Design Pattern For C# Part 1

Agenda - Future Architecture Tracks• AJAX Design Patterns Part-4 (Later)

AJAX Level I , AJAX Level II

AJAX Message Patterns

(Text Message ,  HTML Message , XML Message ,  XSL Message  , JavaScript Msg ,  JSON Message , Custom Message  )

• Software AntiPatterns Part-5 (Later)

• Software Refactoring Part-6 (Later)

Page 4: Design Pattern For C# Part 1

Design Pattern

• In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't 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.

Types of Design Patterns:• Creational design patterns • Structural design patterns • Behavioral design patterns

Page 5: Design Pattern For C# Part 1

Design Principles• Robert Martin "Agile Software Development: Principles,

Patterns, and Practices“• Characteristics of a bad design:

• Rigidity - It is hard to change because every change affects too many other parts of the system.

• Fragility - When you make a change, unexpected parts of the system break.

• Immobility - It is hard to reuse in another application because it cannot be disentangled from the current application.

Page 6: Design Pattern For C# Part 1

Software Design Principles

Open Close Principle • Software entities like classes, modules and

functions should be open for extension but closed for modifications.

Dependency Inversion Principle • High-level modules should not depend on low-level

modules. Both should depend on abstractions. • Abstractions should not depend on details. Details

should depend on abstractions.

Page 7: Design Pattern For C# Part 1

Software Design PrinciplesInterface Segregation Principle

• A class should have only one reason to change.• Tom DeMarco, His book Structured Analysis and

Systems Specification, 1979. • Robert Martin reinterpreted the concept and defined

the responsibility as a reason to change.

Page 8: Design Pattern For C# Part 1

Singleton(Creational Patterns) Ensure a class has only one instance and provide a global point of

access to it.

ParticipantsExamplesSingleton   (LoadBalancer)

•defines an Instance operation that lets clients access its unique instance. Instance is a class operation. •responsible for creating and maintaining its own unique instance.

Page 9: Design Pattern For C# Part 1

Abstract Factory Design (Creational Patterns) • Provide an interface for creating families of related or dependent

objects without specifying their concrete classes.

Page 10: Design Pattern For C# Part 1

Participants The classes and/or objects participating in this pattern are: • AbstractFactory  (ContinentFactory)

• declares an interface for operations that create abstract products

• ConcreteFactory   (AfricaFactory, AmericaFactory) • implements the operations to create concrete product

objects • AbstractProduct   (Herbivore, Carnivore)

• declares an interface for a type of product object • Product  (Wildebeest, Lion, Bison, Wolf)

• defines a product object to be created by the corresponding concrete factory

• implements the AbstractProduct interface • Client  (AnimalWorld)

• uses interfaces declared by AbstractFactory and AbstractProduct classes

Examples

Page 11: Design Pattern For C# Part 1

Factory Method(Creational Patterns) Define an interface for creating an object, but let subclasses decide which

class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Page 12: Design Pattern For C# Part 1

Participants

• The classes and/or objects participating in this pattern are: • Product  (Page)

• defines the interface of objects the factory method creates• ConcreteProduct  (SkillsPage, EducationPage,

ExperiencePage) • implements the Product interface

• Creator  (Document) • declares the factory method, which returns an object of type

Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.

• may call the factory method to create a Product object. • ConcreteCreator  (Report, Resume)

• overrides the factory method to return an instance of a ConcreteProduct.

Examples

Page 13: Design Pattern For C# Part 1

Prototype(Creational Patterns)

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.

Page 14: Design Pattern For C# Part 1

Participants

    The classes and/or objects participating in this pattern are:

• Prototype  (ColorPrototype) • declares an interface for cloning itself

• ConcretePrototype  (Color) • implements an operation for cloning itself

• Client  (ColorManager) • creates a new object by asking a prototype to

clone itself

Examples

Page 15: Design Pattern For C# Part 1

Builder   Separate the construction of a complex object from its representation

so that the same construction process can create different representations.

Page 16: Design Pattern For C# Part 1

ParticipantsThe classes and/or objects participating in this pattern are: • Builder  (VehicleBuilder)

• specifies an abstract interface for creating parts of a Product object

• ConcreteBuilder  (MotorCycleBuilder, CarBuilder, ScooterBuilder) • constructs and assembles parts of the product by

implementing the Builder interface • defines and keeps track of the representation it creates • provides an interface for retrieving the product

• Director  (Shop) • constructs an object using the Builder interface

• Product  (Vehicle) • represents the complex object under construction.

ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled

• includes classes that define the constituent parts, including interfaces for assembling the parts into the final result

Examples

Page 17: Design Pattern For C# Part 1

Criticism on Design Patterns Targets the wrong problem Lacks formal foundations Leads to inefficient solutions Does not differ significantly from other abstractions

Page 18: Design Pattern For C# Part 1

Reference:

• www.dofactory.com • www.oodesign.com• http://wiki.asp.net/page.aspx/276/design-patterns/• http://sourcemaking.com/design_patterns

Page 19: Design Pattern For C# Part 1

Further Discussion

A soft copy will be available at:

http://softarchitect.wordpress.com

For Future discussion, join

http://tech.groups.yahoo.com/group/SoftArchitect/