You are on page 1of 19

Architecture and design patterns

Design Patterns
Patterns are about design and interaction of objects, as well as providing a communication platform, reusable solutions to commonly encountered programming challenges. They are categorized in three groups: Creational. Structural. Behavioral.

Patterns List

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

// "Creator" abstract class Creator { public abstract Product FactoryMethod(); } // "ConcreteProductA" // "ConcreteCreator" class ConcreteProductA : Product class ConcreteCreatorA : Creator { { } public override Product FactoryMethod() // "ConcreteProductB" { class ConcreteProductB : Product return new ConcreteProductA(); { } } } // "ConcreteCreator" // Factory Method pattern -- Structural example using System; class ConcreteCreatorB : Creator using System.Collections; { class MainApp public override Product FactoryMethod() { { static void Main() return new ConcreteProductB(); { } // An array of creators } Creator[] creators = new Creator[2]; } creators[0] = new ConcreteCreatorA(); creators[1] = new ConcreteCreatorB(); // Iterate over creators and create products foreach(Creator creator in creators) { Product product = creator.FactoryMethod(); Console.WriteLine("Created {0}", product.GetType().Name); }

// "Product" abstract class Product { }

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

Ensure a class has only one instance and provide a global point of access to it.

Example: class Singleton { private static Singleton instance;

class MainApp {

static void Main() { // Constructor is protected -- cannot use // Note: Constructor is 'protected' new Singleton s1 = Singleton.Instance(); protected Singleton() { Singleton s2 = Singleton.Instance();

public static Singleton Instance() { { Console.WriteLine("Objects are the // Use 'Lazy initialization' same instance"); if (instance == null) } { instance = new Singleton(); } return instance; } } }

if (s1 == s2)

Provide a unified interface to a set of interfaces in a subsystem. Faade defines a higher-level interface that makes the subsystem easier to use.

Model View Controller

The Model-View-Controller (MVC) pattern separates the modeling of the domain, the presentation, and the actions based on user input into three separate classes The controller changes the model The View Listens to Model Changed events and update itself

Define Application Layers

A logical architecture helps developers understand how a system is put together, how it works, and how they should add code to it. Logical architectures are not required to make your application work; instead, they are simply a means of explaining how it works. Therefore, it can be helpful to define a logical understanding of the layers, components, and communication paths in your system.

What Does It Mean to Have Application Layers?

Application layers can be thought of as similar to application tiers. The term tiers was preferred for a time because applications were being built as two-tier and three-tier designs. Tiers include the UI, middle tier, and database tier. A two-tier application is a UI working with a database. A three-tier application adds a middle tier to manage the business objects and rules in the system.

Layers of ASP.NET Applications

Most enterprise-level ASP.NET applications written today employ some version of the three primary layers: UI, middle tier, and database. Each of these layers might then be split into additional layers.

Data Flow Using ADO.NET

N tier Architecture
The n tier architecture is based on the concept of separating a system to different layers (usually 3) Each layer interacts with only the layer directly below, and has specific function that it is responsible for. Classic for IT systems

N tier Architecture
3 Tier architecture:

Presentation Layer Presentation Layer is the layer responsible for displaying user interface. Business Tier Business Tier is the layer responsible for accessing the data tier to retrieve, modify and delete data to and from the data tier and send the results to the presentation tier. This layer is also responsible for processing the data retrieved and sent to the presentation layer. BLL and DAL Often this layer is divided into two sub layers: the Business Logic Layer (BLL), and the Data Access Layers (DAL). Business Logic Layers are above Data Access Layers, meaning BLL uses DAL classes and objects. DAL is responsible for accessing data and forwarding it to BLL. Data Tier Data tier is the database or the source of the data itself.

Common mistakes tightly coupling layers in technology and writing business logic in presentation tier

SOA is an architectural style whose goal is to achieve loose coupling among interacting software agents. A service is a unit of work done by a service provider to achieve desired end results for a service consumer in SOA, services are the mechanism by which needs and capabilities are brought together.

SOA - Principles
Visibility the ability for a service consumer to see a service provider (Awareness, willingness and Reachability) Interaction - the activity of using a capability. (usually message exchange by contracts, constraints and policies, for example Web Service Description Language) Effect the result of an interaction

SOA - example