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

  • SEARCH
  • Home
  • 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 7831823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:54:10+00:00 2026-06-02T11:54:10+00:00

I’ve simplified down the real code into the smallest example that illustrated the point.

  • 0

I’ve simplified down the real code into the smallest example that illustrated the point. Excuse the lack of setters/getters, etc.

Imagine we have a couple of web pages that a customer goes through in sequence. The use case here is:-

  • User selects which book they want
  • User selects if they want it sent via post or email and associated details
  • System fulfils the order

This quesiton is focused on the two ways of delivering. This is modelled as follows:

interface DeliveryDetails
{
    // Implementations of this have nothing in common other than that they
    // fulfil the same logical role.
}

class EmailDeliveryDetails implements DeliveryDetails
{
    String emailAddress;  // It really has a constructor and getter, I promise.
}

class PostalDeliveryDetails implements DeliveryDetails
{
    String streetAddress;
    String Country;
}

Now, to represent the information entered by the user when going through the pages, we have this class:

class PurchaseData
{
    String title;
    DeliveryDetails deliveryDetails;
}

As the user steps through the web pages, information is stored in an instance of PurchaseData. If the user goes back a page, we can show them what they previously entered. After the user has confirmed and it is time to deliver the book, deliveryDetails references an instance of PostalDeliveryDetails or EmailDeliveryDetails.

On conclusion, when the user confirms their information:

    // Some code in a factory
    if ( purchaseData.deliveryDetails instanceof EmailDelivery )
    {
        // construct a EmailDeliveryService( purchaseData, SMTP details, etc ... )
    }
    if ( purchaseData.deliveryDetails instanceof PostalDelivery )
    {
        // construct a PostalDeliveryService( purchaseData, etc ... )
    }
}

It troubles me that Delivery interface has no methods.

This is forced by the differences between email and postal delivery.

I do not think that DeliveryDetails.deliver() is a good method as this would force implementations to get things like the SMTP server address statically. This confuses concerns (plumbing vs. information entered by a user).

If you have to store something of arbitary type, generics can be useful. It is not possible to use generics (PurchaseData<T extends Delivery>) because the delivery type is not known when the PurchaseData instance is created. Anyway, this would not help in the factory.

Is this empty interface ok? Is there a better way to design this code?

  • 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-06-02T11:54:11+00:00Added an answer on June 2, 2026 at 11:54 am

    To me the (data) differences between EmailDeliveryDetails and PostalDeliveryDetails boil down to addresses. So my first instinct would be to extract that data into a separate Address class. Then you may decide to have a single Address with optional fields for emailAddress and streetAddress, or a class hierarchy with distinct subclasses for email and postal addresses.

    I would prefer the single class with optional fields, as it is cleaner to use, and to me practicality trumps conceptual "purity".

    Update

    Based on the comment chain below:

    When there is no overlap between the properties (thus the possible states) of specific classes, it is very awkward to try to handle them polymorphically. And if one doesn’t intend to put much functionality into them either, it is even more difficult to handle them in distinct classes inheriting some common interface (as you too noted). OTOH conceptually all of this is some sort of address data, so it can be handled in one class.

    Note that most of this is speculation though – it is difficult to reason about your design without more detailed information.

    You are right in that I don’t intend to put much behaviour in EmailDeliveryDetails and PostalDeliveryDetails. In the real application, these details would be persisted in the database and details sent through to outside systems.

    Ah OK, so you aren’t actually going to treat them polymorphically. You only need a common "handle" to access different bits of data to be persisted. And persistence typically doesn’t care about polymorphism and interfaces anyway. In this case having your classes inherit from an empty interface is fine.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a

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.