Note that there are some explanatory texts on larger screens.

plurals
  1. PODependency between components in Factory Pattern
    primarykey
    data
    text
    <p>Thank you for your time!</p> <p>Here is a problem which confused me a lot!It's about the Dependency between components in Factory Pattern.</p> <hr> <h2>Original Model</h2> <p><img src="https://i.stack.imgur.com/qYqiP.jpg" alt="Window Model in Factory Pattern"></p> <p>Just like the pic above,we got a Window Factory Model here.</p> <p>1.Interface: IFatory IWindow,IButton,ITextBox (model definition);</p> <p>2.Implement: WindowsFactory,MacFatory (to implement the interfaces);</p> <hr> <h2>New Model</h2> <p><img src="https://i.stack.imgur.com/j4lci.jpg" alt="New Model"></p> <p>Now,we have changed the old model.So the IWindow has two properties named "CloseButton" and "TitileBox".Problems come out:</p> <p>1.When and how to implement these two properties?</p> <p>2.These two properties should have the same style with their container window,which means we can only have WindowsStyle(Window/Button/TextBox) or MacStyle(Window/Button/TextBox).No mixing!</p> <hr> <p>To solve these problems,I had got some trials.Please check the code below.</p> <h2>Solution 1</h2> <pre><code>class MacWindow:IWindow { public IButton CloseButton {get;private set;} public ITextBox TitleBox {get;private set;} public MacWindow() { this.CreateCloseButton(); this.CreateTitleBox(); } protected virtual void CreateCloseButton() { CloseButton = new MacButton(); } protected virtual void CreateTitleBox() { TitleBox = new MacTextBox(); } } </code></pre> <p>As you see,this solution is not good.</p> <p>1.Class MacWindow has to rely on the specific IButton/ITextBox implements (although they are in the same Factory).It feels bad!</p> <p>2.If someday,we get a new Class derived from the Class MacWindow,we have to override those virtual methods to keep them in the same style.</p> <hr> <h2>Solution 2</h2> <pre><code>class MacWindow:IWindow { public IFactory ActiveFactory{get;private set;} public IButton CloseButton {get;private set;} public ITextBox TitleBox {get;private set;} public MacWindow(IFactory factoy) { this.ActiveFactory=factory; this.CreateCloseButton(); this.CreateTitleBox(); } private void CreateCloseButton() { CloseButton = ActiveFactory.MakeButton(); } private void CreateTitleBox() { TitleBox = ActiveFactory.MakeTextBox(); } } </code></pre> <p>This one looks better,but now quite perfect.</p> <p>1.Now works are much easier to do ,if we got a new factory derived from MacFactory which is named MacOSXFactory,the only thing we need to do is to override IFactory.MakeButton() and IFactory.MakeTextBox() methods;(er..those methods should be definited as 'public virtual')</p> <p>2.The dependency on specific implement is no longer existed,but the window class have to inject the dependency on IFactory. It doesn't feel good to have a product known the details about the Factory.</p> <hr> <p>So here is the puzzle! Which solution is better? And is there any other way to solve this problem?(Eager to know!Really thanks!) Could you please give me some advices?</p> <h2>Acknowledgment</h2> <p>Please allow me to say "Thank you so much!" for your time to read the question!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload