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 501277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:10:23+00:00 2026-05-13T06:10:23+00:00

i am a newbie to the design patterns and here is my question if

  • 0

i am a newbie to the design patterns and here is my question

if we have an abstract class with few classes that implement it and each of this classes has different attributes.

now i have another (Manager class) that holds an array of the abstract class and i want to put a search method in it … how can i do that without casting to the concrete classes ?

i have got 2 ideas:

First One : adding an extra levels of interfaces (ie. rather than casting to concrete class i will be casting to an interface ) which goes with the code to interface not implementation rule…
but this way when i add another class i will have to make an interface for it and i will also have to edit the manager (client) , which doesnt seem very good.

Second Solution:
it look somewhat strange and still needs enhancements but its main goal is to make the manager or any other client works with abstract class without knowing any thing about who extends it or its attributes.

the solutin is as folows :
each new item added will have to overide one interface that enforces it to generate a complete discription of its fields for example a car object will have to return
a hash map having the folowing

field : { fieldType , fieldValue }

example

  • model : { text , “ford” }
  • manifactureDate : { Date , “12/1/89” }

and each object will have also to implement a method called compareFields that take a
hash map like this and compare it to its field and return true or false.

now by this way i have solved many issues
-for the gui i will only have to make a rendering engine for this hashmap that can display
any item without having to know what its type. (again the gui is another client for the abstract class)

-for the search i can get a hash map that contain the fields the user enters
in the search form and loop on the abstract items and invoke the compare fieldmethod

i still don’t how i will handle the complex object (that have another object as its attributes)

i dont know what kind of pattern is this .. it is just an idea that i thought about.

EDIT : CONCRETE EXAMPLE

if i have an abstract item Class with a car and bus and boat that implements it,,and each of this classes has different attributes….
how can a manager for example traffic manager search for a certain item using the abstract class without casting to car or bus…
really sorry for the long question

  • 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-13T06:10:24+00:00Added an answer on May 13, 2026 at 6:10 am

    Encapsulation

    The OO principle of encapsulation states that you object should not expose its state to the outside. If your object epxose it internal information it breaks the encapsulation. What is still OK according to OO design is to pass the search criterion to the object and let it decides whether they match.

    interface IVehicle{
       bool doesMatch( Map<String,String> searchCriterion )
    }
    

    You can have an interator on all vehicles and retrieve the ones which match whithout breaking the encapsulation. Particular implementation of vehicles can still be reimplemented as desired.

    Visitor

    Otherwise, I would suggest you look at the Visitor pattern. The idea then is to traverse all the object and have an extra class handle the treatment for each specific type. This also breaks pure encapsulation (because the object need to expose its data to the visitor), but it’s much elegant.

    class VehicleSearchVisitor
    {
       Map<String,String> searchCriterion;
       void visit( Car car ) {...}
       void visit( Bike bike ) { ... }
       ....
    }
    

    Meta-programming

    The idea of object which are self-describing is another concept, which is called meta-programming. The presentation layer then introspect other object to know how to handle them. This traditionally is considered an advanced OO technique. You could could create custom annotations to describe the field of your class, so that the presentation layer can dynamically render the appropriate label. The same idea is for instance used with hibernate annotations. Meta-programming need to be done carefully otherwise you run into other issue.

    Insteanceof

    Use insteanceof is also a form of introspection (because you ask the object for its class) and is usually discouraged. Not because it is wrong in itself, but because it tend to be abused. Whenever possible, rely on traditional OO principles. Using instanceof abusively is a code smell.

    All in all, I would recommend to use a visitor for the search, and to not use meta-programming for the presentation layer and to instead create one simple page per type of vehicle.

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

Sidebar

Related Questions

Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
(Newbie DBUnit question Alert!) It appears that DBUnit for each table 'deletes all the
So this is a newbie rails design question. Lets say I want some of
Just a C++ newbie question. I have a class like class Person { private:
This question is about how to design a SQL relationship. I am pretty newbie
Newbie question... The objective: I intend to have an HTML text input field as
Newbie question: I just installed VisualSVN Server and created a repository. I noticed that
Newbie question here. I'm in the beginning stages of laying out a site in
DDD Newbie question: I read in a blog somewhere that in a scenario where
Duplicate: Learning implementing design patterns for newbies I have been a developer for years

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.