Java Virtal Machine.net

[ News ] rss

February 01, 2011

Mark Wielaard: New GPG key. Finally created a new GPG key using gnupg. The old one was a DSA/1024 bits one and 8 years old. The new one is a RSA/2048 bits one. I will use the new one in the future to sign any release tarballs I might create. pub 2048R/57816A6A 2011-01-29 Key f...

More »

February 01, 2011

Andrew Hughes: [SECURITY] IcedTea6 1.7.8, 1.8.5, 1.9.5 Released!. We are pleased to announce a new set of security releases, IcedTea6 1.7.8, IcedTea6 1.8.5 and IcedTea6 1.9.5. This update contains the following security updates: The IcedTea project provides a harness to build the source code from OpenJDK6 u...

More »

November/2024
SunMonTueWedThuFriSat
     12
3456789
10111213141516
171819 20212223
24252627282930
       

[ Archives News
for 'Java Technology' ]

home > news > java technology > using the builder pattern with subclasses

Using the builder pattern with subclasses

 Josh Bloch's Effective Java popularized the Builder Pattern as a more palatable way of constructing objects than constructors or factory methods when there are potentially many constructor parameters. The formulation in Effective Java makes for a particularly readable construction, like this: new Rectangle.Builder().height(250).width(300).color(PINK).build(); The advantage over a constructor invocation like new Rectangle(250, 300, PINK); here is that you don't have to guess whether 250 is the height or the width. More generally, if the constructor allowed you to specify many other parameters — such as position, opacity, transforms, effects, and so on — it would quickly get very messy. The builder pattern stays clean. But one question that arises is: how does it work in the presence of inheritance? For example, suppose you have an abstract Shape class that represents an arbitrary graphical shape, with a set of properties that are common to all shapes, such as opacity and transforms. And suppose you have a number of concrete subclasses such as Rectangle, Circle, Path and so on, each with its own properties, like Rectangle's height and width. As a reminder, here's what the builder pattern looks like in the absence of subclassing: public class Rectangle { private final double opacity; private final double height; ... publi...


Date: October, 25 2010
Url: http://www.java.net/blog/emcmanus/archive/2010/10/25/using-builder-pattern-subclasses


Others News

©2002-2019Java-Virtual-Machine.net nl | Mobirise Themes | easy website builder | top website builder | html slider