Anda di halaman 1dari 26

PI2133

Pemrograman Berorientasi
Objek

MVC dengan Java


Konsep MVC

VIEW CONTROL MODEL


[GUI] [Event- [Data
Handler] /Proses]
Previous
 We have build application that used
for handle-event in GUI based
application
 From previous code:
 The main idea is to split the code for
event-handler with the GUI-code
 We need to make Interface to make the
relation between class more flexible
About MVC [1]
 The model-view-controller (MVC)
architecture provides a set of design
patterns that help you separate the
areas of concern involved in building
and running a GUI-based application.
 The model encapsulates the business
logic and persistence code for the
application. The model should be as
view-technology-agnostic as possible.
About MVC [2]
 The view should display model objects
and contain presentation logic only.
There should be no business logic or
controller logic in the view
 The controller (along with its attending
logic) acts as the mediator between the
view and the model. The controller talks
to the model and delivers model objects
to the view to display.
About MVC [3]
 In an MVC architecture the controller
always selects the next view.

 What’s is the position of our code?


 Is it MVC model??
 How to make it MVC , can we ??
Step-By-Step [1]
 Defined M-V-C class
 1. Model Class
 From definition it imply that models class
must consist the business logic mechanism
and data persistence. In our case it consist
of Adding and Multiply mechanism, and
data about operand 1 and operand 2.
 Let’s Build this class and named it as
ModelCalc
Declare the Model Class
Step-By-Step [2]
 Defined M-V-C class
 2. View Class
 From definition it imply that View class has
responsibility as presentation logic (interact
with user). We already has this class from
the previous code [just use it]
TipeUTAMA

Tipe1 Tipe2
Declare the View Class
 The code is still same as previous
example, but we will make some
adjustment [later]
Step-By-Step [3]
 Defined M-V-C class
 3. Controller Class
 The controller (along with its attending logic) acts as
the mediator between the view and the model. The
controller talks to the model and delivers model objects
to the view to display
 Consider our previous controller => MyHandlerClass
does it match with above definition??
 In our previous controller, it has functionality to do
some business logic process => we already build model
class for the business logic process [let’s make
adjustment to the code]
Declare the Controller Class [1]
 Interaction between class

VIEW
[Tipe1]

MODEL
CONTROL
[ModelCalc]
[Event-
Handler]

VIEW
[Tipe2]
Declare the Controller Class [2]
 From the figure that depict the
interaction between MVC, we know
that the controller class must handle
2 View
 It mean there are a collection of view in
controller class
Declare the Controller Class [3]

 Now we must change the code for


method actionPerformed() => must
determine which view class that send
message ???
Declare the Controller Class [4]
 Wow, there is a problem!!
 we want to know which view is
responsible for the action => we can
only trace the event-source not the view
that contain that object ??
Declare the Controller Class [5]
 From the basic concept of MVC we
can see that controller class became
“center” of the model
 The code for implementation is
depend on our strategy to implement
it and the interaction that we design
 So, what’s the solution to our
problem??? => depend on what we
want to make
Solution
 What we want:
 If there is a change in Tipe1 then it
reflect to Tipe2 vice versa
 The event on click in View class only
update the model attr op1,op2 and
model set attr hasil base on operation
 Solution:
 Pure MVC => stick to our design
 Alternative MVC =>NEXT_LESSON
Solution
 We cancel the implementation of
ActionListener in Control class and
make it on View class (the interaction
between View and Control will be
done using message-passing on
actionPeformed on View class)
Final Code For Model Class
Final Code For Controller Class
Final Code For View Class[1]
Final Code For View Class[2]
Final Code For View Class[3]
Summary [1]
 The basic design-pattern use in this
model is Observer
 From above example, we can see that if
we purely using MVC model, it look very
“hard”. But we can take the essential
characteristics of MVC model that is
separation between the Model – View –
Controller
 The code that we build depend on the
application that we want to build
Summary [2]
 This model also adopted to make
Web-Based Application
 Many Framework implement this
model in different ways (Struts,
Spring, JSF, ZK, etc)
 The code for this lesson can be
download on usual link =>
DemoMVCVersi1.rar
Any Question ??