Design patterns

Содержание

Слайд 2

Christopher Alexander says,
"Each pattern describes a problem which occurs over and

Christopher Alexander says, "Each pattern describes a problem which occurs over and
over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice"

In software engineering,
a design pattern is a general reusable solution to a commonly occurring problem. 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.
Object-oriented design patterns show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.

What is a Design Pattern ? 

Слайд 3

Must have… and must read…

Must have… and must read…

Слайд 4

Desing patterns classification 

Creational patterns concern the process of object creation.
Structural patterns

Desing patterns classification Creational patterns concern the process of object creation. Structural
deal with the composition of classes or objects.
Behavioral patterns characterize the ways in which classes or objects interact and distribute responsobility

Слайд 5

Abstract Factory 

Creational pattern
Provide an interface for creating families of related

Abstract Factory Creational pattern Provide an interface for creating families of related
or dependent objects without specifying their concrete classes.

Participants
The classes and/or objects participating in this pattern are:
Abstract Factory  
Concrete Factory   
Abstract Product   
Product  
Client  

Слайд 6

AbstractFactory - declares an interface for operations that create abstract products

Abstract

AbstractFactory - declares an interface for operations that create abstract products Abstract
Factory: participants

ConcreteFactory  - implements the operations to create concrete product objects

AbstractProduct   - declares an interface for a type of product object

Product  - defines a product object to be created by the corresponding concrete factory implements the AbstractProduct interface

Client  uses interfaces declared by AbstractFactory and AbstractProduct classes

Слайд 7

Abstract Factory: UML class diagram

Abstract Factory: UML class diagram

Слайд 8

CarFactory. Step 1

CarFactory. Step 1

Слайд 9

Car Factory. Step 2

Car Factory. Step 2

Слайд 10

Car Factory. Step 3

Car Factory. Step 3

Слайд 11

Car Factory. Step 4

Car Factory. Step 4

Слайд 12

Abstract Factory: example.

Abstract Factory - CarFactory

abstract class CarFactory    {      public abstract

Abstract Factory: example. Abstract Factory - CarFactory abstract class CarFactory { public
AbstractCar CreateCar();      public abstract AbstractEngine CreateEngine();    }

Слайд 13

Concrete Factory - BMWFactory

   
class BMWFactory : CarFactory    {      public override AbstractCar

Concrete Factory - BMWFactory class BMWFactory : CarFactory { public override AbstractCar
CreateCar()      {        return new BMWCar();      }  public override AbstractEngine CreateEngine()      {       return new BMWEngine();      }    }   

Abstract Factory->Concrete Factory

Слайд 14

    class AudiFactory : CarFactory    {      public override AbstractCar CreateCar()      {       return new

class AudiFactory : CarFactory { public override AbstractCar CreateCar() { return new
AudiCar();      }      public override AbstractEngine CreateEngine()      {       return new AudiEngine();      }    }

Abstract Factory->Concrete Factory

Concrete Factory - AudiFactory

Слайд 15

 abstract class AbstractCar   {
public abstract void MaxSpeed(AbstractEngine engine); 
}

Abstract Factory->Abstract Product

   abstract class

abstract class AbstractCar { public abstract void MaxSpeed(AbstractEngine engine); } Abstract Factory->Abstract
AbstractEngine    {       public int max_speed;    }

Abstract Product - AbsrtactCar

Abstract Product - AbsrtactEngine

Слайд 16

Class implementation - BMWCar

 class BMWCar : AbstractCar   {  public override void MaxSpeed(AbstractEngine engine) 

Class implementation - BMWCar class BMWCar : AbstractCar { public override void
{  Console.WriteLine(«Max speed:« + engine.max_speed.ToString());  }   } 

Abstract Product->Concrete Product

Class implementation - BMWEngine

   class BMWEngine : AbstractEngine   {      public BMWEngine()   {      max_speed = 200;   }   }

Слайд 17

  class AudiCar : AbstractCar    {  public override void MaxSpeed(AbstractEngine engine)  {  Console.WriteLine(«Макcимальная скорость:

class AudiCar : AbstractCar { public override void MaxSpeed(AbstractEngine engine) { Console.WriteLine(«Макcимальная
« + engine.max_speed.ToString());  }  }
//Concrete Engine
class AudiEngine : AbstractEngine    {      public AudiEngine()    {      max_speed = 180;    }    }

Abstract Product->Concrete Product

Class implementation - AudiCar

Слайд 18

   class Client   {     private AbstractCar abstractCar;  private AbstractEngine abstractEngine;  public Client(CarFactory car_factory)     {       abstractCar = car_factory.CreateCar();       abstractEngine

class Client { private AbstractCar abstractCar; private AbstractEngine abstractEngine; public Client(CarFactory car_factory)
= car_factory.CreateEngine ();     }     public void Run()     {        abstractCar.MaxSpeed(abstractEngine);      }    }

Abstract Factory. Пример

Class implementation – Client, works with abstract factory

Слайд 19

  public static void Main()     {       // Abstract Factory № 1  CarFactory bmw_car = new

public static void Main() { // Abstract Factory № 1 CarFactory bmw_car
BMWFactory ();       Client c1 = new Client(bmw_car);       c1.Run();      // Abstract Factory № 2       CarFactory audi_factory = new AudiFactory();       Client c2 = new Client(audi_factory);       c2.Run();       Console.Read();      }

Abstract Factory.

Слайд 20

Builder Pattern 

Creational pattern
Separate the construction of a complex object from

Builder Pattern Creational pattern Separate the construction of a complex object from
its representation so that the same construction process can create different representations.

Participants
The classes and/or objects participating in this pattern are:
Builder  
Concrete Builder 
Director   
Product   

Слайд 21

Builder - specifies an abstract interface for creating parts of a Product

Builder - specifies an abstract interface for creating parts of a Product
object

Builder: participants

ConcreteBuilder  -
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  - constructs an object using the Builder interface

Product 
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

Слайд 22

Builder: UML class diagram

Builder: UML class diagram

Слайд 24

Builder. UML

Builder. UML

Слайд 25

Builder: Example

HappyMeal, BigHappyMeal

 class HappyMeal    {      // contains information about parts of HappyMeal ArrayList

Builder: Example HappyMeal, BigHappyMeal class HappyMeal { // contains information about parts
parts = new ArrayList();      // adds a new part public void Add(string part)      {        parts.Add(part);      }      // Shows information about HappyMeal      public void Show()      {        Console.WriteLine(" Happy Meal Parts ——-");        foreach (string part in parts)        Console.WriteLine (part);      }    }

Слайд 26

Builder. Example

Declare a builder - an abstract interface for creating an object in parts

 abstract class

Builder. Example Declare a builder - an abstract interface for creating an
HappyMealBuilder    {          public abstract void BuildBurger();      public abstract void BuildPepsi();      public abstract void BuildFries();          public abstract void BuildToy();      public abstract HappyMeal GetProduct();    }   

Слайд 27

Builder

Declare a concrete builder BigHappyMeal

{  class BigHappyMealBuilder : HappyMealBuilder    {      private HappyMeal happy_meal =

Builder Declare a concrete builder BigHappyMeal { class BigHappyMealBuilder : HappyMealBuilder {
new HappyMeal();      public override void BuildBurger()      { happy_meal.Add("BigMac"); }      public override void BuildPepsi()      { happy_meal.Add("Pepsi 0.7"); }      public override void BuildFries()      { happy_meal.Add("BigFries");  }      public override void BuildToy()      { happy_meal.Add("Two toys");     }      public override HappyMeal GetProduct()      {        return happy_meal;      }    } 

Слайд 28

Builder

Declare a concrete builder HappyMeal

 class SmallHappyMealBuilder : HappyMealBuilder   {      private HappyMeal happy_meal = new HappyMeal();     

Builder Declare a concrete builder HappyMeal class SmallHappyMealBuilder : HappyMealBuilder { private
public override void BuildBurger()     { happy_meal.Add("Hamburger");  }      public override void BuildPepsi()     { happy_meal.Add("Pepsi 0.3");  }      public override void BuildFries()     { happy_meal.Add("SmallFries"); }      public override void BuildToy()     { happy_meal.Add("One toy");  }      public override HappyMeal GetProduct()     {       return happy_meal;      }    }   

Слайд 29

Builder

Class Director – will construcr the object

  class Director    {      // Constructing the object in

Builder Class Director – will construcr the object class Director { //
parts public void Construct(HappyMealBuilder builder)     {        builder.BuildBurger(); //Call Build-methods        builder.BuildPepsi();        builder.BuildFries();        builder.BuildToy();      }    }  
Имя файла: Design-patterns-.pptx
Количество просмотров: 183
Количество скачиваний: 0