CSC/ECE 517 Fall 2012/ch2a 2w17 pt: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
(Created page with "==Design Pattern== A software design pattern is a problem-solution pair that gives a general re-usable solution to a commonly occurring problem and can be applied in a similar f...")
 
No edit summary
Line 10: Line 10:


==UML Class Diagram==
==UML Class Diagram==
 
[[File:uml.png]]


By defining a Subject interface, the presence of the Proxy object standing in place of the RealSubject is transparent to the client.
By defining a Subject interface, the presence of the Proxy object standing in place of the RealSubject is transparent to the client.

Revision as of 20:33, 16 October 2012

Design Pattern

A software design pattern is a problem-solution pair that gives a general re-usable solution to a commonly occurring problem and can be applied in a similar fashion in new contexts. A design pattern is a template that gives a solution to many similar situations. It cannot be directly transformed into code. The solution is usually a simple mechanism because it is a collaboration between two or more classes, objects, services, processes, threads, components, or nodes that work together to solve the underlying architecture or development challenge Design patterns documents simple mechanism that work. They provide a common vocabulary and taxonomy for developers and architects. They allow solutions to be described concisely as combinations of patterns. They enable reuse of architecture, design, and implementation decisions. All these features make design patterns very useful to software developers and architects. Design patterns are typically represented as relationships between classes and objects with defined responsibilities that act in concert to carry out the solution. For instance consider the Adapter pattern. Adapter pattern provides a solution to the scenario in which a client and server need to interact with one another, but cannot because their interfaces are not compatible. To implement the Adapter pattern, you create a custom class that honors the interface provided by the server and defines the server operations in terms the client expects. This is a much better solution than altering the client to match the interface of the server. There are many design patterns like Algorithm strategy patterns, Computational design patterns, Execution patterns, Implementation strategy patterns, Structural design patterns.

Proxy pattern

The Proxy pattern, also known as a surrogate pattern, is a Structural design pattern. A proxy provides a surrogate or placeholder for another object to control access to it. There will be situations in which a client does not or cannot reference an object directly but still wants to interact with the object. A proxy object can act as an intermediary between the client and the target object. A proxy object has the same interface as the target object. The proxy holds a reference to the target object and can forward requests to the target as required. In effect, the proxy object has the authority to act on behalf of the client to interact with the target object.

UML Class Diagram

By defining a Subject interface, the presence of the Proxy object standing in place of the RealSubject is transparent to the client.

Applications of Proxy Pattern

  • A proxy can interface a network connection, a large object in memory, a file or any other resource that is expensive or impossible to duplicate.
  • Proxies are useful wherever there is a need for a more sophisticated reference to a object than a simple pointer or simple reference can provide.
  • In situations where multiple copies of a complex object must exist, the proxy pattern can be used to reduce the application’s memory requirements. A single instance of the complex object and multiple proxy objects are created all of which are references to the original complex object. Any operation on the proxy is forwarded to the original object.

Types of proxies

Remote Proxy

A remote proxy provides a reference to an object located in a different address space on the same or different machine. They are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.

Virtual Proxy

A virtual proxy allows the creation of a memory intensive object on demand. The object will not be created until it is really needed.

Copy-on-Write Proxy

A copy-on-write defers cloning a target object until required by client actions. It is really a form of virtual proxy.

Protection Proxy

A protection proxy provides different clients with different levels of access to a target object.

Cache Proxy

A cache proxy provides temporary storage of the results of expensive target operations so that multiple clients can share the results.

Firewall Proxy

A firewall proxy protects targets from bad clients (or vice versa).

Synchronization Proxy

A synchronization proxy provides multiple accesses to a target object.

Smart reference Proxy

A smart reference proxy provides additional actions whenever a target object is referenced such as counting the number of references to the object.

Examples of Proxy pattern

C++

http://sourcemaking.com/design_patterns/proxy/cpp/1 http://sourcemaking.com/design_patterns/proxy/cpp/2


Java

http://sourcemaking.com/design_patterns/proxy/java/1

C#

http://sourcemaking.com/design_patterns/proxy/c%2523

PHP

http://sourcemaking.com/design_patterns/proxy/php

Similar patterns

Adapter Pattern

An adapter pattern translates one interface for a class into a compatible interface. An adapter allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients while using the original interface

Bridge Pattern

A Bridge Pattern decouples an abstraction from its implementation so that the two can vary independently". The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes.

Composite Pattern

A composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.

Decorator Pattern

The decorator pattern can be used to extend (decorate) the functionality of a certain object dynamically, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new decorator class that wraps the original class.

Facade Pattern

A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can make a software library easier to use, understand and test, since the facade has convenient methods for common tasks. It can make the library more readable, for the same reason. It can reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system. It wraps a poorly designed collection of API’s with a single well-designed API .

Flyweight Pattern

A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory. Often some parts of the object state can be shared, and it is common practice to hold them in external data structures and pass them to the flyweight objects temporarily when they are used.

Front Controller Pattern

Front controllers are often used in web applications to implement workflows. While not strictly required, it is much easier to control navigation across a set of related pages (for instance, multiple pages might be used in an online purchase) from a front controller than it is to make the individual pages responsible for navigation.

Module Pattern

A module pattern is a design pattern used to implement the concept of software modules, defined by modular programming, in a programming language that does not support it, or only supports it, partially.

Comparisons

Comparison of