architect question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can someone share some insight on this:

I have a class A. It needs functions from class B.

Option 1: instantiate A by passing in an instance of B [ I think this is less memory than option 2 ]
Option 2: Have A create an instance of B.

other ideas - some form of interface, but then some place I still have an instance of B. What are some good ways to decouple or reduce the dependency ?

I went to a seminar and heard of how when a class gets created it gets created with the right object types... say like a Tax object tailored for the US vice Canada. Thanks for the advice. Andrew
 
Without a full understanding of your problem, it is difficult to provide a
correct architectural analysis, but here are some things to keep in mind.

Both O1 and O2 sound good and are really not much different. The choice
between the two may lie in whether you want to reuse the same object B on
more then one object A. In that case O1 would save some memory. This is an
effective way to decouple the objects so you may be on the right path.

From your last paragraph, it sound like you are talking about the strategy
design pattern
(http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/StrategyPattern
..htm).

Hope this hellps with the limited information.

andrewcw said:
Can someone share some insight on this:

I have a class A. It needs functions from class B.

Option 1: instantiate A by passing in an instance of B [ I think this is less memory than option 2 ]
Option 2: Have A create an instance of B.

other ideas - some form of interface, but then some place I still have an
instance of B. What are some good ways to decouple or reduce the dependency
?
I went to a seminar and heard of how when a class gets created it gets
created with the right object types... say like a Tax object tailored for
the US vice Canada. Thanks for the advice. Andrew
 
andrewcw said:
Can someone share some insight on this:

I have a class A. It needs functions from class B.

Option 1: instantiate A by passing in an instance of B [ I think this is
less memory than option 2 ]

If B is a value type, the instance is copied when you pass it to a method,
which is an extra object in memory. If A just creates an instance of B and
another instance of B didn't exist in the first place, then there is no
difference.

If B is a reference type, a copy of the reference would be passed, resulting
in no significant difference in memory use.
Option 2: Have A create an instance of B.

Your choice of whether to pick Option 1 or Option 2 depends on the
relationship between A and B. If B depends on the lifetime of A, then use
Option 2. However, if B has an independent lifetime, then Option 1 would be
better.
other ideas - some form of interface, but then some place I still have an
instance of B. What are some good ways to decouple or reduce the dependency
?

Make B implement an interface and define any parameters or variables to be
of the interface type. This will allow you to pass different types that
implement that interface.
I went to a seminar and heard of how when a class gets created it gets
created with the right object types... say like a Tax object tailored for
the US vice Canada.

Check out the docs on Localization and Resources, which .NET supports.

Joe
 
Andrew... As Joe Mayo has stated, if the lifetime of B is dependent on
A,
then use composition aka containment by ownership. If you are simply
wrapping B to adapt the interface to A use containment by reference.
Modeling forces you to think about these things <g>.

http://www.geocities.com/jeff_louie/OOP/oop12.htm

Regards,
Jeff
Option 1: instantiate A by passing in an instance of B [ I think this
is
less memory than option 2 ]
Option 2: Have A create an instance of B.<
 
First of all, I wouldnt say this is an architectural question. It is merely
a design decision based on forces. In both situations your memory impact is
completely dependant on whether the instance of B is associated to many
other instances of A at the same time. If your situation is simply that you
need an A, and it needs a B and one is created and passed in, you will
clearly get a memory impact - but if that is the true domain, then that is
the true domain.

Option 2 is a design choice one would make early on while the system is
taking shape. However this violates a few fundamental principles of object
orientation, in that you will clearly need to recompile the whole system if
you need A to now use a C instead of a B, but C decends from B. Option 2
violates a principle pioneered by Bertrand Meyer many moons ago, known as
the Open Closed Principle: "A module should be open to extension, but
closed for modification". In laymen terms, you should be able to extend A's
use with other neighbouring objects without requiring the need to change
your code in class A (the modification aspect). Depending on abstract types
is the key.

The most advanced technique for decoupling is using interfaces, and
inverting the dependancy. A then has a dependancy on an interface that is
contextually part of A's micro-problem domain. For example a Swtich could
depend on ISwitchable. Then, any bunch of objects could implement
ISwitchable, and Switch can now be extended to talk to a whole suite of
classes withouth making a single change to Switch.

Based on this, the whole reason for creating objects with the right types is
because a policy layer makes the decisions at application start up or
runtime (using factories). As an example, you might have a business object
that depends on a dal object. For arguments sake A might need persisting by
IADataService. At some point a decision is made whether to use the RDBMS
IADataService, the XML IADataService or some other data service
implementation. This decision is then applied as a policy, to the
instantiation of the association between A and its IADataService. If you
didnt create the objects passing in its types in this manner, the system
wouldnt be extendable.

HTH

Nick.

andrewcw said:
Can someone share some insight on this:

I have a class A. It needs functions from class B.

Option 1: instantiate A by passing in an instance of B [ I think this is less memory than option 2 ]
Option 2: Have A create an instance of B.

other ideas - some form of interface, but then some place I still have an
instance of B. What are some good ways to decouple or reduce the dependency
?
I went to a seminar and heard of how when a class gets created it gets
created with the right object types... say like a Tax object tailored for
the US vice Canada. Thanks for the advice. Andrew
 
Hello Andrew,

You've taken a basic course in Design Patterns, from the looks of it. I
suggest that you investigate some of the patterns more thoroughly. The
information you provide is insufficient to make a good choice between your
two options, or a potential third or fourth option. Other folks have
provided some good advice, and I hope that helps.

Note: memory is not the point. Maintainability, readability,
extensability... these are nearly always far more important than a few bytes
of RAM. Using published Design Patterns will give you both.

Good Luck,
--- Nick M.

andrewcw said:
Can someone share some insight on this:

I have a class A. It needs functions from class B.

Option 1: instantiate A by passing in an instance of B [ I think this is less memory than option 2 ]
Option 2: Have A create an instance of B.

other ideas - some form of interface, but then some place I still have an
instance of B. What are some good ways to decouple or reduce the dependency
?
I went to a seminar and heard of how when a class gets created it gets
created with the right object types... say like a Tax object tailored for
the US vice Canada. Thanks for the advice. Andrew
 
Thanks for your views - I really appreciate you comments
and yes I was thinking of factory patterns...
-----Original Message-----
Can someone share some insight on this:

I have a class A. It needs functions from class B.

Option 1: instantiate A by passing in an instance of B
[ I think this is less memory than option 2 ]
Option 2: Have A create an instance of B.

other ideas - some form of interface, but then some place
I still have an instance of B. What are some good ways to
decouple or reduce the dependency ?
I went to a seminar and heard of how when a class gets
created it gets created with the right object types... say
like a Tax object tailored for the US vice Canada. Thanks
for the advice. Andrew
 
Hi andrewcw,

Thanks for your feedback.

I am glad the community's reply makes sense to you.

If you have any further concern, please feel free to tell me, I will work
with you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top