SADP Unit 3
SADP Unit 3
Software patterns are reusable solutions to recurring problems that occur during
software development.
In general, a pattern has four essential elements:
The pattern name is a handle we can use to describe a design problem, its
solutions, and consequences in a word or two. Naming a pattern immediately
increases our design vocabulary.
It lets us design at a higher level of abstraction. Having a vocabulary for patterns
lets us talk about them with our colleagues, in our documentation, and even to
ourselves. It makes it easier to think about designs and to communicate them and
their trade-offs to others. Finding good names has been one of the hardest parts of
developing our catalog.
The problem describes when to apply the pattern. It explains the problem and its
context. It might describe specific design problems such as how to represent
algorithms as objects. It might describe class or object structures that are
symptomatic of an inflexible design. Sometimes the problem will include a list of
conditions that must be met before it makes sense to apply the pattern.
The solution describes the elements that make up the design, their relationships,
responsibilities, and collaborations. The solution doesn't describe a particular
concrete design or implementation, because a pattern is like a template that can
be applied in many different situations. Instead, the pattern provides an abstract
description of a design problem and how a general arrangement of elements
(classes and objects in our case) solves it.
The consequences are the results and trade-offs of applying the pattern. Though
consequences are often unvoiced when we describe design decisions, they are
critical for evaluating design alternatives and for understanding the costs and
benefits of applying the pattern.
The consequences for software often concern space and time trade-offs. They may
address language and implementation issues as well. Since reuse is often a factor
in object-oriented design, the consequences of a pattern include its impact on a
system's flexibility, extensibility, or portability. Listing these consequences
explicitly helps you understand and evaluate them.
Design patterns vary in their granularity and level of abstraction. Because there
are many design patterns, we need away to organize them. This section classifies
design patterns so that we can refer to families of related patterns. The
classification helps you learn the patterns in the catalog faster, and it can direct
efforts to find new patterns as well.
We classify design patterns by two criteria.
The first criterion, called purpose, reflects what a pattern does. Patterns can have
either creational, structural, or behavioural purpose. Creational patterns concern
the process of object creation. Structural patterns deal with the composition of
classes or objects. Behavioral patterns characterize the ways in which classes or
objects interact and distribute responsibility.
The second criterion, called scope, specifies whether the pattern applies
primarily to classes or to objects. Class patterns deal with relationships between
classes and their subclasses. These relationships are established through
inheritance, so they are static -fixed at compile-time. Object patterns deal with
object relationships, which can be changed at run-time and are more dynamic.
Almost all patterns use inheritance to some extent. So, the only patterns labeled
"class patterns" are those that focus on class relationships. Note that most patterns
are in the Object scope
With more than 20 design patterns in the catalog to choose from, it might be hard
to find the one that addresses a particular design problem, especially if the catalog
is new and un familiar to you. Here are several different approaches to finding the
design pattern that's right for your problem:
Once you've picked a design pattern, how do you use it? Here’s a step-by-step
approach to applying a design pattern effectively:
1.Read the pattern once through for an overview. Pay particular attention to the
Applicability and Consequences sections to ensure the pattern is right for your
problem.
2.Go back and study the Structure, Participants, and Collaborations sections.
Make sure you understand the classes and objects in the pattern and how they
relate to one another.
3.Look at the Sample Code section to see a concrete example of the pattern in
code. Studying the code helps you learn how to implement the pattern.
4.Choose names for pattern participants that are meaningful in the
application context .The names for participants in design patterns are usually too
abstract to appear directly in an application. Nevertheless, it's useful to incorporate
the participant’s name into the name that appears in the application. That helps
make the pattern more explicit in the implementation. For example, if youuse the
Strategy pattern for a text compositing algorithm, then you might have classes
Simple Layout Strategy or TeXt Layout Strategy.
5.Define the classes. Declare their interfaces, establish their inheritance
relationships, and define the instance variables that represent data and object
references. Identify existing classes in your application that the patternwillaffect,
and modify them accordingly.
6.Define application-specific names for operations in the pattern. Here again,
the names generally depend on the application. Use the responsibilities and
collaborations associated with each operation as a guide.Also, be consistent in
your naming conventions. For example, you might use the "Create -" prefix
consistently to denote a factory method
7.Implement the operations to carry out the responsibilities and
collaborations in the pattern. The Implementation section offers hints to guide
you in the implementation. The examples in the Sample Code section can help as
well.
V.Software Design Pattern
In software engineering, creational design patterns are design patterns that deal
with object creation mechanisms, trying to create objects in a manner suitable
to the situation. The basic form of object creation could result in design
problems or added complexity to the design. Creational design patterns solve
this problem by somehow controlling this object creation.
The abstract factory pattern is a software design pattern that provides a way to
encapsulate a group of individual factories that have a common theme. In normal
usage, the client software creates a concrete implementation of the abstract
factory and then uses the generic interfaces to create the concrete objects that
are part of the theme. The client does not know (or care) which concrete objects
it gets from each of these internal factories, since it uses only the generic
interfaces of their products. This pattern separates the details of implementation
of a set of objects from their general usage.
The builder pattern is an object creation software design pattern. The intention
is to abstract steps of construction of objects so that different implementations of
these steps can construct different representations of objects. Often, the builder
pattern is used to build products in accordance with the composite pattern.
Model − the lowest level of the pattern which is responsible for maintaining data
View − this is responsible for displaying all or a portion of the data to the user
Controller − Software Code that controls the interactions between the Model and View
MVC is popular as it isolates the application logic from the user interface layer and supports
separation of concerns. Here the Controller receives all requests for the application and then
works with the Model to prepare any data needed by the View. The View then uses the data
prepared by the Controller to generate a final presentable response. The MVC abstraction can be
graphically represented as follows.
Organizing catalogs
Applicability
We should use Abstract Factory design pattern when:
The system needs to be independent of the products it works with are created.
The system should be configured to work with multiple families of products.
A family of products is designed to be used together and this constraint is needed to be
enforced.
A library of products is to be provided and only their interfaces are to be revealed but not
their implementations.
Structure
Participants: The classes that participate in the Abstract Factory are: AbstractFactory,
ConcreteFactory, AbstractProduct, Product, Client
Consequences
It isolates concrete classes.
It makes exchanging product families easy.
It creates consistency among products.
Implementation
abstract class AbstractProductA
{
public abstract void operationA1();
public abstract void operationA2();
}
Sample code
public interface Window
{
public void setTitle(String text);
public void repaint();
}
Known uses
ET++ [WGM88] uses the Abstract Factory pattern to achieve portability across different window
systems (X Windows and SunView, for example).
Related patterns
AbstractFactory classes are often implemented with factory methods but they can also be
implemented using Prototype.
A concrete factory is often a Singleton.
3. BUILDER
Pattern name and classification: Builder and Creational
Intent: Separate the construction of a complex object from its representation so that the same
construction process can create different representations.
Also known as
Motivation: Text Converter
Applicability
The algorithm for creating a complex object should be independent of the parts that make
up the object and how they are assembled.
The construction process must allow different representations for the object that is
constructed.
Structure
Implementation
//Abstract Builder
class abstract class TextConverter
{
abstract void convertCharacter(char c);
abstract void convertParagraph();
}
// Product
class ASCIIText
{
public void append(char c)
{ //Implement the code here }
}
Sample code
abstract class AbstractProduct implements Cloneable
{
public static AbstractProduct thePrototype;
public static AbstractProduct makeProduct()
{
try
{
return (AbstractProduct) thePrototype.clone();
}
catch(CloneNotSupportedException e)
{
return null;
}
}
}
Known uses
The RTF converter application is from ET++ [WGM88]. Its text building block uses a builder to
process text stored in the RTF format.
Related patterns
Abstract Factory (87) is similar to Builder in that it too may construct complex objects.
The primary difference is that the Builder pattern focuses on constructing a complex
object step by step.
A Composite (1 i6s3) what the builder often builds.
4. FACTORY METHOD
Related patterns
Abstract Factory is often implemented with factory methods. as well.
Factory methods are usually called within Template Methods
Prototypes don't require subclassing Creator.
5. PROTOTYPE
Applicability: Use the Prototype pattern when a system should be independent of how its
products are created, composed, and represented; and
when the classes to instantiate are specified at run-time, for example, by dynamic loading
; or
to avoid building a class hierarchy of factories that parallels the class hierarchy of
products; or
When instances of a class can have one of only a few different combinations of state. It
may be more convenient to install a corresponding number of prototypes and clone them
rather than instantiating the class manually, each time with the appropriate state.
Structure