Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>it make better sense to pass parameters </p> </blockquote> <p>In your example I think it does, but design intent clarifies the issue.</p> <ul> <li>Allow maximum flexibility. This sacrifices encapsulation to some degree and is potentially more error prone for the client code.</li> <li>Is the <code>Class1</code> object immutable? I.E. does changing the current <code>URL</code> or <code>Proxy</code> of an existing <code>HttpRequest</code> make sense vice an all new <code>HttpRequest</code>?</li> <li><em>Guarantee</em> a valid object state</li> </ul> <p>Make a <code>HttpRequestBase</code> constructor requiring 2 parameters - the <code>URL</code> and <code>Proxy</code>.</p> <pre><code>public HttpRequestBase (string url, IWebProxy proxy) { this.Proxy = null; this.URL = url; // how to handle null parameters? make defaults? throw exception? other? } </code></pre> <p><strong>Why a constructor?</strong> </p> <p>If <code>URL</code> and <code>Proxy</code> are central/essential to a (derived) <code>HttpRequest</code> - if the notion of a <code>HttpRequest</code> existing without them does not make sense then constructor parameters enforces that idea.</p> <p>If you don't want the client to arbitrarily change the object's <code>URL</code> and/or <code>Proxy</code> after instantiation.</p> <p>Why put this constructor at the base of the inheritance chain? Same reasoning.</p> <p>Why this instead of <code>Request()</code> method parameters? Same reasoning.</p> <p><strong>Parameters make Flexible design</strong></p> <p>As it is you "new-up" a proxy inside <code>HttpRequest</code>. This <em>tightly couples</em> <code>HttpRequest</code> to that proxy. I.E. every instance of <code>HttpRequest</code> must and will have the same proxy (state). What if that's not what the client code wants? What if you want to unit test (and you should) and you need to <em>inject</em> proxies and urls for testing? Instead let (force) the user to <em>inject</em> it via the constructor. Further, even if you intended this to be a default object (your public property <code>Proxy</code> leads me to this guess) <em>loose coupling</em> is more desirable.</p> <p><strong>Encapsulation</strong></p> <p><em>Encapsulation</em> means that the client code does not need to know details of the class he's using. But in this case the client needs to know a whole lot about setting url and proxy. Does order matter? Why should he set proxy to null? Does he have to? Why does he have to? Does setting anything matter depending on what method he's calling? What must be set in order to call a particular method? blah, blah, blah.</p> <blockquote> <p>what is the purpose of the shared properties if i am not going to use them ?</p> </blockquote> <p>If you have no purpose for them, don't use them. But again, this goes to design intent.</p> <p>By "shared" I assume you mean <code>public</code>. In generic terms, to allow client code to customize the object (the object's state) at will. To the extent that you must not allow the client to shoot himself in the foot, don't. </p> <p>Your intent here may be for the client to set these values immediately after instantiation; or to never change them once set, but programming is not rainbows and unicorns. You need to bullet-proof your design as much as practical.</p> <p>As far as <code>private</code> or <code>protected</code> fields are concerned; to the extent that these are part of the essential being of a class then that's good. W/in that class they are simply there and passing these as "internal" method parameters is not as imperative as the public API.</p>
    singulars
    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.
    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