Factory pattern Unit of Work

Slides:



Advertisements
Similar presentations
GoF State Pattern Aaron Jacobs State(305) Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Advertisements

Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Design Patterns Yes, they are important Robert Cotton April 23, 2009.
Design Patterns CS is not simply about programming
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
Builder A Creational Design Pattern A Presentation by Alex Bluhm And.
Design Patterns and Principles of Object Oriented Application Development “design patterns are proven techniques used by experienced developers to tackle.
CSSE 374: Introduction to Gang of Four Design Patterns
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
1 Computer Science 340 Software Design & Testing Inheritance.
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Five design principles
Principles of Object Oriented Design
PRINCIPLES OF OBJECT ORIENTED DESIGN S.O.L.I.D. S.O.L.I.D Principles What is SOLID?  Acrostic of 5 Principles:  The Single Responsibility Principle.
SOLID Design Principles
Session 33 More on SOLID Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office: Moench.
Seung Ha.  Façade is generally one side of the exterior of a building, especially the front.  Meaning “frontage” or “face”  In software architecture,
Abstract Factory Pattern Jiaxin Wang CSPP Winter 2010.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Factory Method. Intent/Purpose Factory Method is used to deal with a problem of creating objects without specifying the EXACT class of object that we.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Introduction To Design Patterns
Design Patterns Source: “Design Patterns”, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides And Created.
Unit II-Chapter No. : 5- design Patterns
How to be a Good Developer
MPCS – Advanced java Programming
Design Patterns C++ Java C#.
Low Budget Productions, LLC
Factory Patterns 1.
Inheritance and Polymorphism
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Design Patterns C++ Java C#.
Introduction To Design Patterns
Dependency Injection Andres Käver, IT College 2016/2017 Spring.
object oriented Principles of software design
Adaptive Code Umamaheswaran Senior Software Engineer
Programming Design Patterns
Design Patterns Outline
Software Design Lecture : 14.
DESIGN PATTERNS : Introduction
CS 350 – Software Design Singleton – Chapter 21
Advanced ProgramMING Practices
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Advanced ProgramMING Practices
Design Patterns (Gamma, Helm, Johnson, Vlissides)
Introduction To Design Patterns
Composite Design Pattern By Aravind Reddy Patlola.
Object Oriented Design & Analysis
Introduction To Design Patterns
Dependency Inversion principle
Some principles for object oriented design
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Presentation transcript:

Factory pattern Unit of Work Andres Käver, IT College 2016/2017 Spring

OOD SOLID Object Oriented Design S – Single responsibility principle O – Open-closed principle L – Liskov substitution principle I – Interface segregation principle D – Dependency Inversion principle

SOLID Single responsibility principle Open-closed Principle A class should have one and only one reason to change, meaning that a class should have only one job. Open-closed Principle Objects or entities should be open for extension, but closed for modification. Liskov substitution principle Every subclass/derived class should be substitutable for their base/parent class

SOLID Interface segregation principle Dependency Inversion principle A client should never be forced to implement an interface that it doesn’t use or clients shouldn’t be forced to depend on methods they do not use. Dependency Inversion principle Entities must depend on abstractions not on concretions. High level module must not depend on the low level module, but they should depend on abstractions.

Factory pattern General idea - using another class to create objects on your behalf Common factory patterns Simple factory pattern The factory class and factory method Abstract factory method Service locator (anti-pattern) Dependency injection Design Patterns: Elements of Reusable Object-Oriented Software Erich Gamma, John Vlissides, Ralph Johnson, and Richard Helm - Gang of Four

The Factory public interface IDog { string GetBreed { get; } } public class DogPoodle : IDog public string GetBreed => "Poodle"; public class DogLabrador : IDog public string GetBreed => "Labrador"; public enum DogType { Poodle, Labrador public static class DogFactory { public static IDog CreateDog(DogType dogType) { switch (dogType) case DogType.Poodle: return new DogPoodle(); case DogType.Labrador: return new DogLabrador(); default: throw new NotImplementedException( message: $"DogFactory – unknown dog type: {dogType}"); }

The Factory Factory pattern deals with the instantiation of objects without exposing the instantiation logic. In other words, a Factory is actually a creator of objects which have a common interface. Factory is fixed

The Factory Method abstract class DogOwner { protected abstract IDog CreateDog(); private IDog _dog; private IDog Dog => _dog ?? (_dog = CreateDog()); public void CheckBreed() Console.WriteLine(value: Dog.GetBreed); } class PoodleOwner : DogOwner protected override IDog CreateDog() return new DogPoodle(); Generic processing in class, but need to vary actual object you are working with

Abstract factory Provide an interface for creating families of related or dependent objects without specifying their concrete classes. public interface ICat { string GetColor { get; } } public class WhiteCat : ICat public string GetColor => "White"; public class BlackCat : ICat public string GetColor => "Black"; public interface IDog { string GetBreed { get; } } public class DogPoodle : IDog public string GetBreed => "Poodle"; public class DogLabrador : IDog public string GetBreed => "Labrador";

Abstract factory abstract class PetFactory { public abstract IDog CreateDog(); public abstract ICat CreateCat(); } class NicePetFactory : PetFactory public override IDog CreateDog() return new DogLabrador(); public override ICat CreateCat() return new WhiteCat(); class BadPetFactory : PetFactory { public override IDog CreateDog() return new DogPoodle(); } public override ICat CreateCat() return new BlackCat();

Abstract factory class PetOwner { private readonly IDog _dog; private readonly ICat _cat; public PetOwner(PetFactory factory) _dog = factory.CreateDog(); _cat = factory.CreateCat(); } public void ListPets() Console.WriteLine( value: _dog.GetBreed); value: _cat.GetColor); class Program { static void Main(string[] args) PetFactory factory = new BadPetFactory(); PetOwner owner = new PetOwner(factory: factory); owner.ListPets(); factory = new NicePetFactory(); owner = }

Service Locator Variation of Factory pattern Metaexample var foo = ServiceLocator<IFoo>(); var bar = ServiceLocator<IBar>() Inputs are not limited – impossible to test (usually only one Service Locator for whole program – singleton) Anti-patern – using service locator in the middle of the code hides dependencies (not exposing them through constructor) Breaks SOLID principles

Unit of Work Effective access to data Conflict handling Transaction handling Atomic save

UoW Logical transaction inside business logic Client, invoice, products, taxes, warehouse – billing Business logic will modify several entities and inform UoW that work is done UoW makes an atomic save, business logic does not have to worry about implementation details

UoW Usually UoW provider is not used directly Interface Repo public interface IStudentUow { //save pending changes to the data store void Commit(); // repositories,standard IEFRepository<Grade> Grades { get; } IEFRepository<Student> Students { get; } IEFRepository<Subject> Subjects { get; } IEFRepository<GradeInSubject> GradeInSubjects { get; } IEFRepository<StudentInSubject> StudentInSubjects { get; } // repos with special methods in interfaces // ISubject Subjects2 { get; } }