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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:58:04+00:00 2026-06-07T08:58:04+00:00

Suppose I have a class called Factory . I intend to have that class

  • 0

Suppose I have a class called Factory. I intend to have that class publicly accessible so anyone can create and access the factory. I want the factory to create and distribute widgets. I only want the factory to be able to create widgets, not anyone in the public program space. However, I’d like to have a method called distribute() that would give out a widget to the main program. At that point the main program could access all the public methods of that widget. It could also give it back to the factory if need be, thereby removing any public access to that object.

If this is possible, how does one accomplish 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-06-07T08:58:07+00:00Added an answer on June 7, 2026 at 8:58 am

    What you’re looking for is an inner class. By declaring the constructor of the inner class private, only the encapsulating class can access it! Then you create a factory method in the outer class to build the widget and distribute it. I’ve declared it static here, but that’s not necessary, depending on your needs.

    public class Factory {
    
      public static Widget buildWidget() {
        Widget widget = new Widget();
        return widget;
      }
    
      public class Widget {
        private Widget() { ... }
        public void someMethod() { ... }
      }
    }
    

    Note that in clean code your other concerns must be addressed by being careful about your referential integrity. To ‘give back’ the widget all other references to it must be released. Your Factory can certainly keep track of what widgets it’s given out, but it’s very hard to ‘recall’ widgets reliably.

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

Sidebar

Related Questions

Suppose I have a generic class called MyClass<T> how can I create a JavascriptConverter
Suppose that we have a class called class1. The class1 has a method called
Suppose we have a class called AccountService that manages the state of accounts. AccountService
Suppose that I have a JPA-annotated class called MyData with a primary identifier field
Suppose I have a class called vector that maintains some internal, dynamic array of
Suppose I have a class Baz that inherits from classes Foo and Bar ,
I have a very beginning C# question. Suppose I have a class called GameObject
Suppose I have a class called Foo . This class will be modified by
Suppose I have Objective C interface SomeClass which has a class method called someMethod
Suppose I have a class called Animal and an interface called AnimalTrainer. public interface

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.