3

I am new to design pattern. I have a small project in which java classes use dummy data when not connected to server. I have if condition in class that switches between dummy data and server data depending on a flag . Is there a better way this can be implemented?

7
  • I would recommend the Bridge pattern Commented Mar 29, 2012 at 20:06
  • @LuiggiMendoza The Bridge pattern is really the last pattern that fits this purpose. The bridge pattern is used to isolate and decouple two subsystems and this has nothing to do with the OP's problem Commented Mar 29, 2012 at 20:16
  • Bridge Pattern: "Decouple an abstraction from its implementation so that the two can vary independently", maybe you're confusing the terms with Facade Pattern: "Provide a unified interface to a set of interfaces in a subsystem" Commented Mar 29, 2012 at 20:44
  • I would recommend you this book if you're interested in design patterns and you are a beginner: amazon.com/First-Design-Patterns-Elisabeth-Freeman/dp/… The First Pages of the book are available and describe the Strategy Pattern and that's what you want to look at (The Duck stuff). Commented Mar 29, 2012 at 21:00
  • @Gevorg Strategy pattern serves for encapsulates an algorithm, like sorting methods (quicksort, mergesort, radixsort and so on). OP problem is to detach the data access. This can be done with DAO and Bridge Pattern. Commented Mar 29, 2012 at 21:07

5 Answers 5

5

Instead of controlling your code with an 'if' statement, you should write an interface that defines all the methods you will need to interact with the server, and reference that interface instead of a concrete implementation. Then, have your 'dummy data' implement that interface.

The advantage of this is that your code will be written in a way that is not dependent upon the server implementation. This will allow you to change details on the server without changing your client's implementation.

Sign up to request clarification or add additional context in comments.

3 Comments

this can be achieved by the Bridge pattern
@LuiggiMendoza hmmm, isn't mtmurdock just describing the Strategy pattern in his answer?? ;)
@Gevorg Bridge Pattern is different than Strategy Pattern. The code is in C# but the concepts are explained and detailed to not get confused between both patterns.
1

I would recommend using the Repository pattern to encapsulate your data layer. Create an interface for the Repository and have two concrete implementations, one for dummy data and the other for server data. Use the Factory pattern to create your Repository. The Factory would return the correct concrete implementation of the Repository based on whether you are connected or not.

Comments

1

You need a Data Access Object, or an object which acts as a proxy between the requesting program and the data.

You ask the DAO for the data, and based on it's configuration, it responds with your server data, or other data. That other data might be newly instantiated classes, data from text files, etc.

In this image, the "Business Object" is your program, the "Data Access Object" is the reconfigurable gate-keeper, the "Transfer Object" is the object representation of the data requested, and the "Data Source" is the interface which you previously used to get to the data. Once the "Data Access Object" is in place, it is not hard to add code to it to "select" the desired data source (DummyDataSource, FileDataSource, JDBCDataSource, etc).

1 Comment

@LuiggiMendoza fixed the links, apparently they finally killed the java.sun.com address. Actually I like the www.corej2eepatterns.com address format better.
0

Proxy_pattern suits best for your use case.

In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes.

enter image description here

You can find explanation for tutorials point example here:

Proxy Design Pattern- tutorialspoint.com example

Have a look at below articles

oodesign proxy

tutorials point proxy

sourcemaking proxy example

dzone proxy example

Comments

-1

If you want a design pattern, then State pattern is what you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.