java - How to design an object which can be one of two types? -


I am reading an HTTP post and the main request for the HTTP request can be either JSON or XML Now I have a special utility The class has the right to read.

  Interface HTTP Youths {BodyWeeParseBody (HTTP Servet Rev. Rake); } Is the interface bodybuilder {Boolean ISXML}; // 1 boolean isJSON (); // 2 string body (); // 3}  
  • I hate the fact that BodyWrapper's methods (1 & amp; 2) are for identifying its type. Maybe I use heritage If I do this, then I have the HTTP Youths. An installation will be required to find out if the PRSBD (..)
  • is being returned. Ideally I want body too () method, or how do I either JSONObject or a dome node?
  • Do not ask for your objects for information, and then those who tell you, Make decisions about your things that work for you, do not do this:

      if (body.isXML ()) {// do XML content} and if (body .isJSON ()) {// JSON stuff}}  

    This is a maintenance headache. Instead, do something like this (BodyDrap implementation was made using one or the same)

      Public Interface BodyWrapper {Object DustFool (); } Applies to the public class domody vapor BodyWrapper {public object dustf {}} Public class JasonBodyVipar object bodydropper {public object dotflu} {// do something and get success / failure results I have not defined // I purposely What is this object ....}}  

    And then:

      // Get the body through a factory or similar body. Dust stick ();  

    In this way, makes some appropriate bodywrapper implementation, and then instead of asking how it is, you use it Note that BodyWrapper is not returning to different types of internal structures, because it (probably an abstract base class) is working for you.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -