0% found this document useful (0 votes)
48 views7 pages

School of Computing and Informatics Department of Information Technology

This document discusses the strategy design pattern. It defines the strategy pattern as abstracting an algorithm into its own class so the algorithm can be changed independently of the context class that uses it. The document then provides an example implementation of the strategy pattern with an interface defining an action and concrete strategy classes that implement the interface and provide different algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views7 pages

School of Computing and Informatics Department of Information Technology

This document discusses the strategy design pattern. It defines the strategy pattern as abstracting an algorithm into its own class so the algorithm can be changed independently of the context class that uses it. The document then provides an example implementation of the strategy pattern with an interface defining an action and concrete strategy classes that implement the interface and provide different algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

GROUP ASSIGNMENT OF INTEGRATIVE PROGRAMMING AND TECHNOLOGY ITec 4131

School of Computing and Informatics

Department of Information Technology

Group Assignment of Integrative Programming and Technology

Section one Group 4 Course Code: ITec 4131

Title: Definition of types of design pattern and strategy design pattern

Group member

Submission Date: 25/12/2017 Submitted to: Mr.Bharat

DEPARTMENT OF INFORMATION TECHNOLOGY 4th Year


1
GROUP ASSIGNMENT OF INTEGRATIVE PROGRAMMING AND TECHNOLOGY ITec 4131

Q1.List and define all the design patterns as much as possible.

Design pattern:-

 A standard solution to a common programming problem.


 Techniques for making code more flexible by making it meet certain criteria.
 A design or implementation structure that achieves a particular purpose.
 A high-level programming idiom.
 Shorthand for describing certain aspects of program organization
 Connections among program components
 the shape of an object diagram or object model

There are three categories of Design Patterns

1. Creational DP vary object creation (Five DP)

2. Behavior DP vary the behavior you want (Seven DP)

3. Structural DP vary object structure (Eleven DP)

1. Creational Design Patterns:-

 Deal with one of the most commonly performed tasks in an OO application, the creation of objects.

• Factory Method Design Pattern

 Method in a derived class creates associates.


 Creates an instance of several derived classes
 Define an interface for creating an object, but let subclasses decide which class to instantiate.
 Factory Method lets a class defer instantiation to subclasses.
 Defining a "virtual" constructor.

• Abstract Factory Design Pattern

 Factory for building related objects.


 Creates an instance of several families of classes

• Builder Design Pattern

 Factory for building complex objects incrementally.


 Separates object construction from its representation
 Parse a complex representation, create one of several targets.

• Prototype Design Pattern

 Factory for cloning new instances from a prototype.


 A fully initialized instance to be copied or cloned.
 Co-opt one instance of a class for use as a breeder of all future instances.
 The new operator considered harmful.

DEPARTMENT OF INFORMATION TECHNOLOGY 4th Year


2
GROUP ASSIGNMENT OF INTEGRATIVE PROGRAMMING AND TECHNOLOGY ITec 4131

• Singleton Design Pattern

 Factory for a singular (sole) instance.


 A class of which only a single instance can exist.
 Ensure a class has only one instance, and provide a global point of access to it.
 Encapsulated "just-in-time initialization" or "initialization on first use".

2. Structural Design Patterns:-

• Adapter Design Pattern

 Translator adapts a server interface for a client.


 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

• Bridge Design Pattern

 Abstraction for binding one of many implementations.


 Decouple an abstraction from its implementation so that the two can vary independently.
 Publish interface in an inheritance hierarchy, and bury implementation in its own inheritance hierarchy.
 Beyond encapsulation, to insulation.

• Composite Design Pattern

 Structure for building recursive aggregations.


 Compose objects into tree structures to represent whole-part hierarchies.
 Composite lets clients treat individual objects and compositions of objects uniformly.
 Recursive composition.
 Directories contain entries, each of which could be a directory.

• Decorator Design Pattern

 Decorator extends an object transparently.


 Attach additional responsibilities to an object dynamically.
 Decorators provide a flexible alternative to sub classing for extending functionality.

• Façade Design Pattern

 Facade simplifies the interface for a subsystem.


 Clients of a subsystem may need to interact with a number of subsystem classes.

• Flyweight Design Pattern

 Flyweight pattern is primarily used to reduce the number of objects created and to decrease memory
footprint and increase performance.
 Flyweight pattern tries to reuse already existing similar kind objects by storing them and creates new
object when no matching object is found.

• Proxy Design Pattern

 In proxy pattern, a class represents functionality of another class.


 In proxy pattern, we create object having original object to interface its functionality to outer world .

DEPARTMENT OF INFORMATION TECHNOLOGY 4th Year


3
GROUP ASSIGNMENT OF INTEGRATIVE PROGRAMMING AND TECHNOLOGY ITec 4131

3. Behavioral Design Patterns:-

• Chain of Responsibility Design Pattern

 Request delegated to the responsible service provider.


 A way of passing a request between chains of objects.

• Command Design Pattern

 Request as first-class object.


 Encapsulate a command request as an object.

• Interpreter Design Pattern

 Language interpreter for a small grammar.


 A way to include language elements in a program.

• Iterator Design Pattern

 Aggregate elements are accessed sequentially.


 Sequentially access the elements of a collection.

• Mediator Design Pattern

 Mediator coordinates interactions between its associates.


 Defines simplified communication between classes.

• Memento Design Pattern

 Snapshot captures and restores object states privately.


 Capture and restore an object's internal state.

• Observer Design Pattern

 Dependents update automatically when a subject changes.


 A way of notifying change to a number of classes.

• State Design Pattern

 Object whose behavior depends on its state.


 Alter an object's behavior when its state changes.

• Strategy Design Pattern

 Abstraction for selecting one of many algorithms.


 Encapsulates an algorithm inside a class.

• Template Method Design Pattern

 Algorithm with some steps supplied by a derived class.


 Defer the exact steps of an algorithm to a subclass.

• Visitor Design Pattern

 Operations applied to elements of a heterogeneous object structure.


 Defines a new operation to a class without change.

DEPARTMENT OF INFORMATION TECHNOLOGY 4th Year


4
GROUP ASSIGNMENT OF INTEGRATIVE PROGRAMMING AND TECHNOLOGY ITec 4131

Q2 Define Strategy Design Pattern with some example and coding:-

This question number two is assigned to group four and we will something about this as much as possible. In
Strategy pattern, a class behavior or its algorithm can be changed at run time. This type of design pattern comes
under behavior pattern. In Strategy pattern, we create objects which represent various strategies and a context object
whose behavior varies as per its strategy object.The strategy object changes the executing algorithms of the context
object.

Code for the example of strategy design pattern below.

Implementation

We are going to create the strategy interface defining an action and concrete strategy classesimplements the strategy
interface. Context is a class which uses a strategy.StrategyPatternDemo, our demo class, will use Context and
strategy objects to demonstrate change in Context behavior based on strategy it deploys or uses.

DEPARTMENT OF INFORMATION TECHNOLOGY 4th Year


5
GROUP ASSIGNMENT OF INTEGRATIVE PROGRAMMING AND TECHNOLOGY ITec 4131

/********************************************************************/

Codes for this design pattern based on the above structure

Step 1
Create an interface.

Strategy.java

PublicinterfaceStrategy {publicintdoOperation (int


num1, int num2) ;}

Step 2
Create concrete classes implementing the same interface.

OperationAdd.java

Publicclassoperation AddimplementsStrategy {
@Override publicintdo Operation (int num1, int num2) {return num1 + num2;
}
} OperationSubstract.java

PublicclassOperationSubstractimplementsStrategy {
@Override
PublicintdoOperation (int num1, int num2) {return num1 - num2;
}
}

OperationMultiply.java

PublicclassOperationMultiplyimplementsStrategy {
@Override
PublicintdoOperation (int num1, int num2) {return num1 * num2;
}
}

Step 3
Create Context Class.

Context.java
publicclassContext{ private Strategy
strategy;

Public Context (Strategy strategy) {this. Strategy= strategy;


}

publicintexecute Strategy(int num1,int num2){ return


strategy.doOperation(num1, num2);
}}

DEPARTMENT OF INFORMATION TECHNOLOGY 4th Year


6
GROUP ASSIGNMENT OF INTEGRATIVE PROGRAMMING AND TECHNOLOGY ITec 4131

Step 4
Use the Context to see change in behavior when it changes its Strategy.

StrategyPatternDemo.java

PublicclassStrategy Pattern Demo {publicstaticvoidmain (String [] args) {


Context context =newContext (newOperation Add ());
System.out.println ("10 + 5 = "+context.executeStrategy (10, 5));

Context=newContext (newOperationSubstract ());


System.out.println ("10 - 5 = "+context.executeStrategy (10, 5));

Context=newContext (newOperation Multiply ());


System.out.println ("10 * 5 = "+context.executeStrategy (10, 5));
}
}

Step 5
Verify the output.

10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
Summary

• Strategy pattern allows selection of one of several algorithms dynamically.

• Algorithms may be related via an inheritance hierarchy or unrelated [must implement the same interface].

• Strategies don’t hide everything -- client code is typically aware that there are a number of strategies and
has some criteria to choose among them -- shifts the algorithm decision to the client.

DEPARTMENT OF INFORMATION TECHNOLOGY 4th Year


7

You might also like