0

In this first attempt I'm trying to keep a reference of a variable in DialogBoxFactory by injecting either an int or string to an IDialogFooterText, and call 'GetFooterDisplay()' in my factory which then returns a string. I was simulating a dialog page increment and should receive an updated page index.

This didn't work properly (I then found a forum answer from Eric Lippert that outlined the reason why I couldn't store the ref parameter in the external class' method and tried another route.

My second attempt does in fact work but it seems dirtier since in DialogFooterPagenum I explicitly know the factory type of DialogBoxFactory. What are some other ways I can get the page index without directly referencing the factory instantiating either IDIalogFooterText?

    public class DialogBoxFactory 
    {

    public int _currentPageIndex = 0;

    private string[] _dialogText = new string[10]; //filled with dialog content

    //an example I've left to demonstrate the need for injecting different parameter types in their constructors
    //private IDialogFooterText = new DialogFooterText( "ok" );

    private IDialogFooterText  _footerText = new DialogFooterPagenum( ref _currentPageIndex, dialogText.Length );


    void Start(){

        _currentPageIndex +=1;

        Debug.Log( _footerText.GetFooterDisplay() );
        //expecting the log to show "1/10"

    }


}

public class DialogFooterPagenum : IDialogFooterText 
{

private int _pageIndex;
private int _dialogLength;

public DialogFooterPagenum( ref int p_pageIndex, int p_dialogLength ){

    _pageIndex = p_pageIndex;
    _dialogLength = p_dialogLength;

}

public string GetFooterDisplay(){

    return _pageIndex.ToString() + " /  " + _dialogLength;

}

}

SECOND ATTEMPTED VERSION - Reworked since the ref didn't work

public class DialogBoxFactory 
{

public int _currentPageIndex = 0;

private string[] _dialogText = new string[10]; //filled with dialog content

//an example I've left to demonstrate the need for injecting different parameter types in their constructors
//private IDialogFooterText = new DialogFooterText( "ok" );

private IDialogFooterText  _footerText = new DialogFooterPagenum( this, dialogText.Length );


void Start(){

    _currentPageIndex +=1;

    Debug.Log( _footerText.GetFooterDisplay() );
    //expecting the log to show "1/10"

}


}


 public class DialogFooterPagenum : IDialogFooterText 
 {

private int _dialogLength;
private DialogBoxFactory _factory;

//this works now but is unacceptable since I wish this class to be unaware of the factory instantiating this class.

public DialogFooterPagenum( DialogBoxFactory p_factory , int p_dialogLength ){

    _factory = p_factory;
    _dialogLength = p_dialogLength;

}

public string GetFooterDisplay(){

    return _factory._currentPageIndex.ToString() + " /  " + _dialogLength;

}

}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user2183858
  • 25
  • 1
  • 3
  • you can use event in factory for example `onChangePageIndex` and subscribe your `DialogFooterPagenum` on this event, or use `property` instead of `field` and in setter set `pageIndex` in `factory` and in `DialogFooterPagenum` – Grundy Dec 04 '13 at 05:57

2 Answers2

1

I can't see any reason to not to store current page index in IDialogFooterText implementations:

interface IDialogFooterText
{
    string GetFooterDisplay();
    int CurrentPageIndex { get; set; }
}

public class DialogBoxFactory
{
    IDialogFooterText _footerText = // ...

    void Start()
    {
        _footerText.CurrentPageIndex += 1;

        Debug.Log( _footerText.GetFooterDisplay() );
    }
}
Dennis
  • 37,026
  • 10
  • 82
  • 150
  • I see what you mean, in this case I don't want the CurrentPageIndex property for this reason: Some classes implementing **IDialogFooterText** won't need to include currentPageIndex, while in my original post it requires it. – user2183858 Dec 04 '13 at 06:51
  • Do you need to know current page index in factory class? – Dennis Dec 04 '13 at 07:06
  • Yes, I'll need to keep that index referenced in the factory, I'd like to also keep any of classes that implement IDialogFooterText decoupled from referencing the factory if I can. – user2183858 Dec 04 '13 at 07:16
0

I don't yet read your problem in depth. I can only see that you need referenced of mutable page index to DialogFooterPagenum. Is a PageIndex class can solve the problem?

class PageIndex{
    private int index;
    public int Index{get{return index;} 
        set{
            index = value;
            OnPageIndexChange();
        }
    }

    public Action OnPageIndexChange{get;set;} // change this to event-based
}

This way you can keep references to page index.

Fendy
  • 4,565
  • 1
  • 20
  • 25
  • what if `OnPageIndexChange` is null? :-) – Grundy Dec 04 '13 at 06:03
  • That's why I made the comment to change it to event-based to handle `null`-value and multiple delegation. However I forgot how to declare event-based in `C#`, that's why I used Action as illustration. – Fendy Dec 04 '13 at 06:05
  • i mean that in setter we need add check that `OnPageIndexChange` is not null :-) – Grundy Dec 04 '13 at 06:07
  • Null check at setter can do the job. But it still does not make it able to handle multiple delegation as event do. – Fendy Dec 04 '13 at 06:08