dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Achieving State like pattern without having +3 entities?

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    3

    Achieving State like pattern without having +3 entities?

    Here is the design problem in pseudo example:

    It is ASP.NET User control (UC) which uses the "Food" object as data source and this object has property FoodType - "Veg" || "Nonveg".

    The user control changes UI display depending upon FoodType property. In code-behind class of User Control, some methods have same if/then/else condition: if(Food.FoodType == "Veg")... else ...

    Here, I would like use State-like pattern such that code-behind class of User Control will contain two inner/nested classes, i.e. "VegFood" class and "NonvegFood" class. Now, lets say, NonvegFood class will contain its own logic implementation.

    But having one interface, two instance classes (as nested) which will be used in this user control only, could be overdesign. In addition to that, UC does not have driver method where I can call related methods in one if block, like they are "DataBound" and "ItemCreated".

    Still, is it possible to have State like pattern inside UC class? Probably two instance inner classes inside UC and somehow if I can delegate call to one of these inner class?

    class UC : UserControl

    {


    class VegFood
    {
    string DisplayName
    {
    get
    {
    return "Rice";
    }
    }
    }

    class NonvegFood
    {
    string DisplayName
    {
    get
    {
    return "Chicken";
    }
    }
    }

    protected string DisplayName
    {
    get
    {
    return Instance.DisplayName;
    }
    }

    /*********** MAGIC Property ****************/
    private ? Instance
    {
    get
    {
    return ?;
    }
    }
    }

  2. #2
    Join Date
    Jul 2006
    Posts
    297

    Re: Achieving State like pattern without having +3 entities?

    Something like this?

    Code:
    public interface IDisplayName
    {
        String DisplayName { get; }
    }
    
    public class VegFood : IDisplayName
    {
        public string DisplayName
        {
            get
            {
                return "Rice";
            }
        }
    }
    
    public class NonVegFood : IDisplayName
    {
        public string DisplayName
        {
            get 
            {
                return "Chicken";    
            }
        }
    }
    
    protected String DisplayName
    {
        get
        {
            return Instance.DisplayName;
        }
    }
    
    private IDisplayName Instance
    {
        get
        {
            // Return here whatever class you need.
            return new VegFood();
        }
    }

  3. #3
    Join Date
    Aug 2009
    Posts
    3

    Re: Achieving State like pattern without having +3 entities?

    Quote Originally Posted by monalin View Post
    Something like this?

    Code:
    public interface IDisplayName
    {
        String DisplayName { get; }
    }
    
    public class VegFood : IDisplayName
    {
        public string DisplayName
        {
            get
            {
                return "Rice";
            }
        }
    }
    
    public class NonVegFood : IDisplayName
    {
        public string DisplayName
        {
            get 
            {
                return "Chicken";    
            }
        }
    }
    
    protected String DisplayName
    {
        get
        {
            return Instance.DisplayName;
        }
    }
    
    private IDisplayName Instance
    {
        get
        {
            // Return here whatever class you need.
            return new VegFood();
        }
    }
    Thanks for reply.

    But as mentioned in title and description "Achieving State like pattern without having +3 entities?", I am looking for alternate of this State Pattern.

    A User Control class having (small) class hierarchy inside class, hmmm.... That is why looking for an alternate. These extra entities are not going to be used anywhere else, so does not make sense to put anywhere else and so should be inner classes of User Control UC

  4. #4
    Join Date
    Jul 2006
    Posts
    297

    Re: Achieving State like pattern without having +3 entities?

    The only way I know of to avoid doing something like

    Code:
    if(Food.FoodType == FoodType.VegFood)
    Is to use the method you posted above. Where you have two classes that share the same interface. I'm not sure you can get around this without having 2 classes and an interface/class to inherit from.

  5. #5
    Join Date
    Aug 2009
    Posts
    3

    Re: Achieving State like pattern without having +3 entities?

    Not sure but was thinking that it could be possible using new constructs like anonymous delegate or Func<> or generics or may be FP style???

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)