Param-Ganak
Param-Ganak

Reputation: 5875

Which design pattern should I use to construct GUI in java as well as in following scenario?

I am developing a standalone application in Java using swing API. I need experts guidance in following scenario.

I have a UI which shows some information of an employee.

There are four Operation in a Menu like add emp, edit emp, view emp,delete emp.

I want to use the same GUI for all four actions.

I designed a class which create GUI for above; I have used Singleton design pattern to construct GUI.

As depending on selected operation; in GUI some componants gets disabled or removed or added more and then GUI gets Constructed and shown to User.

I was thinking of passing a string describing the operation to GUI construncor and then do the above things related to componants.But as I have used the Singleton Design pattern its not possible.

What should I do in above scenario, to complete all my requirement? Which design pattern you suggest to me for above scenario and as well as for constructing the GUI?

Please guide me experts!

Upvotes: 1

Views: 543

Answers (3)

Marcelo
Marcelo

Reputation: 4608

Create an Enum with the operations:

public enum Operation{
  ADD, EDIT, VIEW, DELETE
}

And in your GUI create a method:

public void setOperation(Operation op){
    case ADD: ...
    case EDIT: ...
    case VIEW: ...
    case DELETE: ...
}

Other option would be to have sort of a GUI inheritance:

abstract class EmployeeView
class AddEmployeeView extends EmployeeView
class EditEmployeeView extends EmployeeView
class ViewEmployeeView extends EmployeeView
class DeleteEmployeeView extends EmployeeView

So your abstract class would have the required fields and the subclasses would just enable/disable them.

Upvotes: 1

romje
romje

Reputation: 660

as mentionned wisely by duffymo I would suggest to try to get a higher abstraction view with D.P because they should appear intuitively as evidence , if they don't appear it implies that they are not obviously well suited... Singleton is not the best one and brings many constraints.... I have a strange impress while rading your question...why using the same view for 4 different use cases ? I guess you won't find any magic pattern... Of course you could design a very generic screen and using observer/observable pattern trying to hide/unhide some widgets but it may become very tricky to maintain and for which gain?

HTH jerome

Upvotes: 1

StanislavL
StanislavL

Reputation: 57381

Create a EmployeeModel class to keep all the fields of employee. Add one more field e.g. int or enum to keep desired action.

In the JDialog (or JFrame) check the action field and depending on the action show buttons and/or enable disable controls which represent the fields.

Upvotes: 2

Related Questions