简体   繁体   中英

Strategy Pattern in LinkedList in java

I am trying to insert into doubly linked list by using strategy pattern. I wrote this code so does this follow pure strategy pattern in Java? or something I am missing here? Any suggestions will be appreciated.

Update Code:-

doubleLinkedList = new DoubleLinkedList(new ConcreteStrategyAdd());
doubleLinkedList.executeStrategy("john");
doubleLinkedList.executeStrategy("tom");
doubleLinkedList.executeStrategy("carls");

This is the class

class DoubleLinkedList {

 private Strategy strategy;

 private NewLink firstNode;
 private NewLink lastNode;
 private static NewLink rootNode;

 // Constructor
 public DoubleLinkedList(Strategy strategy) {
     this.strategy = strategy;
 }

 public void executeStrategy(String a) {
    strategy.execute(a, this);
 }


    // Initializing values in the Constructor for DoubleLinkedList
    public DoubleLinkedList() {

        rootNode  = null;
        firstNode = null;
        lastNode  = null;

    }

    public boolean isEmpty() {

        return rootNode == null;

    }


}

This is the interface for strategy

interface Strategy {
    void execute(String a, DoubleLinkedList list); 
}

//Implements the algorithm using the strategy interface

public class ConcreteStrategyAdd implements Strategy {

    DoubleLinkedList doubleLinkedList = new DoubleLinkedList();


    public void execute(String a, DoubleLinkedList list) {
        System.out.println("Called ConcreteStrategyAdd's execute()");

        //insert here by using the logic


 }


    }
}

I have updated the code, Let me know whether this is right or not?. But with this I am not sure where should I put my insertion method.

You're indeed using the strategy pattern, but your implementation is buggy: the strategy inserts items in its private, different list from the list containing the strategy.

The Strategy interface should look like this:

interface Strategy {
    void execute(String a, DoubleLinkedList list); 
}

The concrete implementation should look like this:

public class ConcreteStrategyAdd implements Strategy {

    public void execute(String a, DoubleLinkedList list) {
        System.out.println("Called ConcreteStrategyAdd's execute()");
        list.insertReverseAlphabeticalOrder(a);
    }
}

And the executeStrategy method should be

public void executeStrategy(String a) {
    strategy.execute(a, this);
}

Also, the goal of the strategy pattern is to be able to customize the behavior of some object without modifying this object's class directly. I think your DoubleLInkedList class shouldn't have an insertReverseAlphabeticalOrder method. It should just have an insert method, and the concrete strategy should implement the reverse alphabetical order logic, and then call insert() .

I don't know what this method does, so I will give you another example. Suppose that you want to insert "nhoJ" to the list when "John" is passed to the executeStrategy() method. This strategy could be called "ReverseStringInsertStrategy". Its implementation would be:

public void execute(String a, DoubleLinkedList list) {
    String reversed = reverseString(a);
    list.insert(reversed);
}

Now suppose that you want to insert "JOHN" to the list when "John" is passed to the executeStrategy() method. This strategy could be called "UppercaseStringInsertStrategy". Its implementation would be:

public void execute(String a, DoubleLinkedList list) {
    String uppercased = uppercaseString(a);
    list.insert(uppercased);
}

Yes it does. The strategy pattern, as its names implies, defines a strategy or, in other words a processing algorithm that can be used to execute a certain action without the stakeholder knowing what and how the algorithm works and this is exactly what you have implemented.

I would split the answer in two parts:

  • For what concerns the DoubleLinkedList class, I would say it is OK. You are effectively delegating a method exposed by the DoubleLinkedList to an underlying Strategy .
  • If I look at your implementation of the concrete Strategy, I would say it is not; the reason is that you still embed the insertion logic in the DoubleLinkedList. In addition to this, it must be the list to hold the strategy and not vice versa (see

    public class ConcreteStrategyAdd implements Strategy {

     DoubleLinkedList doubleLinkedList = new DoubleLinkedList(); 

Update: as per comments, I elaborate a bit further on my statement about the Strategy embodying the insertion logic.

A Strategy implementation should know how exactly where in the DoubleLinkedList an item shall be inserted (ordered, non-ordered, etc.) rather than simply calling back a method in the DoubleLinkedList. Otherwise, the strategy is still embodied in the DoubleLinkedList and you won't be able to hot-swap it without changing the DoubleLinkedList class, which is the main objective of this pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM