结合综合与战略

eou*_*uti 2 design-patterns

我正在寻找一个示例,展示如何组合这两种设计模式(策略和复合).我知道如何使用策略,但复合材料对我来说还不够清楚,所以我无法真正看到如何将它们结合起来.有人有例子还是smthg?
干杯

Sky*_*ker 8

确定这是一种突然出现的方法(在伪Java代码中):

interface TradingStrategy {
    void buy();
    void sell();   
}

class HedgingLongTermStrategy implements TradingStrategy {
    void buy() { /* TODO: */ };
    void sell() { /* TODO: */ };   
}

class HighFreqIntradayStrategy implements TradingStrategy {
    void buy() { /* TODO: */ };
    void sell() { /* TODO: */ };   
}

class CompositeTradingStrategy extends ArrayList<TradingStrategy> implements TradingStrategy {
    void buy() {
       for (TradingStrategy strategy : this) {
           strategy.buy();
       }
    }
    void sell() {
       for (TradingStrategy strategy : this) {
           strategy.sell();
       }
    }
}

// sample code
TradingStrategy composite = new CompositeTradingStrategy();
composite.add(new HighFreqIntradayStrategy());  
composite.add(new HedgingLongTermStrategy());
composite.buy();
Run Code Online (Sandbox Code Playgroud)