Презентация СNet English 04

Скачать презентацию  СNet English 04 Скачать презентацию СNet English 04

snet_english_04.ppt

  • Размер: 1.3 Mегабайта
  • Количество слайдов: 30

Описание презентации Презентация СNet English 04 по слайдам

NET. C #. 04 Design Patterns.  NET. C #. 04 Design Patterns.

Christopher Alexander says ,  Each pattern describes a problem which occurs over  and overChristopher 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 ?

Must have… and must read… Must have… and must read…

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

Abstract Factory  Creational pattern Provide an interface for creating families of related or dependent objectsAbstract Factory Creational pattern Provide an interface for creating families of related or dependent objects without specifying their concrete classes. P articipants The classes and/or objects participating in this pattern are: • Abstract Factory • Concrete Factory • Abstract Product • Client

Abstract. Factory  - declares an interface for operations that create abstract products Abstract Factory :Abstract. Factory — declares an interface for operations that create abstract products Abstract Factory : participants Concrete. Factory — implements the operations to create concrete product objects Abstract. Product — declares an interface for a type of product object Product — defines a product object to be created by the corresponding concrete factory implements the Abstract. Product interface Client uses interfaces declared by Abstract. Factory and Abstract. Product classes

Abstract Factory : UML class diagram Abstract Factory : UML class diagram

Car. Factory. Step 1 Car. Factory. Step

Car Factory. Step 2 Car Factory. Step

Car Factory. Step 3 Car Factory. Step

Car Factory. Step 4 Car Factory. Step

Abstract Factory :  example. Abstract Factory -  Car. Factory  abstract class Car. FactoryAbstract Factory : example. Abstract Factory — Car. Factory abstract class Car. Factory { public abstract Abstract. Car Create. Car(); public abstract Abstract. Engine Create. Engine(); }

Concrete Factory - BMWFactory  class BMWFactory : Car. Factory { public override Abstract. Car Create.Concrete Factory — BMWFactory class BMWFactory : Car. Factory { public override Abstract. Car Create. Car() { return new BMWCar(); } public override Abstract. Engine Create. Engine() { return new BMWEngine(); } }Abstract Factory ->Concrete Factory

 class Audi. Factory : Car. Factory { public override Abstract. Car Create. Car() { return class Audi. Factory : Car. Factory { public override Abstract. Car Create. Car() { return new Audi. Car(); } public override Abstract. Engine Create. Engine() { return new Audi. Engine(); } }Abstract Factory ->Concrete Factory — Audi. Factory

 abstract class Abstract. Car { public abstract void Max. Speed(Abstract. Engine engine);  }Abstract Factory abstract class Abstract. Car { public abstract void Max. Speed(Abstract. Engine engine); }Abstract Factory ->Abstract Product abstract class Abstract. Engine { public int max_speed; } Abstract Product — Absrtact. Car Abstract Product — Absrtact. Engine

Class implementation - BMWCar class BMWCar : Abstract. Car {  public override void Max. Speed(Abstract.Class implementation — BMWCar class BMWCar : Abstract. Car { public override void Max. Speed(Abstract. Engine engine) { Console. Write. Line( « Max speed : « + engine. max_speed. To. String()); } } Abstract Product->Concrete Product Class implementation — BMWEngine class BMWEngine : Abstract. Engine { public BMWEngine() { max_speed = 200; } }

 class Audi. Car : Abstract. Car { public override void Max. Speed(Abstract. Engine engine) class Audi. Car : Abstract. Car { public override void Max. Speed(Abstract. Engine engine) { Console. Write. Line( «Макcимальная скорость: « + engine. max_speed. To. String()); } } //Concrete Engine class Audi. Engine : Abstract. Engine { public Audi. Engine() { max_speed = 180; } }Abstract Product->Concrete Product Class implementation — Audi. Car

 class Client { private Abstract. Car abstract. Car; private Abstract. Engine abstract. Engine; public Client(Car. class Client { private Abstract. Car abstract. Car; private Abstract. Engine abstract. Engine; public Client(Car. Factory car_factory) { abstract. Car = car_factory. Create. Car(); abstract. Engine = car_factory. Create. Engine (); } public void Run() { abstract. Car. Max. Speed(abstract. Engine); } }Abstract Factory. Пример Class implementation – Client, works with abstract factory

 public static void Main() { // Abstract Factory № 1  Car. Factory bmw_car = public static void Main() { // Abstract Factory № 1 Car. Factory bmw_car = new BMWFactory (); Client c 1 = new Client(bmw_car); c 1. Run(); // Abstract Factory № 2 Car. Factory audi_factory = new Audi. Factory(); Client c 2 = new Client(audi_factory); c 2. Run(); Console. Read(); }Abstract Factory.

Builder Pattern  Creational pattern Separate the construction of a complex object from its representation soBuilder Pattern Creational pattern Separate the construction of a complex object from its representation so that the same construction process can create different representations. P articipants The classes and/or objects participating in this pattern are: • Builder • Concrete Builder • Director • Product

Builder  - specifies an abstract interface for creating parts of a Product object. Builder: participantsBuilder — specifies an abstract interface for creating parts of a Product object. Builder: participants Concrete Builder — • 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 Concrete. Builder 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

Builder: UML class diagram Builder: UML class diagram

Builder Builder

Builder. UML Builder. UML

Builder: Example Happy. Meal, Big. Happy. Meal class Happy. Meal { // containsinformation about parts ofBuilder: Example Happy. Meal, Big. Happy. Meal class Happy. Meal { // containsinformation about parts of Happy. Meal Array. List parts = new Array. List(); // adds a newpart public void Add(string part) { parts. Add(part); } // Shows information about Happy. Meal public void Show() { Console. Write. Line(» Happy Meal Parts ——-«); foreach (string part in parts) Console. Write. Line (part); } }

Builder.  Example Declare a builder - an abstract interface  for creating an object inBuilder. Example Declare a builder — an abstract interface for creating an object in parts abstract class Happy. Meal. Builder { public abstract void Build. Burger(); public abstract void Build. Pepsi(); public abstract void Build. Fries(); public abstract void Build. Toy(); public abstract Happy. Meal Get. Product(); }

Builder Declare a concrete builder  Big. Happy. Meal { class Big. Happy. Meal. Builder :Builder Declare a concrete builder Big. Happy. Meal { class Big. Happy. Meal. Builder : Happy. Meal. Builder { private Happy. Meal happy_meal = new Happy. Meal(); public override void Build. Burger() { happy_meal. Add(«Big. Mac»); } public override void Build. Pepsi() { h appy_meal. Add(«Pepsi 0. 7»); } public override void Build. Fries() { happy_meal. Add(«Big. Fries»); } public override void Build. Toy() {happy_meal. Add(«Two toys»); } public override Happy. Meal Get. Product() { return happy_meal; } }

Builder Declare a concrete builder  Happy. Meal class Small. Happy. Meal. Builder : Happy. Meal.Builder Declare a concrete builder Happy. Meal class Small. Happy. Meal. Builder : Happy. Meal. Builder { private Happy. Meal happy_meal = new Happy. Meal(); public override void Build. Burger() {happy_meal. Add(«Hamburger»); } public override void Build. Pepsi() {happy_meal. Add(«Pepsi 0. 3»); } public override void Build. Fries() { ha ppy_meal. Add(«Small. Fries»); } public override void Build. Toy() {happy_meal. Add(«One toy»); } public override Happy. Meal Get. Product() { return happy_meal; } }

Builder Class  Director – will construcr the object class Director { // Constructingthe objectin partsBuilder Class Director – will construcr the object class Director { // Constructingthe objectin parts public void Construct (Happy. Meal. Builder builder) { builder. Build. Burger(); //Call Build-methods builder. Build. Pepsi(); builder. Build. Fries(); builder. Build. Toy(); } }

Builder public static void Main() {  // Create a. Director andbuilders  Director director =Builder public static void Main() { // Create a. Director andbuilders Director director = new Director(); Happy. Meal. Builder big_hmbuilder = new B ig. Happy. Meal. Builder(); Happy. Meal. Builder small_hmbuilder = new Small. Happy. Meal. Builder(); // Constructtwo products director. Construct(big_hmbuilder); Happy. Meal hm 1 = big_hmbuilder. Get. Product(); hm 1. Show(); director. Construct(small_hmbuilder); Happy. Meal hm 2 = small_hmbuilder. Get. Product(); hm 2. Show(); }