Sunday, August 6, 2017

Desgin Patterns - GOF and J2EE

Design Patterns:-
- provide solutions to common software design problems.
 - or the best practices one software developer should follow in developing software.
- important to apply appropriate pattern for a situation. Otherwise, can over complicate code or may lead to maintainability issues.

GOF
1. Creational Pattern:-
- provide ways to create single or group of related objects.

Singelton:-
- ensures that only one object of a particular class is ever created.
- all further references to objects of this particular class refer to same underlying instance.

Prototype:-
- used to create a new object by copying all of the properties of an existing object.
- this practice is particularly useful when the construction of a new object is inefficient.

Factory:-
- create an object without exposing the creation logic to the client and refer to the newly-created object using a common interface. It is one of the most widely used creational patterns.

Main Class -> asks -> Factory -> creates -> Interface/Abstract Class -> Concrete Class 
                                                                                                              -> Concrete Class 

Real Example:- You have some row materiel (Input) , you provided it to one factory (factory class) and the factory gives you a product (object) so based on your raw material and factory implementation you get a product.



Abstract Factory:-
- creating a factory of related or dependent objects without explicitly specifying their concrete classes. Each generated factory can give objects as per factory pattern. So there is a super-factory which creates other factories.

Real Example:-  Its like you have raw material , you provided it to one factory as input but instead of processing your input this factory delegates the task to create product to its sub factories . Simply abstract factory means multiple factories.



The main difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.

Builder:-
- construct a complex object from simple objects using step by step approach.


2. Structural Pattern:-
- provide a manner to define relationships between classes or objects.

Facade:-
- used to simplify access to functionality in complex designed subsystem(complicated to use or difficult to understand).
- facade class is a "wrapper" that contains a set of members that are easily understood and simple to use. These members access the subsystem on behalf of the facade user, hiding the implementation details of the underlying code.
- One example use of the facade pattern is for integrating a web site with a business application. The existing software may include large amounts of business logic that must be accessed in a particular manner. The web site may require only limited access to this business logic. For example, the web site may need to show whether an item for sale has reached a limited level of stock. The isLowStock method of the facade class could return a boolean value to indicate this. Behind the scenes, this method could be hiding the complexities of processing the current physical stock, incoming stock, allocated items and the low stock level for each item.



Proxy:-
- a class represents functionality of another class.
- a proxy is a wrapper or agent object that is being called by the client which references to real serving object behind the scenes.
- use of the proxy can simply be forwarding to the real object, or can provide additional logic.
- In the proxy, extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked. For the client, usage of a proxy object is similar to using the real object, because both implement the same interface.



Remote Proxy. A remote proxy provides a local object that references a subject object in another location, generally via a network connection. The proxy performs the required actions to encode the requests for network transfer and accept the results from the remote resource before returning them to the client.

Adapter:-
- used to allow two incompatible types to communicate.
- involves a single class which is responsible to join functionalities of independent or incompatible interfaces. This is called the adapter which acts as a translator between the two types.

A real life example could be a case of card reader which acts as an adapter between memory card and a laptop. You plug-in the memory card into card reader and card reader into the laptop so that memory card can be read via laptop.







Decorator:-
- creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact. Its like acting as a wrapper to existing class.


Composite:- 
- creates a tree structure of group of objects that is treated the same way as a single instance of the same type of object.
- compose objects into tree structures to represent part as well as whole hierarchies.
- lets clients treat individual objects and compositions uniformly.



Flyweight:-
- If an application requires a large number of fine-grained objects of a class, share them instead of repeated instantiation.
The object you share is referred as a flyweight.
To apply flyweight pattern, the object properties can be divided into intrinsic and extrinsic properties.

Intrinsic properties make the object unique whereas extrinsic properties are set by client code and used to perform different operations.
For applying flyweight pattern, we need to create a Flyweight factory that returns the shared objects. For our example, lets say we need to create a drawing with lines and Ovals. So we will have an interface Shape and its concrete implementations as Line and Oval. Oval class will have intrinsic property to determine whether to fill the Oval with given color or not whereas Line will not have any intrinsic property.

3. Behavioral Patterns:-
- define manners of communication between classes and objects

Strategy:-
defines a set of related algorithm and encapsulate them in separated classes, and allows client to choose any algorithm at run time.
- It allows to add new algorithm without modifying existing algorithms or context class, which uses algorithm or strategies.

- Combination of Collections.sort() and Comparator interface is an solid example of Strategy pattern.

There are three participants in the strategy pattern.

Strategy declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a concrete strategy.

Concrete strategy implements the algorithm using the StrategyInterface.

Context is configured with a ConcreteStrategy Object; maintains a reference to a strategy object; may define an interface that lets strategy access its data.

Strategy Pattern Class Diagram

Command:-
- used to implement loose coupling in a request-response model.


1. Client create receiver object and then attach it to command object.
2. A request is wrapped under an object as command and send to invoker object.
3. Invoker object pass it to encapsulated command object.
4. Command Object pass it to appropriate method of receiver object to perform specific action.



Command declares an interface for all commands, providing a simple execute() method which asks the Receiver of the command to carry out an operation. The Receiver has the knowledge of what to do to carry out the request.  The Invoker holds a command and can get the Command to execute a request by calling the execute method. The Client creates ConcreteCommands and sets a Receiver for the command. The ConcreteCommand defines a binding between the action and the receiver. When the Invoker calls execute the ConcreteCommand will run one or more actions on the Receiver

Observer:-
- defines a link between objects so that when one object's state changes, all dependent objects are updated automatically. This pattern allows communication between objects in a loosely coupled manner.

J2EE
Front controller:-
- all requests that come for a resource in an application will be handled by a single handler and then dispatched to the appropriate handler for that type of request. The front controller may use other helpers to achieve the dispatching mechanism.

DAO:-
 

No comments:

Post a Comment

Web Development

Design Phase:- Below all these represent different stages of the UX/UI design flow:- Wireframes represent a very basic & visual repr...