Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2004

Tip: Looking for answers? Try searching our database.

Constructor help...

Thread view: 
Jeffrey Spoon - 24 Nov 2004 22:34 GMT
...please

Hello, I'm still mucking about with my decorator patterns. I have an
annoying problem though. I am trying to wrap (or decorate) a class, like
so:

Company cotest = new CompanyDecorator(new Company("Jeff's Corp."));

I have done this simply by extending Company with CompanyDecorator and
adding a relation:

protected Company    theDecoratedCompany;

I managed to wrap an interface before and decorate all the classes that
implemented it, but this Company class is giving me problems because the
Company constructor accepts a String as a  parameter (eg "Jeffs corp") ,
yet it seems my CompanyDecorator needs to accept a Company as a
parameter, since I have related it to Company and the point is to wrap a
Company in the first place. So, I can't see how I call the Constructor
in Company from the CompanyDecorator constructor and pass the String,
rather than the Company.

Am I doing anything obviously wrong?

I chopped this down to SEC. As you can see I cheated by using
toString(), which gives garbage rather than "Jeff's Corp".

*****************************************************

public class Main {

   public static void    main(String[] args) {
       Application app = new Application();
       app.run();

   }    // method: main

}//class Main

*********************************************

public class Application  {

   public final void    run() {

     //  Company co = new Company("The Loony University");
 Company cotest = new CompanyDecorator(new Company("Jeffs Corp."));

       cotest.displayEmployees();

   }    // method: run

  }    // class: Application

*************************************************************************
**

import java.util.*;

public class Company  {

   // constructor
   public     Company(java.lang.String aName) {
       theName = aName;
       theEmployees = new ArrayList();

   }    // method: Company

   public void    displayEmployees() {
       System.out.println();
       System.out.println("Staff list for " + theName);
       System.out.println();

       System.out.println();

   }    // method: displayEmployees

   // ---------- Attributes --------------------

   private java.lang.String    theName;

   // ---------- Relations --------------------

   private java.util.Collection    theEmployees;    // of Employee

}    // class: Company

***********************************************************************

public class CompanyDecorator extends Company  {

   public     CompanyDecorator(Company aCompany) {
       super(aCompany.toString()); //ahem!
       theDecoratedCompany = aCompany;

   }    // method: CompanyDecorator

   public final void    displayEmployees() {
       super.displayEmployees();
       //...
}

   protected Company    theDecoratedCompany;

}

***************************

Cheers.

Signature

Jeffrey Spoon

Bjorn Abelli - 26 Nov 2004 13:49 GMT
"Jeffrey Spoon" wrote...

> ...please
>
[quoted text clipped - 3 lines]
>
> Company cotest = new CompanyDecorator(new Company("Jeff's Corp."));

> I managed to wrap an interface before and decorate all the classes that
> implemented it, but this Company class is giving me problems because the
> Company constructor accepts a String as a  parameter (eg "Jeffs corp"),

It would surely be "easier" and "prettier" if Company at least had a public
accessor to "theName".

Nevertheless, as a decorator actually is a "wrapper", and not a true
"extension" of the original instance, the content of *that* string wouldn't
really matter. Actually *no* content of inherited members would matter.

That is, you don't actually *use* the superclass other than as an inherited
*type*, all "old" functionality is *delegated* to the wrapped instance.

For instance:

>    public final void    displayEmployees() {
>        super.displayEmployees();
>        //...
> }

should be:

   public final void    displayEmployees() {
       theDecoratedCompany.displayEmployees();
       //...
}

...So your "toString"-solution would work as anything else...

HTH.

// Bjorn A
Jeffrey Spoon - 26 Nov 2004 16:15 GMT
>It would surely be "easier" and "prettier" if Company at least had a public
>accessor to "theName".
[quoted text clipped - 5 lines]
>That is, you don't actually *use* the superclass other than as an inherited
>*type*, all "old" functionality is *delegated* to the wrapped instance.

Well I can't actually change Company as it is a class being tested. I
think the reason there is no accessor is to simplify the example (for
example there is no compareTo,hashCode or equals methods for the
Collection used either - which is not shown in the SEC)

>For instance:
>
[quoted text clipped - 11 lines]
>
>...So your "toString"-solution would work as anything else...

Doh! That's how I did it using the Interface. For some reason I decided
to change it in this case.

>HTH.

Yes it did, thanks very much.

Signature

Jeffrey Spoon



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.