Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 556773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:57:19+00:00 2026-05-13T11:57:19+00:00

I have this problem where I need to design a Java package which is

  • 0

I have this problem where I need to design a Java package which is used for:

  • Getting data from different data sources. For example, Class A will retrieve customer data from an Oracle database, while Class B will retrieve the same information from a web service data source (via SOAP).
  • The results will need to be combined, the rule for combination is quite complex, so ideally I should hide this from the users (other developers) of this package.
  • When one data sources fails, I need to still return the result from other data sources. However, I also need to let the caller know one of the data sources failed to respond.

Right now I’m doing it by having a boolean value inside the Class A and Class B indicating whether there’s an error, and another object for storing the actual error message. The caller will have to check this boolean value after making a call to see whether an error has occurred.

What is a good design model for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:57:19+00:00Added an answer on May 13, 2026 at 11:57 am

    The answer would be very broad, so I would suggest you to use the:

    • The Data Access Object (DAO) design pattern to abstract the source of the data (database or webservice)
    • The strategy pattern to abstract the algorithm by which the data is merged (when both sources are available and one there is only one)
    • And finally the state design pattern to change the way your application works depending on which source is available.
    • All this wrapped (why not) in a nice facade.

    This psuedo code has similar syntax as UML and Python:

    // The data implements one interface
    Data {interface}
    
    // And you implement it with DatabaseData
    DbData -> Data
       ...
    
    // Or WebServiceData
    WsData -> Data
       ...
    
    // -- DAO part
    Dao {interface}
       + fetch(): Data[]
    
    // From database
    DatabaseDao -> Dao
        - data: Data[0..*]
        // Query database and create dbData from rows...
        + fetch(): Data[]
            self.status = "Not ok"
            self.status = connectToDb()
            if( self.status == ok ,
                performQuery()
                forEach( row in resultSet,
                    data.add( DbData.new( resultSet.next() ) )
                )
                disconnect()
            )
        ...
    
    // From web service
    WebServiceDao -> Dao
        - data: Data[0..*]
        // Execute remote method and create wsData from some strange object
        + fetch(): Data[]
            remoteObject: SoapObject = SoapObject()
            remoteObject.connect()
            if (remoteObject.connected?(),
                differentData: StrangeObject = remoteObject.getRemoteData()
                forEach( object in differentData ,
                    self.data.add( WsData.new( fromElement ))
                )
            ).else(
               self.status = "Disconnected"
            )
        ....
    // -- State part
    // Abstract the way the data is going to be retrieved
    // either from two sources or from a single one.
    FetcheState { abstract }
    
        - context: Service
        - dao: Dao // Used for a single source
    
        + doFetch(): Data[] { abstract }
    
        + setContext( context: Service )
            self.context = context
        + setSingleSource( dao: Dao)
            self.dao = dao
    
    // Fetches only from one DAO, and it doesn't quite merge anything
    // because there is only one source after all.
    OneSourceState -> FetcheState
       // Use the single DAO and fetch
       + doFetch(): Data[]
           data: Data[] =  self.dao.doFetch()
           // It doesn't hurt to call "context's" merger anyway.
           context.merger.merge( data, null )
    
    // Two sources, are more complex, fetches both DAOs, and validates error.
    // If one source had an error, it changes the "state" of the application (context),
    // so it can fetch from single source next time.
    TwoSourcesState -> FetcheState
        - db: Dao = DatabaseDao.new()
        - ws: Dao = WebServiceDao.new()
    
        + doFetch(): Data[]
            dbData: Data[] =  db.doFetch()
            wsData: Data[] =  ws.doFetch()
    
            if( ws.hadError() or db.hadError(),
                // Changes the context's state
                context.fetcher = OneSourceState.new()
                context.merger = OneKindMergeStrategy.new()
                context.fetcher.setContext( self.context )
                // Find out which one was broken
                if( ws.hadError(),
                    context.fetcher.setSingleSource( db )
                )
                if( db.hadError(),
                    context.fetcher.setSingleSource( ws )
                )
            )
            // Since we have the data already let's 
            // merge it with the "context's" merger.
            return context.merger.merge( dbData, wsData)
    
    // -- Strategy part --
    // Encapsulate algoritm to merge data
    Strategy{ interface }
        + merge( a: Data[], with : Data[]  )
    
    // One kind doesn't merge too much, just "cast" one array
    // because there is only one source after all.
    OneKindMergeStrategy -> Strategy
        + merge( a: Data[], b: Data[]  )
             mergedData: Data[]
             forEach( item, in( a ),
                mergedData = Data.new( item ) // Take values from wsData or dbData
             )
             return mergedData
    
    // Two kinds merge, encapsulate the complex algorithm to
    // merge data from two sources.
    TwoKindsMergeStrategy -> Strategy
        + merge( a: Data[], with: Data[] ): Data[]
            forEach( item, in( a ),
                mergedData: Data[]
                forEach( other, in(with ),
                     WsData wsData = WsData.cast( item )
                     DbData dbData = DbData.cast( other )
                     // Add strange and complex logic here.
                     newItem = Data.new()
                     if( wsData.name == dbData.column.name and etc. etc ,
                        newItem.name = wsData+dbData...e tc. etc
                        ...
                        mergedData.add( newItem )
                     )
                )
            )
            return mergedData
    
    // Finally, the service where the actual fetch is being performed.
    Service  { facade }
    
        - merger: Strategy
        - fetcher: FetcheState
    
        // Initialise the object with the default "strategy" and the default "state".
        + init()
            self.fetcher  = TwoSourcesState()
            self.merger = TwoKindsMergeStrategy()
            fetcher.setContext( self )
    
        // Nahh, just let the state do its work.
        + doFetch(): Data[]
            // Fetch using the current application state
            return fetcher.doFetch()
    

    Client usage:

         service: Service = Service.new()
         service.init()
         data: Data[] = service.doFetch()
    

    Unfortunately, it looks a bit complex.

    OOP is based a lot on polymorphism.

    So in Dao, you let the subclass fetch data from whatever place and you just call it dao.fetch().

    In Strategy the same, the subclass performs one algorithm or the other (to avoid having a lot of strange if‘s, else‘s, switch‘s, etc.).

    With State the same thing happens. Instead of going like:

    if isBroken and itDoesntWork() and if ImAlive()
    

    etc., etc. you just say, “Hey, this will be the code one. There are two connections and this is when there is only one.”.

    Finally, facade say to the client “Don’t worry, I’ll handle this.”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have solved this problem, I just need to know what to do. I
I need some help in solving this problem. We have a large amount of
I have been stumped for 3 hours now on this problem, I need to
The problem is this: I have multiple competing threads (100+) that need to access
I have a problem, I need to change body of method when this class
I have a problem with using facet. I need autocomplete and for this I
This is my first post here. I have a problem. I need to take
I have this problem: I want to generate a new source code file from
I have this general problem in design, refactoring or triage: I have an existing
I have a general OO design question that stems from a Hibernate Model. Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.