Question on OO Principle

This is a discussion on Question on OO Principle within the Java forums in Programming Languages category; Hi All I read a book which mentioned Java new keyword vialiate "Open-close" and "DIP" OO principle. I don't understand it, anybody having deep knowledge explain to me? Thanks a lot. Steven...

Go Back   Application Development Forum > Programming Languages > Java

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 03:27 AM
QQ
Guest
 
Default Question on OO Principle

Hi All

I read a book which mentioned Java new keyword vialiate "Open-close"
and "DIP" OO principle.

I don't understand it, anybody having deep knowledge explain to me?

Thanks a lot.

Steven
Reply With Quote
  #2  
Old 08-27-2008, 08:07 AM
Patrick May
Guest
 
Default Re: Question on OO Principle

QQ <chaojiang.au@gmail.com> writes:
> I read a book which mentioned Java new keyword vialiate "Open-close"
> and "DIP" OO principle.
>
> I don't understand it, anybody having deep knowledge explain to me?


You can read the definitive papers on the two principles here:

http://www.objectmentor.com/resources/articles/ocp.pdf

http://www.objectmentor.com/resources/articles/dip.pdf

I don't see how the 'new' keyword violates either. In what book did
you read that?

Regards,

Patrick

------------------------------------------------------------------------
S P Engineering, Inc. | Large scale, mission-critical, distributed OO
| systems design and implementation.
pjm@spe.com | (C++, Java, Common Lisp, Jini, middleware, SOA)
Reply With Quote
  #3  
Old 08-27-2008, 05:19 PM
QQ
Guest
 
Default Re: Question on OO Principle

Hi Patrick

Thanks a lot, actually I read twice, one from a website, the other
from a chinese book. I did a thought at night and found out.

1, regarding Open-Close
I think new create a instance so current class changed(not close for
modification)

2, regarding DIP
e.g. List = new ArrayList(). Abstract depends on a concrete instance
now,(in DIP, concret depends on abstract right?)

That is my thinking, not sure its right or not.

Thanks
Steven

On Aug 27, 10:07*pm, Patrick May <p...@spe.com> wrote:
> QQ <chaojiang...@gmail.com> writes:
> > I read a book which mentioned Java new keyword vialiate "Open-close"
> > and "DIP" OO principle.

>
> > I don't understand it, anybody having deep knowledge explain to me?

>
> * * *You can read the definitive papers on the two principles here:
>
> http://www.objectmentor.com/resources/articles/ocp.pdf


>
> http://www.objectmentor.com/resources/articles/dip.pdf
>
> I don't see how the 'new' keyword violates either. *In what book did
> you read that?
>
> Regards,
>
> Patrick
>
> ------------------------------------------------------------------------
> S P Engineering, Inc. *| Large scale, mission-critical, distributed OO
> * * * * * * * * * * * *| systems design and implementation.
> * * * * * p...@spe.com *| (C++, Java, Common Lisp, Jini, middleware, SOA)


Reply With Quote
  #4  
Old 08-27-2008, 08:42 PM
Mark Space
Guest
 
Default Re: Question on OO Principle

QQ wrote:

> 1, regarding Open-Close
> I think new create a instance so current class changed(not close for
> modification)


I'm guessing here, but I think the opposite. "new" violates the open
principle. "new" returns a single concrete class and that can never
change. It is not "open for change".

Solution: factory method. Factory methods can return sub-classes, thus
allowing the design to be extended. They are "open for change."

>
> 2, regarding DIP
> e.g. List = new ArrayList(). Abstract depends on a concrete instance
> now,(in DIP, concret depends on abstract right?)


I'm less sure about this but I think the same here. "new" returns a
concrete class that is not open for extension. DIP may require making a
different class.

ArrayList is not a good example because it is concrete and depends on an
abstract interface, List. So it's well positioned for further extension.
The fact that "new" is often used in the Collection design pattern
doesn't prevent other patterns.

Example:

List list = otherList.subList( from, to );

is a factory method that leaves the concrete type to runtime code. That
doesn't mean it's bad to have to call "new" eventually to get some
object, just that you need flexibility when you decide to call it.



Reply With Quote
  #5  
Old 08-27-2008, 10:44 PM
Patrick May
Guest
 
Default Re: Question on OO Principle

QQ <chaojiang.au@gmail.com> writes:
> Thanks a lot, actually I read twice, one from a website, the other
> from a chinese book. I did a thought at night and found out.
>
> 1, regarding Open-Close
> I think new create a instance so current class changed(not close for
> modification)


The Open-Closed Principle is a good practice for the design of
software components like classes, modules, and functions. It should
be possible to create useful new extensions of those components, but
impossible and unnecessary to modify them. This prevents changes from
cascading throughout a system. As the OCP paper describes, the way to
achieve this is through abstraction. In the case of OO classes, that
means the use of abstract base classes (among other techniques).

'new' is simply a mechanism for creating instances of classes
(objects). A class is not changed by creating an instance of it.

> 2, regarding DIP
> e.g. List = new ArrayList(). Abstract depends on a concrete instance
> now,(in DIP, concret depends on abstract right?)


Dependency inversion is also a heuristic for the design of
software components. Basically, it recommends that any dependencies
should be on abstractions, not concrete implementations. That results
in the dependency between high level components and detailed
components being inverted in comparison to typical top-down designs.

As with the OCP, your example deals with object creation, not
class design. Even given that, you are demonstrating the benefits of
the OCP because the client code that creates the ArrayList only
depends on the List abstraction. That's what the DIP recommends.

Regards,

Patrick

------------------------------------------------------------------------
S P Engineering, Inc. | Large scale, mission-critical, distributed OO
| systems design and implementation.
pjm@spe.com | (C++, Java, Common Lisp, Jini, middleware, SOA)
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 11:54 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.