Design Pattern & Practices Interview Questions and Answers (103) - Page 2

What is the difference between Factory Pattern and Singleton Pattern?

Singleton pattern ensures only one instance of the type always be available for the client(s).Let us look into the below code

class Singleton


{

private static Singleton singletonInstance;

private Singleton(){}

public static Singleton CreateInstance()

{
return singletonInstance == null ? new Singleton() : singletonInstance;
}
}


The constructor is defined as private ensuring that no instance of the class can be created. Singleton keeps common data in only one place and this can be done by using the "static" keyword where the property "Singleton" is defined.Next checking if the property value is null or not.If yes then we are creating an instance and if no then we are returning back the old instance only.This is done inside the CreateInstance method The client invocation part is as under

var _singletonInstance = Singleton.CreateInstance();


The factory pattern deals with how an object is created. It gets classified under the creational pattern.We can go for a factory pattern when we need for

a) Object creation without exposing it to client

b) Refer to newly created objects through interface.

Factory pattern defines an interface for creating an object, but let the classes that implement the interface decide which class to instantiate.
What is the difference between Factory Pattern and Repository Pattern?

The factory pattern deals with how an object is created. It gets classified under the creational pattern.We can go for a factory pattern when we need for

a) Object creation without exposing it to client

b) Refer to newly created objects through interface.

Factory pattern defines an interface for creating an object, but let the classes that implement the interface decide which class to instantiate.

Repository pattern comes into use when we have (at a minimum) atleast one of the following requirements

a) We want to centrally manage the data by applying logic and reules while dealing with data that comes from varied location.

b) We need to use business entities that are strongly typed.These helps to find errors at comile time rather at runtime.

c) Isolate data layer for unit testing.

d) Want to have a caching mechanism in place to hold the data inorder to improve the application performance.

e) Seperate business logic and data layer logic for better code maintainability.
Why should we go ahead with a Layered architecture model?

- Layered Architecture helps to decompose an otherwise monolithic system into groups of components each placed at a certain layer.

- Layers also standardize the format, the content and the meaning of the messages that cross the layer boundary.

- Conceptualizes the whole system as stack of layers

- Each layer focuses on one specific responsibility

- Each layer consumes the services of the layers below it and offers services to the layers above it.

- Data flow is always linear, both upstream and downstream
List some of the Advantages of Layered Architecture.

Ex changeability
Allows swapping with an alternate implementation since communication between layers is through well defined protocols/interfaces
Standardization
Enables development of standardized tasks and interfaces since the levels of abstraction are clearly defined and commonly accepted
Localization of Dependencies
Standardized interfaces between layers confine the effect of change implementation to the layer that is changed
List some of the Dis-advantages of Layered Architecture.

Low Efficiency
Data transfer occurs through a number of intermediate layers. This is less efficient than a group of objects communicating directly with each other
Granularity of Layers
The decision about the granularity of layers and the assignments of tasks to the layers is difficult. Too few layers do not fully exploit the pattern and too many layers add overheard of data transfer.
Duplication
Work Services performed by lower layers may not be required by higher layers and those performed by higher layers may not be needed by the lower layers. The duplication has a negative impact on the performance of the system
In which situation we should go ahead for a MVC pattern?

- The pattern is applicable only in the context of interactive applications with human–computer interface

- MVC divides an interactive application into three areas Processing, Output, Model

- The Model is independent of specific input or output representations.

- The View component has more than one representation of a single model

- The Controller translates events to service requests for the model or display requests for the view
List some of the Advantages of MVC Architecture.

Multiple views of the same model

MVC separates the model views and allows multiple views to represent the same model simultaneously

Pluggable views and controllers

The conceptual separation of MVC allows exchange of view and controller objects of a model
List some of the Dis-advantages of MVC Architecture.

View and the Controller code breaks

Both view and controller components make direct calls to the model. If a change occurs in the model’s interface, this coupling increases the possibility of code breaks in the view and the controller code

Inefficiency of data access in View

The view at times might have to make multiple calls to obtain its display data and thereby weakens the performance of the system
Which situation we should dive for Event Driven Architecture?

- Deals with how the change in the state of one system is propagated to other systems in the application

- The subject has neither the knowledge the interested parties nor of the event’s subsequent processing

- Also known as Publish-Subscribe pattern
What are the participating components for Event Driven Architecture?

Generators / Subject / Publisher – The source of the event

Subscribers / Observer – Reaction in response to events

Dispatcher – Dispatches events using pattern matching, and correlation techniques

Channel – Component responsible for activities such as translation, filtering of event messages between event generators and event subscribers
List some of the Advantages of Event Driven Architecture

- Extremely loosely coupled architecture permits a lower level subject to communicate with a higher level observer without impacting the system’s layering

- Supports broadcast communication in a highly distributed architecture style

- New business services as event listeners can be invoked dynamically and transparently without making any changes to the event source itself.

- Event interceptors help make informed business decisions by providing insights into the system
List some of the Dis-advantages of Event Driven Architecture

- Event traceability through the system is difficult as the event can traverse in different directions

- Without complex update semantics, all changes in the subject are pushed to all observers regardless of their need, resulting in unnecessary broadcast traffic

- The observers have no knowledge of each other’s presence, so the final outcome of an event response can be hard to track down

- In distributed event architecture, the event context gets copied for each observer and reduces memory scalability
What are the drivers for Broker Architecture?

- Broker is a central piece of software that is used by several distributed components to communicate with each other.

- Individual components need not be equipped with different communication mechanism; the components only need the means to talk to the Broker.

- Broker also provides registry services for adding, removing, exchanging, activating and locating components.

- Client-side proxies represent a layer between clients and the Broker. This additional layer provides transparency, in that a remote object appears to the client as a local one.

- A Broker is analogous to a messenger that is responsible for the transmission of requests from clients to servers, as well as the transmission of responses and exceptions back to the client.
Who are the participating components in Broker Architecture?

Server
Objects that expose their functionality through interfaces conforming to Interface Definition Language (IDL) or through a binary standard specific for the Broker

Clients
Applications that access the services of at least one server by forwarding requests to the Broker

Proxies
Components that hide the communication details by exposing the same object oriented interface as client and server
Bridge
This is an optional component used for integrating two brokers across different heterogeneous network
List some advantages of Broker Architecture

Distribution transparency

A client can treat remote objects like a local object by using a client-side proxy as a layer between itself and the Broker (CORBA).

Centralization

Additional responsibilities such as Instance management, Security, Transaction Management etc can be centralized Ex:EJB Container
List some Dis-advantages of Broker Architecture

Complex Memory Management

Services referenced by external clients are managed by the Broker process and executed remotely in a separate process. The lifecycle of such service instances referenced by external clients makes memory management complex

Increased Overhead

Client-side proxies expose the same interface as the server object. Invocations that appear to be local, might actually be remote and carry overheads with regard to marshalling, network and so on

Portability Concerns

Broker architectures utilize non-standard generation of stub and proxies to implement the core connection and marshalling mechanism
List some of the characteristics of Pipes and Filter architecture

- The pattern is envisaged as a stream of data flowing through a set of processing stages.

- Data can be processed sequentially or in parallel, based on the context

- Pipes and Filters can be combined in different ways to create a family of systems

- Many integration architectural patterns are variants of the Pipes and Filter pattern.

- The key benefit of this architectural style is - The processing steps can be changed and composed in different ways as all the filters and pipes expose the same external interface.

e.g.
Many Enterprise Integration Patterns are based on Pipes and Filters Architecture Style
- Content-Based Router
- Splitter
- Aggregator
- ReSequencer
What are the main participating components of Pipes and Filter architecture

Filter

Enriches, refines or transforms input data. Each processing step is implemented by a filter component

Pipes

Implement the data flow between adjacent processing steps

Pipeline
A sequence of filters that are connected using pipes

Port
The connection between the filter and the pipe. In the basic form, each filter component has one input port and one output port
List some of the Advantages of Pipes and Filters.

- Allows complex processing on a message while maintaining independence and flexibility

- Filters that use the same external interface can be composed into different solutions, by connecting the components to different pipes, without having to change the filters themselves

- Filters are easy to reuse in different contexts than large components

- Multi-processing of filters, for example running them in parallel or quasi-parallel, is possible
List some of the Dis-Advantages of Pipes and Filters.

A) Increased overhead due to
•The small granularity of each processing step
•Number of context switch to be made between filters

B) Performance is affected by the need to copy data between address spaces of filters

C) Constraints on the allowed data size in the pipes (typically queues) affects the scalability of data transfers between filters
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories