| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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) |
|
#3
| |||
| |||
| 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) |
|
#4
| |||
| |||
| 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. |
|
#5
| |||
| |||
| 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) |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.