Harsha
Harsha

Reputation: 1879

Adapter pattern for exisiting application

I have an application which is written long back. Part of the application gets the string(which is a date) from the DB and display it on the screen.

Since DATE is converted to string and then stored in the DB. While displaying it, string(DATE) is retrived from db and displayed on the Winform.

Now, We have a requrement where we need to support french customers and french follow -DDMMYYYY format. I thought of using a adpater pattern between the DB layer and UI layer. The adapter which converts the DATE format based on the regional settings.

  1. My question is, in this senario, I have to create an object of this adapter. Since I am opening the class for modification. Is not violating the Open-Closed principle.

  2. is the right way to use the adapter pattern. Is it OK to use the adapter patter when client application is in use(in this case people are using it from last 5 years).

  3. Or any other ways to change the feature to support customer's requirments.

Thank you, Harsha

Upvotes: 0

Views: 327

Answers (2)

radarbob
radarbob

Reputation: 5101

The adapter pattern is for converting an existing interface into an interface a client needs/expects. But it sounds like all you need is the date reformatted - it's still a string. This then does not sound like a job for adapter-man per se.

Using the adapter pattern, in theory you should not have to open your existing class. An adapter wraps the to-be-adapted class and exposes the new interface. I'm imagining that the pain is that the adapter class will have to pass through all your unchanged public stuff too, because the client ideally "talks to" the adapter only, not the original class.

A general solution

The general idea is to reformat the date just prior to the UI control being set to the value. I'd hope your UI control classes have events that trigger just before an actual OnChange event.

Is your application in C#? Does it use .NET Binding classes?

The .NET Binding class has 2 very, very handy events: Parse and Format. Their purpose in life is to allow you to change the format, value, type, whatever of a value as it goes back and forth. You should not need to modify any existing interfaces or existing method code. You add event handlers to reformat the date string.

Open/Closed Principle is not absolute
If you have a very good reason to open your class for modification then OK. But keep in mind some implied goals of Open/Closed:

  • Don't change the existing interface(s)
  • Don't change existing behavior. We're adding new stuff, we don't want to accidentally change existing stuff.
  • Modifying code is more error prone than writing new code.

Open/Closed is not an excuse to:

  • NOT refactor bad code to implement a sane fix
  • Make bad fix decisions for the sake of Open/Closed

Upvotes: 1

Mairbek Khadikov
Mairbek Khadikov

Reputation: 8089

It is a good practice to separate your domain logic from the presentation logic and persistence.

And the adaptor between DB layer and UI layer you talking about seems to be a domain model.

Upvotes: 0

Related Questions