问题描述
我已经看到策略对象经常使得优秀的flyweights(从可重用的面向对象软件的设计模式元素),我想知道如何实现。我没有在互联网上找到任何例子。
I've read that "Strategy objects often make good flyweights" (from Design Patterns Elements of Reusable Object-Oriented Software), and I'm wondering how can this be implemented. I didn't find any example in the Internet.
下面的代码(C#)是否正确,遵循这个想法?
Is the code (C#) below right, following this idea?
谢谢!
using System;
using System.Collections.Generic;
namespace StrategyFlyweight
{
class Program
{
static void Main(string[] args)
{
Client client = new Client();
for(int i = 1; i <= 10;i++)
{
client.Execute(i);
}
Console.ReadKey();
}
}
public interface IStrategy
{
void Check(int number);
}
public class ConcreteStrategyEven : IStrategy
{
public void Check(int number)
{
Console.WriteLine("{0} is an even number...", number);
}
}
public class ConcreteStrategyOdd : IStrategy
{
public void Check(int number)
{
Console.WriteLine("{0} is an odd number...", number);
}
}
public class FlyweightFactory
{
private Dictionary<string, IStrategy> _sharedObjects = new Dictionary<string, IStrategy>();
public IStrategy GetObject(int param)
{
string key = (param % 2 == 0) ? "even" : "odd";
if (_sharedObjects.ContainsKey(key))
return _sharedObjects[key];
else
{
IStrategy strategy = null;
switch (key)
{
case "even":
strategy = new ConcreteStrategyEven();
break;
case "odd":
strategy = new ConcreteStrategyOdd();
break;
}
_sharedObjects.Add(key, strategy);
return strategy;
}
}
}
public class Client
{
private IStrategy _strategy;
private FlyweightFactory _flyweightFactory = new FlyweightFactory();
public void Execute(int param)
{
ChangeStrategy(param);
_strategy.Check(param);
}
private void ChangeStrategy(int param)
{
_strategy = _flyweightFactory.GetObject(param);
}
}
}
推荐答案
我认为要在这里正确实现flyweight模式,您的工厂方法应该始终返回与特定策略相同的实例(例如 ConcreteStrategyEven
),而不是构建新的
I think that to properly implement the flyweight pattern here, your factory method should always return the same instance of a particular strategy (like ConcreteStrategyEven
) rather than constructing a new instance each time.
如果我没有误会,那么说Strategy对象可以做出好的Flyweights的一点是,他们经常封装没有状态(因为它们代表算法而不是实体),并可以重复使用。
If I'm not mistaken, the point of saying that Strategy objects make good Flyweights is that they often encapsulate no state (since they represent algorithms rather than entities) and can be reused.
这是一个链接到Flyweight工厂的示例:。注意这一部分,特别是:
Here is a link to an example of a Flyweight factory: http://www.java2s.com/Code/Java/Design-Pattern/FlyweightFactory.htm. Note this part, in particular:
public synchronized FlyweightIntr getFlyweight(String divisionName) {
if (lstFlyweight.get(divisionName) == null) {
FlyweightIntr fw = new Flyweight(divisionName);
lstFlyweight.put(divisionName, fw);
return fw;
} else {
return (FlyweightIntr) lstFlyweight.get(divisionName);
}
}
在工厂方法中,一个新的 FlyweightIntr
仅在没有正确的情况下初始化;否则将从 lstFlyweight
中检索。
Here in the factory method, a new FlyweightIntr
is only initialized if the correct one is not available; otherwise it is retrieved from lstFlyweight
.
这篇关于战略和飞行模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!