strategy-pattern

How to create a strategy pattern in Objective-C?

╄→гoц情女王★ 提交于 2019-12-02 18:28:00
I need to develop a strategy pattern where i have a main class with other three classes where i need to refer to the objects of the other three classes using the main class object.To solve this is the strategy pattern will help me? If so please do give me the syntax in Objective-C? You'll want to look at Objective-C's protocol mechanism. Here's a simple protocol with a single required method: @protocol Strategy <NSObject> @required - (void) execute; @end Then you declare a class that fulfills that protocol: @interface ConcreteStrategyA : NSObject <Strategy> { // ivars for A } @end The

what's the difference between the patterns Strategy, Visitor and Template Method?

风格不统一 提交于 2019-12-02 17:46:46
I'm in a class where we just learned about these design patterns. However I couldn't see any difference between them. They sound just like the same, creating concrete classes over the abstract one. Could somebody help me kill this doubt? thanks (: Both the visitor, the strategy, and the template pattern encompass the application of an algorithm. The biggest difference is in how they are evoked and how they are used in practice. While it may seem like they have the same use case, look at the construction of the objects to see the difference. The strategy pattern is often used when we don't have

Strategy Pattern V/S Decorator Pattern

安稳与你 提交于 2019-12-02 14:06:32
I just came across two patterns. Strategy Pattern Decorator Strategy Pattern :- Strategy pattern gives several algorithms that can be used to perform particular operation or task. Decorator Pattern :- Decorator pattern adds some functionality to component. In fact I have found that Strategy Pattern and Decorator Pattern can also be used interchangeably. Here is the link :- When and How Strategy pattern can be applied instead of decorator pattern? What is the difference between Strategy Pattern and Decorator Pattern? when Strategy Pattern should be used and when Decorator Pattern should be used

Can we assign / change traits to the scala class during runtime? How - any sample code? Like Strategy Pattern (of Gang of four Design Pattern)

不想你离开。 提交于 2019-12-02 05:57:12
问题 To explain my question: Class : Toy Trait1: Speak like Male Trait2: Speak like Female Can I change the behavior (traits) of Toy during runtime so sometimes the same object speaks like male and sometimes the same object speaks like female? I want to change the speaking behavior at runtime. 回答1: Scala really doesn't do that. There's Kevin Wright's autoproxy plugin which can do it, and you can instantiate and object with either trait, without that trait being part of the base class. I personally

Can we assign / change traits to the scala class during runtime? How - any sample code? Like Strategy Pattern (of Gang of four Design Pattern)

会有一股神秘感。 提交于 2019-12-02 02:09:49
To explain my question: Class : Toy Trait1: Speak like Male Trait2: Speak like Female Can I change the behavior (traits) of Toy during runtime so sometimes the same object speaks like male and sometimes the same object speaks like female? I want to change the speaking behavior at runtime. Scala really doesn't do that. There's Kevin Wright's autoproxy plugin which can do it, and you can instantiate and object with either trait, without that trait being part of the base class. I personally think that trying to accomplish things that way is to go against the grain of Scala: hard and prone to

How can I use a static method as a default parameter for the strategy design pattern?

白昼怎懂夜的黑 提交于 2019-12-01 17:34:56
I want to make a class that uses a strategy design pattern similar to this: class C: @staticmethod def default_concrete_strategy(): print("default") @staticmethod def other_concrete_strategy(): print("other") def __init__(self, strategy=C.default_concrete_strategy): self.strategy = strategy def execute(self): self.strategy() This gives the error: NameError: name 'C' is not defined Replacing strategy=C.default_concrete_strategy with strategy=default_concrete_strategy will work but, left as default, the strategy instance variable will be a static method object rather than a callable method.

php percentage chance

泄露秘密 提交于 2019-12-01 09:05:35
问题 This is really more a question of approach, but I'm presenting it in php. Suppose we had a list of four percentages that a give event will occur on iteration. array=('walk the dog'=>.25,'read the paper'=>.25,'drink coffee'=>.0,'listen to music'=>.50) (Keys are just tests - in practice this is going to be used in a strategy pattern that applies different methods to an object off chance using call_user_func() ) In a loop, what would be the best way to choose one of these events by random

Can't call static method from class as variable name?

巧了我就是萌 提交于 2019-12-01 04:51:43
I'm using php 5.2.6. I have a strategy pattern, and the strategies have a static method. In the class that actually implements one of the strategies, it gets the name of the strategy class to instantiate. However, I wanted to call one of the static methods before instantiation, like this: $strNameOfStrategyClass::staticMethod(); but it gives T_PAAMAYIM_NEKUDOTAYIM . $> cat test.php <? interface strategyInterface { public function execute(); public function getLog(); public static function getFormatString(); } class strategyA implements strategyInterface { public function execute() {} public

Best way to do this generic abstract class in c#?

放肆的年华 提交于 2019-11-30 18:53:07
I know I'm not doing this right, but I also know there is a way to do this. I'm trying to be as generic and abstract as possible, otherwise my code is going to get real messy. So I'm using strategy pattern here as hell which is the GetAggregateClient() method. I want to have an abstract class called AbstractAggregate, so that it uses generics. The type that will be used are a series of data classes which are BlogItem, ResourceItem, and AskItem. These data classes all inherit from ListItem. So that's the background info. The problem here is that I want GetAbstractAggregate() to return an

How to use the Strategy Pattern with C#?

梦想的初衷 提交于 2019-11-30 08:24:54
Here's what I have so far: namespace Strategy { interface IWeaponBehavior { void UseWeapon(); } } namespace Strategy { class Knife : IWeaponBehavior { public void UseWeapon() { Console.WriteLine("You used the knife to slash the enemy! SLASH SLASH!"); } } } namespace Strategy { class Pan : IWeaponBehavior { public void UseWeapon() { Console.WriteLine("You use the pan! 100% Adamantium power! BONG!"); } } } Now if I have a Character.cs superclass. how can that superclass implement a weaponbehavior so that children classes can be more specific. namespace Strategy { class Character { public