CSC/ECE 517 Fall 2012/ch2b 2w42 aa: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 109: Line 109:
Adapter functions as a wrapper or modifier of an existing class. It provides a different or translated view of that class.
Adapter functions as a wrapper or modifier of an existing class. It provides a different or translated view of that class.


== example ==
== Example ==
interface Cost {
interface Cost {
   double calculateCost(CustomerId, OrderId, Address);
   double calculateCost(CustomerId, OrderId, Address);

Revision as of 22:44, 19 November 2012

Introduction

Facade Pattern

Intent

  • Provide a unified interface to a set of interfaces in a subsystem.
  • Wrap a complicated subsystem with a simpler interface.

Facade discusses encapsulating a complex subsystem within a single interface object. This reduces the learning curve necessary to successfully leverage the subsystem. It also promotes decoupling the subsystem from its potentially many clients. On the other hand, if the Facade is the only access point for the subsystem, it will limit the features and flexibility that “power users” may need. The Facade object should be a fairly simple advocate or facilitator. It should not become an all-knowing oracle or “god” object.

Adapter Pattern

Intent

  • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
  • Wrap an existing class with a new interface.
  • Impedance match an old component to a new system.

Convert the interface of a class into another interface clients expect. Adapter lets classes worktogether that couldn't otherwise because of incompatible interfaces.

Abstract Factory Pattern

Intent

  • Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  • A hierarchy that encapsulates: many possible “platforms”, and the construction of a suite of “products”.
  • The new operator considered harmful.

If an application is to be portable, it needs to encapsulate platform dependencies. These “platforms” might include: windowing system, operating system, database, etc. Too often, this encapsulatation is not engineered in advance, and lots of #ifdef case statements with options for all currently supported platforms begin to procreate like rabbits throughout the code.

Mediator Pattern

Facade Pattern

Non-Software examples

In most of the Pizza centers, orders will be given through phone calls with the customer interacting with the Customer service representative. In this case, consumers do not have access to the Billing system, Kitchen and delivery department. The customer service representative acts as an interface and interacts with each of the departments involved in the transaction and ensures that Pizzas are delivered to the consumer. Customer Service representative corresponds to the facade. Individual departments involved in the transaction correspond to Sub-systems.

Software examples

Web Service Facade Solution Architecture provides an example for Facade design pattern. In this architectural solution, instead of rewriting legacy applications or customizing those with middleware to connect to other applications one by one, this solution helps create a "facade" for the legacy application. Other applications are easily "plugged into" this facade. By modeling a legacy application into its basic functions of create, read, update, and delete and then exposing these functions as Web methods, the Web service facade solution allows other applications to access legacy data by making use of common Web services through standardized protocols. In this way, Facade decouples layers so that they do not depend on each other which can make it easier to develop, to use and to promote code re-use.

Without a Facade, Client contacts the Database to retrieve Company objects. It then retrieves Division objects from them and finally gains access to Employee objects.It uses four classes.
With a Facade, the Client is shielded from most of the classes. It uses the Database Facade to retrieve Employee objects directly.

C# Example

using System;
using System.Collections.Generic;
namespace DesignPatterns.Facade { public class OrderFacade { //Places an Order and Returns an Order ID public int placeOrder(int CustomerID, List<BasketItem> Products) { Order anOrder = new Order(); OrderLine anOrderLine = new OrderLine(); Address DespatchAddress = Address.getCustomerDespatchAddress(CustomerID); int OrderId = anOrder.requestOrderID(); anOrder.createOrder(OrderId, DespatchAddress); anOrderLine.addOrderLinesToOrder(OrderId, Products); return OrderId; } }
//order class public class Order { public int requestOrderID() { //Creates and Returns a Unique Order ID }
public void createOrder(int OrderId, Address DespatchAddress) { } }
//OrderLine Class public class OrderLine { public void addOrderLinesToOrder(int OrderId, List<BasketItem> Products) { } }
//Public Customer public class Address { public static Address getCustomerDespatchAddress(int CustomerID) { return new Address(); } //Address properties }
public class BasketItem { //BasketItem Properties } }

Adapter Pattern

Two types of adapter patterns
1)Object Adapter pattern— relies on one object(the adapting object) containing another (theadapted object).
2)Class Adapter pattern— implemented withmultiple inheritance

Problem
An “off the shelf” component offers compelling functionality that you would like to reuse, but its “view of the world” is not compatible with the philosophy and architecture of the system currently being developed.

Discussion
Reuse has always been painful and elusive. One reason has been the tribulation of designing something new, while reusing something old. There is always something not quite right between the old and the new. It may be physical dimensions or misalignment. It may be timing or synchronization. It may be unfortunate assumptions or competing standards.
It is like the problem of inserting a new three-prong electrical plug in an old two-prong wall outlet – some kind of adapter or intermediary is necessary.

Adapter is about creating an intermediary abstraction that translates, or maps, the old component to the new system. Clients call methods on the Adapter object which redirects them into calls to the legacy component. This strategy can be implemented either with inheritance or with aggregation.

Adapter functions as a wrapper or modifier of an existing class. It provides a different or translated view of that class.

Example

interface Cost {

  double calculateCost(CustomerId, OrderId, Address);

}

Class RectangularShipment implements Cost {

  double calculateCost(CustomerId, OrderId, Address) {
    //based on dimensions calculate packaging cost
  }

}

Class CircularShipment implements Cost {

  double calculateCost(CustomerId, OrderId, Address) {
   //based on dimensions calculate packaging cost
  }

}

Abstract Factory Pattern

Provide a level of indirection that abstracts the creation of families of related or dependent objects without directly specifying their concrete classes. The “factory” object has the responsibility for providing creation services for the entire platform family. Clients never create platform objects directly, they ask the factory to do that for them.

This mechanism makes exchanging product families easy because the specific class of the factory object appears only once in the application - where it is instantiated. The application can wholesale replace the entire family of products simply by instantiating a different concrete instance of the abstract factory. Because the service provided by the factory object is so pervasive, it is routinely implemented as a Singleton.


Mediator Pattern

Differences n similarities between Adapter and Facade

1)In both the Facade and Adapter pattern we havepreexisting classes.

2)In the Facade, however, we do not have aninterface we must design to, as we do in the Adapter pattern.

3)We are not interested in polymorphic behavior in theFacade, while in the Adapter, We probably am.(There are times when we just need to design to aparticular API and therefore must use an Adapter)


4)In the case of the Facade pattern, the motivation isto simplify the interface. With the Adapter, whilesimpler is better,We are trying to design to anexisting interface and cannot simplify things even if a simpler interface were otherwise possible.

Bottom line

A Facade simplifies an interface whilean Adapter converts the interface into a preexistinginterface.