CS代写 Property of Penn Engineering

Property of Penn Engineering

Strategy Pattern

Copyright By PowCoder代写 加微信 powcoder

Property of Penn Engineering

• Creational: how objects are created

• Structural: how objects are composed

• Behavioral: how objects communicate

Categories of Design Patterns

Property of Penn Engineering

• Creational: how objects are created

• Structural: how objects are composed

• Behavioral: how objects communicate

Categories of Design Patterns

Property of Penn Engineering

• Address problems such as:
• how do objects communicate?
• how are responsibilities assigned to different objects?

• Benefits: changeability, reusability

Behavioral Design Patterns

Property of Penn Engineering

• Problem: Want to use a general algorithm; some part of it may
vary depending on the context

• Concern: Want to avoid having multiple implementations of
the algorithm, each with slight differences

• Solution: Use a class that represents the strategy and pass an
instance to a single method that implements the rest of the

Design Problem

Property of Penn Engineering

public boolean searchByName(List dogs, Dog target) {
for (Dog d : dogs) {

if (d.name.equals(target.name)) return true;
return false;

public boolean searchById(List dogs, Dog target) {
for (Dog d : dogs) {

if (d.id == target.id) return true;
return false;

Searching for Dogs

Property of Penn Engineering

Property of Penn Engineering

Property of Penn Engineering

Property of Penn Engineering

public interface DogComparator {
public boolean equals(Dog d1, Dog d2);

Comparators

public class NameComparator implements DogComparator {
public boolean equals(Dog d1, Dog d2) {

return d1.name.equals(d2.name);

public class IdComparator implements DogComparator {
public boolean equals(Dog d1, Dog d2) {

return d1.id == d2.id;

Property of Penn Engineering

public boolean search(List dogs, Dog target,
DogComparator comparator) {

for (Dog d : dogs) {
if (comparator.equals(d, target))

return true;
return false;

Using the Comparator

Property of Penn Engineering

public boolean searchByName(List dogs, Dog target) {
for (Dog d : dogs) {

if (d.name.equals(target.name)) return true;
return false;

public boolean searchById(List dogs, Dog target) {
for (Dog d : dogs) {

if (d.id == target.id) return true;
return false;

Searching for Dogs (Original)

Property of Penn Engineering

public boolean searchByName(List dogs, Dog target) {

return search(dogs, target, new NameComparator());

public boolean searchById(List dogs, Dog target) {
for (Dog d : dogs) {

if (d.id == target.id) return true;
return false;

Searching for Dogs (Modified)

Property of Penn Engineering

public boolean searchByName(List dogs, Dog target) {

return search(dogs, target, new NameComparator());

public boolean searchById(List dogs, Dog target) {

return search(dogs, target, new IdComparator());

Searching for Dogs (Modified)

Property of Penn Engineering

• Behavioral design patterns: address issues around
communication and responsibilities between objects

• Strategy Pattern: create classes that can be used as part of a
larger algorithm
• Allows for more general algorithms with details filled in by

Strategy objects

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com