Which would be the better approach?

Hello!
I'm a working java professional, but has taken on some courses on the local University.
Of course there is a difference in how things are solved in theory and practise, but it makes
me wonder what the "best" solutions to some problems are...
Consider the following structure:
class Book{
   private List chapters;
class Chapter{
   private List pages;
   private Book book;
   public Book getBook(){
      return this.book;
class Page{
   private List chars;
   private Chapter chapter;
   public Chapter getChapter(){
      return this.chapter;
class Char{
   private Page page;
   public Page getPage(){
      return this.page;
   public void method(){
      // blabla
}Check out the "method" in class Char. Say I would like to have a reference to the Book that this character is in. I would say that I have two choices:
1. call getPage().getChapter().getBook()
2. put a getBook() in each of the classes
Where I come from it is a bad thing to "expose structure", the first example exposes structure.
But, it is also a bad thing to let classes do things that they aren't responsible for (cohesion).
What solution would you prefer?

Hi!
With getPage().getChapter().getBook() surely you are having too high coupling and also exposure.
In just one code line you are coupling to three classes and also violating twice De Meter assumption about not accessing class internal structures as character accesses to chapter through Page and to Book through Chapter.
The problem is in the main design idea i think...you are just designing data structures as if you would be designing db entities, with double dispatches between all classes as you would connect db tables with joins but this ends with no real reusability, think to this.... you can't reuse any of your classes without carrying all of the others.
For example why a character should be linked to a book, a character can be an actor of many books but it's not connected to a specific book. A character class should be reusable in movies, lyrichs, books, dreams. Why you want a Chapter to know what is a book?? In concrete that means that every time you modify the book class you must recompile also the chapter cause it's directly connected to the book class and also you can use your Chapter class only within your Book class, it's not reusable at all.
You want to search characters within books? ok you just need an external components that will enter the library and ask to any book what's it about and which characters play in it if it is a characters book,
so everyone will just expose services for the data it's responsible for.

Similar Messages

  • Which would be the better choice?

    I am presently using a EOS T3. What would be the better choice for an upgrade?  A T4i, T5i or 60D?
    Solved!
    Go to Solution.

    Good.
    One thing you don't have is a wide aperture lens. With the savings plus just a bit more you should treat yourself to a 50mm f/1.4, or for just $100 get a 50 f/1.8. They do almost magical things wide open and close to a subject. Subject is isolated, background is blurred bokeh, background lights are big round disks. Plus they are great in low light. Too fun.
    Scott
    Canon 6D, Canon T3i, EF 70-200mm L f/2.8 IS mk2; EF 24-105 f/4 L; EF-S 17-55mm f/2.8 IS; EF 85mm f/1.8; Sigma 35mm f/1.4 "Art"; EF 1.4x extender mk. 3; 3x Phottix Mitros+ speedlites
    Why do so many people say "fer-tographer"? Do they take "fertographs"?

  • What would be the better approach at showing a course is complete?

    (OT: I'm being evlauated to do some online software simulations using Captivate so I'm trying to merge my Authorware "thinking" with Captivate)
    I have a 14 slide simulation; task to edit preference files. Scoring is not necessary but getting to slide 14 is all that is necessary.
    Presently I have a click object on slide 13 whose reporting options is "Include in Quiz" with adding 1 point.
    So I have a course with Objective ID=Quiz10111 Interaction ID=Interaction11184 and a score of 1.
    But I also note in the reporting preferences I could use "Slide views only." So maybe this is all that is necessary?
    I'm not that savy (yet) with LMS's but if I wanted to write this data out as a tab delimited file on the hosting server what should I consider? Authorware had a function "WriteExtFIle." Would a button on the last slide set to execute Javascript be a better approach?
    Another idea looking for a point of view. If I get the project I am considering a 2 month contract Adobe Acrobat Connect Pro to get more confortable with this approach. Would this help?
    FYI...I just downloaded the Captivate 4 manual. Last modified on May 19th according to the doscument.
    Thanks

    Regarding your function hanging, that seems to be because your loop never ends. Also the inner query is missing a WHERE clause:
    Select Translate(comnt_rec.comment_text,'%#$^',' ') into repvalue from test_comnt;  <-- How many rows will this bring back?However I don't think you meant to query the test_comnt table at all - it looks as though you just wanted to assign the result of the TRANSLATE expression (btw please, TRANSLATE() or translate() but not Translate() - the parser doesn't care but it drives me nuts), in which case just
    repvalue := translate(comnt_rec.comment_text,'%#$^',' ');or even include the expression in the cursor itself and save yourself the trouble.
    Another thing that drives me nuts, although not the rest of the world apparently, is the name "c1" for a cursor. Why don't you name your variables "v1", "v2" etc? If you had two cursors would you seriously name the second one "c2"?
    While I'm at it, what kind of a name is "test_comnt"?

  • Which would be the best NAC approach with thin clients up today?

    Hello guys,
    Which would be the best NAC approach using CAM/CAS infrastructure for thin clients? I guess nac agent is still not supported on thin clients/virtual desktops right?
    Would mac address authentication be the best option?
    Regards,
    Emilio

    Hello guys,
    Which would be the best NAC approach using CAM/CAS infrastructure for thin clients? I guess nac agent is still not supported on thin clients/virtual desktops right?
    Would mac address authentication be the best option?
    Regards,
    Emilio

  • What's the better approach?

    Hi, I was just making a program that access a DB through JDBC, and I got myself into this dilemma
    What's the better approach to make a connection to a DB?
    approach #1(use of singleton pattern)
    import java.sql.*;
    public class DBConnection {
        private ResultSet rs;
        private Connection conn;
        private PreparedStatement ps;
        private static boolean singleton = false;
        private DBConnection() throws Exception{
            Class.forName("driverPath").newInstance();
         conn = DriverManager.getConnection("url", "user", "pass");
         singleton = true;
        public static DBConnection getInstance() throws Exception{
            if(singleton)
             return null;
            return new DBConnection();
        protected void finalize() throws Throwable {
            //close the connection and release resources...
            singleton = false;
        //Methods to make DB querys and stuff.     
    }approach #2 (make a connection only when doing querys)
    public class DBConnection {
        private ResultSet rs;
        private Connection conn;
        private PreparedStatement ps;
        public DBConnection() throws Exception {
            Class.forName("driverPath").newInstance();
        //Just some random method to access the DB
        public ArrayList<Row> selectAllFromTable() {
            ArrayList<Row> returnValue = new ArrayList<Row>();
         try {
             conn = DriverManager.getConnection("url", "user", "pass");
             //make querys and fill the arraylist with rows from the table
         } catch(Exception ex) {
             returnValue = null;
             ex.printStackTrace();
         } finally {
             if(ps != null)
                 ps.close();
                if(rs != null)
              rs.close();
             if(conn != null)
              conn.close();
         return returnValue;
    }I know this classes maybe don't even compile and I don't handle the Exceptions, I'm just trying to make a point about how to manage the connection
    So, what it's the better approach in your opinions? #1? #2? neither?

    Hi,
    I'm resurrecting this thread to ask is this approach OK?
    I'm trying to make a single MySql JDBC connection accessible throughout the model.
    I'm planning to use it in a Swing application. Whilst I realise the Swing apps are inherently multi-threaded, everything I plan to do can (I think) be done within the constraint that all access to model happens on the EDT, and the user will just have to wear any unresponsiveness.
    package datatable.utils;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    abstract public class MySqlConnection {
         public static final String URL = "jdbc:mysql://localhost/test";
         public static final String USERNAME = "keith";//case sensitive
         private static final String PASSWORD = "chewie00";//case sensitive
         private static final Connection theConnection;
         static {
              String driverClassName = "com.mysql.jdbc.Driver";
              try {
                   Class.forName(driverClassName);
                   theConnection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
              } catch (Exception e) {
                   throw new DAOException("Failed to register JDBC driver class \""+driverClassName+"\"", e);
         public static Connection get() {
              return(theConnection);
    }Is there a better solution short of c3po? Which I played with, but couldn't work out how to configure.
    Thanx guys (and uj),
    keith.

  • Which would be a better purchase, Zen Touch or Zen Mic

    Okay for christmas this year I am asking for an mp3 player. Ive done my research and decided between the Zen Touch or Zen Micro so that I may take advantage of Napsters music downloading service. Now my delima is which would be a better purchase. Ive read reviews on amazon, epinions, and cnet. As far as the Micro goes its a love/hate type of thing while i've read only good reviews of the Touch. Now the Touch doesnt seem as popular and for what reason I dont know. Also i've noticed the Micro has a great number of more accessories than the Zen Touch. Now taking into consideration accessories and durability which would you say is the better LONG term purchase?
    -Thanks

    If you are planning on using subscriptoin music, I would go with the Touch, as it is a 20GB player and you have unlimited music to download (more space=more music )
    Don't get it wrong, the Micro is awesome, I would have got it if I didn't have so much music.
    And my guess as to why the Touch is not as popular as the Micro is that people don't like the way it looks and it's size (which is understandable, because of it's 24hour battery).
    The Touch is also a great value, because it comes with an AC power cable (the Micro used to come with this when it first came out, but now it is sold separatley), and a carrying pouch (I replaced mine with a better because you can't aceess the MP3 player while it is in the pouch, making it a bit inconvenient). On top of it all, the 20GB version sells for around $200. And with the new firmware, the "over-sensiti've" touchpad that many reviews claim is now taken care of, and it is as easy to use as the Zen Micro's touchpad .
    All in all, it is down to what you want:
    a 5GB MP3 player that looks cool and comes in 0 different colors
    or a 20GB player that comes with a few extra acessories. Both are around the same price (the Micro may be $20 cheaper, if it is on sale).

  • List view to include a column or field which would display the sequence number in a view

    I have a view for a list which displays items following any condition or sorting. Now, i would like to display the list items in such  a way that
    the auto incremented numbers are displayed as an arbitary field which would display the numbers such that
    its just going to display items in sequence. Is there anything that can be done in sharepoint designer for the list to accomodate a column/field which would display the sequence numbers.
    Thanks

    There are columns such as
    Name, Goals
    and  it should be arranged in descending order of Goals and it could be paginated but also a column should be displayed for ranks e.g
    Name Goals Rank
    A          40     1
    B          39     2
    C          30     3
    So, the idea is the ways it is sorted in descending order, if we possibly have a counter then we could display the ranking of the items.
    Thanks

  • I have around 1,000 photos on my old phone and I want to put them on my new phone without using a backup or iTunes, which would be the easiest way?

    I have around 1,000 photos on my old phone and I want to put them on my new phone without using a backup or iTunes, which would be the easiest way?

    I was looking for something on the net, and i run into this post. Am not sure if you have found a soulution or not, anyways, if not here are some programs that i love and they show you every thing in your iPhone:
    iFunBox, iBackupBot and DiskAid
    am sure they will help you find what you want and then you can just copy them to your computer.
    if you use Mac, iPhoto will import everything for you.

  • Which one is the better cover for protection on tje ipad mini with retina

    I  bougth an mini ipad with retina, my 5 years old will use using too. Which one is the better cover for protection?

    You'll have to think in terms of a shell rather than just a cover. Main problems are dropping and liquid getting inside. Plus a shell can add a bit of extra heft and "grippability", aiding little hands in holding on the slippery aluminum back. Can add some sort of screen protector to prevent scratches, though that will reduce sensitivity to an extent; however, a direct blow will inevitably shatter the Gorilla Glass.
    That being said, I am very happy with the Lifeproof Nüüd case that has protected mine from the get-go. Decided to forego screen protectors in lieu of better touch performance; almost 2 years down the road the screen is still scratch free.
    iPad 4th Gen, iOS 7.1.2, 64GB, White

  • Which would be the right Anti-Virus For C7

    Hi All,
    I wanted to know which would be the right anti-virus for C7, F-secure or Kaspersky. F-secure comes preinstalled and there are adds running about of kaspersky. and how much both would cost in India Rs.
    Would be great if anyone could guide me for the same.

    There are no viruses to affect Symbian phones, you DON'T NEED ANTI VIRUS !!! That's it !
    If I have helped at all, a click on the White Star is always appreciated :
    you can also help others by marking 'accept as solution' 

  • Which would be the best option to backup multiple locations?

    Hello,
    My situation is as follows. I have a Mac mini with 250 GB HD and three external HDs with 500 GB, 1 TB and another one with 1 TB.
    I would like to use the HD with 500 GB as Time Machine backup HD for the Mac mini, and one of the 1 TB HD as backup for the other.
    Which would be the best option?
    Using Time Machine I see that I can't set different backup location for different disks, only set multiple backup drives, but they must share the same backup directories.

    You can't do that with Time Machine. You would have to use a third-party solution, such as "SuperDuper" or "CarbonCopyCloner." That's not an endorsement of either one.

  • Which one is the best approach for responsive UI development option in SharePoint 2013

    Which one is the best approach for responsive UI development option in SharePoint 2013
    Device channel or responsive UI (HTML, CSS)?

    In practice you're probably going to end up with a combination. A couple of device channels for classes of device and then responsive UI within those channels to adjust to particular devices within the classes.
    Of course the real answer is as always 'it depends' as you'll need to pick the best option for each client based on their needs.

  • Which would be the best upgrade for the money?

    Here is my situation:
    My school has a six year life cycle for macs, and we have a 2006 Mac Pro (1,1) that is reaching the end of its useful life. I had recently tried Final Cut Pro X and it pains me to say that it took 4 hours to export a 14 minute 720p video. This mac is used for our school news and has to be a beast video editing machine. We have been saving money for quite some time and can now afford a decent upgrade. So my question to you is which would be the best upgrade for the money: a 21.5 inch iMac with an i7, 8gb RAM, and Fusion drive or the cheapest Mac Pro with after market SSD? This mac needs to be the fastest at Final Cut Pro X and able to usefully last us another 6 years. Please help I am forever perplexed with this!

    Id go for the I7 with minimum 8 Gig ram and as much SSD as your money will buy.
    It's not only about having a fast CPU but just as important is to get the data in and out as fast a possible.
    Without a real fast drive your CPU will spend a lot of time just waiting for the data to get to it and then wait more time while it has to write back to the drive.
    You can always add a slower drive for archiving and large amounts storage.
    Cheers!

  • Which one is the Right Approach - Re Using Page Layouts or adding Web parts directly to pages instead of page layouts in SP 2013 online site ?

    Hi Team,
    I am SharePoint 2013 developer. Before asking question I would like to explain the requirement in clarity.
    We are developing one O365 SharePoint online site which is having 10 different page layouts with different and some common webparts and we are reusing these page layouts by deploying a sandboxed solution which is having page layouts. These page layouts contain
    filter and query to display data using some condition and predefined values. By using these layouts we have to create 100+ pages. All pages will show data according to that page name and category (if it belongs any). We have not written any code to develop
    the site, everything is OOB feature.
    We have used below feature/list/lib of SP Online 2013:
    - Document Library
    - Survey
    - Calendar
    - Lists
    - Discussion forum library
    - OOB Search feature
    Now, I would like to know whether this is the right approach to reuse the page layouts. Or can we add webparts directly where the logic resides in the webparts and add them to the pages instead of page layouts?
    Also how to deploy page layouts/pages from one server to another? Currently we are deploying everything as a feature using sandbox solution. 
    Could you please let me know the right approach to follow. I am asking this question because we are facing below issues:
    - Sometime page layouts gets corrupted, showing nothing.
    - All written filters/logic disappear when we open layouts in Designer
    - Deployment is pain using Sandbox solution
    Waiting for your reply.
    Thanks in Advance,
    Shifa Mittal

    Interesting question, to which i do not know a definitive answer.
    Output Caching is used to increase performance and to reduce load on the box. However since you're in the MS Cloud you don't need to worry about the latter and MS do the worrying about the former. I wouldn't be surprised if MS have updated the behaviour
    for O365 and not updated the documentation. That first office.com article is using 2010 screenshots which doesn't fill me with confidence about it's continuing relevance. Don't forget that SharePoint 2013 and O365 are technically different versions and have
    very different behaviour in places.
    Generally i'd say not to worry, but in this case if you do find any more information i'd love to see it.

  • What is the better approach to populate Ref cursor into ADF business Component

    HI All,
    I have a requirement where I get the search results by joining more than 10 tables for my search criteria.
    What is the best approach to populate into ADF BCs.
    I have to go with Stored Procedure approach where I input the search Criteria and get the cursor back from the same.
    If I populate VO programmatically then the filtering at column level is a challenge.
    Anyone Please help me. Thanks in Advance.
    Thanks,
    Balaji Mucheli.

    The number of tables to be joined for a VO's query shouldn't matter - if the query is valid and fixed.
    Are you saying that you have logic to decide which tables to join to create the correct query?  I admit that would be a difficult thing to do, other than with a programmatic VO.  However, there are two possible solutions for doing this without a programmatic VO.
    Instead of your procedure returning a REF CURSOR, you can create an object type (CREATE TYPE) and a nested table of that type.  Then you have a function that returns an instance of the nested table.  The query in the VO is SELECT my_column... FROM TABLE(CAST my_table_function(:bind_variable) AS my_table_type).  You may have trouble getting the VO to accept this query as valid SQL, but it IS valid and it DOES work.
    Create a VO to be the master VO - it should have all the attributes that you intend to use, but the query can be anything, even a SELECT from DUAL.  Then create other VOs that subclass the master VO - one for each major variation of the query.  When you use the VO, use the data control for the master VO to create your pages, but change the pageDef to make the VO name a variable (with expression language).  Then you can set that variable to the correct child VO for the query that needs to execute.

Maybe you are looking for