Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Strategy Pattern and code duplication

  1. #1
    Junior Member jbraque's Avatar
    Join Date
    May 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Strategy Pattern and code duplication

    Hello everyone,
    I've implemented a strategy pattern for reading an XML file in two (for now) cases. In the next image i show what differs and what is the same in both strategies.
    Drawing1.jpg
    Well the classes are not so big but still there is duplicate code. What would you suggest for eliminating duplicate code?


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    313
    My Mood
    Bored
    Thanks
    46
    Thanked 87 Times in 73 Posts
    Blog Entries
    4

    Default Re: Strategy Pattern and code duplication

    I use abstract classes for this kind of thing. 'AbstractXMLReader' would implement all the 'same code' bits and provide an abstract method definition. Then 'LocalXMLReader' and 'RemoteXMLReader' would provide the concrete implementation.

  3. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    jbraque (November 24th, 2013)

  4. #3
    Junior Member jbraque's Avatar
    Join Date
    May 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Strategy Pattern and code duplication

    Oh yes you are right, i will try it. Thanks!

Similar Threads

  1. [SOLVED] pattern matching
    By shenaz in forum Java Theory & Questions
    Replies: 3
    Last Post: April 8th, 2013, 10:03 AM
  2. A poker program that gives advice based on pre set strategy...
    By mcbgun in forum Java Theory & Questions
    Replies: 0
    Last Post: December 19th, 2012, 09:19 AM
  3. Regular Expression pattern - complex pattern syntax
    By SmartAndy in forum Algorithms & Recursion
    Replies: 3
    Last Post: June 7th, 2011, 04:40 AM