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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:49:27+00:00 2026-05-14T14:49:27+00:00

I want to know when we need to use the abstract factory pattern. Here

  • 0

I want to know when we need to use the abstract factory pattern.

Here is an example,I want to know if it is necessary.

The UML

THe above is the abstract factory pattern, it is recommended by my classmate.
THe following is myown implemention. I do not think it is necessary to use the pattern.

And the following is some core codes:

    package net;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;



public class Test {
    public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
        DaoRepository dr=new DaoRepository();
        AbstractDao dao=dr.findDao("sql");
        dao.insert();
    }
}

class DaoRepository {
    Map<String, AbstractDao> daoMap=new HashMap<String, AbstractDao>();
    public DaoRepository () throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException  {
        Properties p=new Properties();
        p.load(DaoRepository.class.getResourceAsStream("Test.properties"));
        initDaos(p);
    }
    public void initDaos(Properties p) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        String[] daoarray=p.getProperty("dao").split(",");
        for(String dao:daoarray) {
            AbstractDao ad=(AbstractDao)Class.forName(dao).newInstance();
            daoMap.put(ad.getID(),ad);
        }
    }
    public AbstractDao findDao(String id) {return daoMap.get(id);}

}
abstract class AbstractDao {
    public abstract String getID();
    public abstract void insert();
    public abstract void update();
}
class SqlDao extends AbstractDao {
    public SqlDao() {}
    public String getID() {return "sql";}
    public void insert() {System.out.println("sql insert");}
    public void update() {System.out.println("sql update");}
}
class AccessDao extends AbstractDao {
    public AccessDao() {}
    public String getID() {return "access";}
    public void insert() {System.out.println("access insert");}
    public void update() {System.out.println("access update");}
}

And the content of the Test.properties is just one line:

dao=net.SqlDao,net.SqlDao

So any ont can tell me if this suitation is necessary?


——————-The following is added to explain the real suitation————–

I use the example of Dao is beacuse it is common,anyone know it.

In fact,what I am working now is not related to the DAO,I am working to build a Web

service,the web serivce contains some algorithms to chang a file to other format,

For example:net.CreatePDF,net.CreateWord and etc,it expose two interfaces to client:getAlgorithms and doProcess.

The getAlogrithoms will return all the algorithms’s ids,each id is realted to the
corresponding algorithm.

User who call the doProcess method will also provide the algorithm id he wanted.

All the algorithm extends the AbstractAlgorithm which define a run() method.

I use a AlogrithmsRepository to store all the algorithms(from

the properties file which config the concrete java classes of the algorithms by the web

service admin).That’s to say, the interface DoProcess exposed by the web service is

executed by the concrete alogrithm.

I can give a simple example:
1)user send getAlgorithms request:

http://host:port/ws?request=getAlgorithms

Then user will get a list of algorithms embeded in a xml.

<AlgorithmsList>
  <algorithm>pdf</algorithm>
  <algorithm>word<algorithm>
</AlgorithmsList>

2)user send a DoProcess to server by:

http://xxx/ws?request=doProcess&alogrithm=pdf&file=http://xx/Test.word

when the server recieve this type of requst,it will get the concrete algorithm instance according to the “algorithm” parameter(it is pdf in this request) from the AlgorithmRepostory. And call the method:

AbstractAlgorithm algo=AlgorithmRepostory.getAlgo("pdf");
algo.start();

Then a pdf file will be sent to user.

BTW,in this example, the each algorithm is similar to the sqlDao,AccessDao.
Here is the image:

The design image

Now,does the AlgorithmRepostory need to use the Abstract Factory?

  • 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-14T14:49:28+00:00Added an answer on May 14, 2026 at 2:49 pm

    The main difference between the two approaches is that the top one uses different DAO factories to create DAO’s while the bottom one stores a set of DAO’s and returns references to the DAO’s in the repository.

    The bottom approach has a problem if multiple threads need access to the same type of DAO concurently as JDBC connections are not synchronised.

    This can be fixed by having the DAO implement a newInstance() method which simply creates and returns a new DAO.

    abstract class AbstractDao {
        public abstract String getID();
        public abstract void insert();
        public abstract void update();
        public abstract AbstractDao newInstance();
    }
    class SqlDao extends AbstractDao {
        public SqlDao() {}
        public String getID() {return "sql";}
        public void insert() {System.out.println("sql insert");}
        public void update() {System.out.println("sql update");}
        public AbstractDao newInstance() { return new SqlDao();}
    }
    

    The repository can use the DAO’s in the repository as factories for the DAO’s returned by the Repository (which I would rename to Factory in that case) like this:

    public AbstractDao newDao(String id) {
        return daoMap.containsKey(id) ? daoMap.get(id).newInstance() : null;
    }
    

    Update

    As for your question should your web-service implement a factory or can it use the repository like you described? Again the answer depends on the details:

    • For web-services it is normal to
      expect multiple concurrent clients
    • Therefore the instances executing the
      process for two clients must not
      influence eachother
    • Which means they must not have shared state
    • A factory delivers a fresh instance on
      every request, so no state is shared
      when you use a factory pattern
    • If (and only if) the instances in your
      repository are stateless your
      web-service can also use the
      repository as you describe, for this
      they probably need to instantiate
      other objects to actually execute the
      process based on the request
      parameters passed
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to know how to use ListView.FixedViewInfo, so i need some code spinet
I want to know how to use variables for objects and function names in
Want to know what the stackoverflow community feels about the various free and non-free
I want to know what a virtual base class is and what it means.
I want to know what are the options to do some scripting jobs in
I want to know what exactly is the sequence of calls that occurs when
I want to know which tool can be used to measure the cyclomatic complexity
I want to know if i can create a custom google maps application,on which
I want to know how does the SQL Server know what @p# is in
i want to know how to edit a single row (which i select) from

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.