Strategy Design Pattern ( Software Engineering )

  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Strategy Design Pattern ( Software Engineering ) as PDF for free.

More details

  • Words: 456
  • Pages: 12
Strategy Design Pattern Technical University­Berlin  Winter Semester 2008 ­ 2009 M.Shafi Tokhi

Design pattern ●



Design patterns are common solutions for software problems. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. (Wikipedia)

Design Patterns ●

Strategy Design Pattern



Decorator Design Pattern



Factory Design Pattern



Observer Design Pattern

Strategy Design Pattern ●



is a particular software design pattern, whereby algorithms can be selected at runtime. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable

public abstract class Vehicle{ public Vehicle(){ } public void go(){ System.out.println(”I am driving Now ”); } } public class BMW extends Vehicle{ public BMW(){ } }

public static void main(String[] args){ BMW bmv = new BMW(); bmW.go(); . . . }

I am driving Now

Problem: What Happen if we have some other vehicles with driving , Flying, Flying Fast behaviours?

Overview of Strategy Design pattern doTask() { }

doTask() { overriding code }

doTask() { more overriding code }

Solution: Strategy Design Pattern

Strategy Design Pattern Your code

Algorithm object 1 Algorithm object 2

Algorithm Variable

Algorithm object 3 Algorithm object 4 Algorithm object 5

public interface GoAlgorithm{ public void go(); } public class GoByDrivingAlgorithm implements GoAlgorithm{ public void go(){ System.out.println("I am driving now..... "); } } public class GoByFlyingAlgorithm implements GoAlgorithm{ public void go(){ System.out.println("I am flying ... "); } } public class GoByFlyingFast implements GoAlgorithm{ public void go(){ System.out.println("I am flying fast... "); } }

public abstract class Vehicle{ private GoAlgorithm goAlgorithm; public Vehicle(){ } public void setGoAlgorithm(GoAlgorithm algorithm){ goAlgorithm = algorithm; } public void go(){ goAlgorithm.go(); } }

public class BMW extends Vehicle{

}

public BMW(){ setGoAlgorithm(new GoByDrivingAlgorithm()); }

public class MercedesBenz extends Vehicle{

}

public MercedesBenz(){ setGoAlgorithm(new GoByDrivingAlgorithm()); }

public class Helicopter extends Vehicle{ public Helicopter(){ setGoAlgorithm(new GoByFlyingAlgorithm()); } } public class Jet extends Vehicle{ public Jet(){ setGoAlgorithm(new GoByFlyingFast()); } }

Testing.. public class Run{ public static void main(String[] args){ Vehicle v; System.out.println("starting..."); v = new MercedesBenz(); v.go(); v = new BMW(); v.go(); v = new Helicopter(); v.go(); v = new Jet(); v.go(); } }

Task For You! (5 Marks)





What are design pattrens and why we use them? (write what you have learned from this presentation) What is Strategy design pattern? Explain what you have learned so far.

Related Documents