473,725 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance, Specialization, and Factory Classes

Using the dotnet v1.1 framework (so no generics possible). I'd like to
create a bunch of Factory classes that all inherit from a single
abstract base Factory class. Each Factory is responsible for the
creation of objects of a type that inherit from a base class. Factory
is probably the wrong word here, as it's really a persistance class,
but the problem is the same.
Example:
abstract BaseClass
abstract BaseFactory, uses BaseClasses internally, has a bunch of
abstract and virtual methods that are responsible for the actual
implementation:
virtual BaseClass createNew(); //does a bunch of stuff and calls
doCreateNew() to do actual creation
abstract BaseClass doCreateNew();
virtual bool save(BaseClass b); //does a bunch of stuff and calls
doSave() to actually save
abstract bool doSave(BaseClas s b);
...
you get the idea.

concrete ClassA extends BaseClass.
concrete ClassAFactory extends BaseFactory, implements doCreateNew,
doSave, etc. Problem is, I'd like this class to only accept instances
of ClassA rather than BaseClass, and I'd like the createNew functions
to return instances of ClassA rather than BaseClass. I know I can use
new on the functions, but that doesn't seem like the right answer as it
doesn't really hide the base functions.

In fact, the problem is a bit deeper then this, as ClassAFactory itself
can require different parameters for the createNew, save, etc.
functions (such as a userId for auditing purposes), and I would like to
truely hide the base functions (current idea is to implement them and
just throw NotImplemented exceptions, but that seems ugly, and it
clutters up my build logs with a bunch of unreachable code detected
messages)

Anyone have any ideas how this type of thing could be / should be done?

Jan 31 '06 #1
4 2024
I'd drop the idea of a BaseFactory completely, because the only way to
implement what you want would be to use the new keywork (or Shadows in
VB) to hide the FactoryBase's Create method and totally rewrite the
method signature.

public classAFactory {
public new Create( int key ) {} // This method totally hides the
FactoryBases's Create method.
}

However, new/Shadows breaks inheritience (by design) and if you find
yourself wanting to use them, you should instead rule out inheritience
completely for those set of classes.

Jan 31 '06 #2
Dropping the BaseFactory leads to alot of code duplication (although
the code will be easier to follow). Most of the implementation code is
the same. For instance, if the Factory (really a persistance class)
used SQL to actually load/save/delete data, the only difference between
the classes would be the actually SQL statement. The logging and data
checking would be the same between classes.

I dunno, I understand that I'm kinda breaking with the purity of
interfaces and inheritance by trying to limit the parameters and return
types, but it still seems logical to me. If you think of inheritance
as specialization rather then extention. I keep thinking this would be
easier if I could use the new generics features in the 2.0 framework,
but the customer wants to stick with 1.1 for now.

Jan 31 '06 #3
Anon,

Perhaps your FactoryBase becomes an internal class, FactoryHelper,
which stores all the code you don't want to duplicate. That way, you
have a good clean design and don't have to worry about duplicating a
lot of code.

Get .Net Reflector sometime (From Lutz Roeder), there are TONS of
internal classes in the .net framework itself that do nothing but
assist the public classes.

HTH
Andy

Jan 31 '06 #4
Actually, I forgot the main reason I want all the Factories to inherit
from a BaseFactory. There is another factory that generates these
factories. The way it works is like this:
DataSource produces Managers which manage Objects. The factories I was
talking about are actually the Managers, each inheriting from a common
base, or implementing a common interface IManager.

DataSource has a function IManager getManagerForTy pe(Type t) that
returns an instance of a TypeTManager that provides the createNew,
save, getById, etc, functions.

This allows objects to locate other classes of objects from the same
DataSource. Example: think of a Student object trying to locate a
Class object (this is not a homework problem, it's just Students and
Classes are a good example). These objects are completely independant
of each other, but it would be nice to be able to have a function like
this in the Class class:

public Student[] getStudents(){
IStudentManager manager =
this.dataSource .getManagerFor( typeof(Student) ) as IStudentManager ;
return manager.getStud entsForClass(th is.class_id);
}

By injecting the DataSource during object creation (hence the
createNew() function), it allows these object to reach back to their
datasource and reference other classes. I like this becauses it allows
me to fully seperate the persistance layer from the business layer.
Each object knows where it came from, and that's all it needs to know,
each DataSource knows what manager is required for each type. I can
have a XMLDataSource, OleDbDataSource , SqlServerDataSo urce, etc, that
would each have their own Managers (XMLStudentMana ger,
OleDbStudentMan ager, etc).

I think this is a good idea, I'm just having a tough time implementing
it as nicely as I'd like. I've got a working implementation, but it's
kinda ugly and requires a bunch of casts and allows things I don't
want.

Jan 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1884
by: cppaddict | last post by:
Hi, Say I have a abstract base class and I know that every derived class will have a static data member (call it SharedInformation) representing information to be shared across all instances of that derived class. However, this information will be differenet for each derived class. Now, if I define SharedInformation as a static member of the base class, then SharedInformation cannot vary across the different derived
5
2121
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I can get! Consider 3 classes in the following heirarchy: base / \ deriv1 deriv2 \
3
1531
by: Tommi Mäkitalo | last post by:
Hi, I try to implement a factory for classes. There are 2 kinds of them, which have different constructors. One of them is the base class. The factory gets all needed parameters in his create-method. I would like to specialize my factory template depending on the base-class. Here is a (not working) example: #include <iostream>
20
23109
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a much finer level. As such, it's very frustrating to be working in VBA which lacks inheritance, one of the more powerful tools for eliminating duplication at the level I'm talking about. I've recently come up with a technique to emulate one...
1
984
by: Jodie | last post by:
Hi All, I have a question regarding about inheritance and member of the class. For example I have the class A and class B what is the situation that I need to define class B is base class of class A or class B is member of class A like below: Scenario 1: class A : public B Scenario 2: or class A
6
1600
by: Dave | last post by:
Hello all, Please see my question embedded in comment form below. Thanks, Dave #include <iostream> #include <boost/shared_ptr.hpp>
9
372
by: nicolas.hilaire | last post by:
Hi all, I have somes classes that have the same concept. I would like to instance one of them depending on a condition Something like this MyObj myObj; if (i==0)
60
4925
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
2
2584
by: Paul McGuire | last post by:
On May 25, 8:37 am, Michael Hines <michael.hi...@yale.eduwrote: Here's a more general version of your testing code, to detect *any* diamond multiple inheritance (using your sample classes). -- Paul for cls in (A,B,C,D): seen = set()
0
8886
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8750
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9105
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8091
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6701
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3218
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.