We can chain some handlers so when one handler can't do the job send to other. Pointer(H1) is a reference not a true pointer, because H1 is a named group. A Pointer((H1)) make a copy of H1 and then return a true pointer.
a=Pointer(H1) is same as a->H1
and
a=Pointer((H1)) is same as a->(H1)
if alfa is a class then a->alfa() is a true pointer because alfa() isn't a named group.
From a class we get a group as value and we create a group not a pointer to group. We can use Pointer() when we pass a parameter of type Group and we want to pass a pointer, either a reference or a true pointer. We can pass a reference using &H1 for H1, but using Pointer(H1) we pass it for later use. If H1 not exist, the pointer will be invalid. A true pointer always hold the object alive. This isn't true for a named group, an object which we define in a module or function. This group deleted when then module exit from execution. Named groups may belong to other groups, so the lifes of that groups depends from the parent's group.
class Request {
Private:
aValue, aDescription$
Public:
function getDescription$ {
=.aDescription$
}
function getValue {
=.aValue
}
Class:
module Request (.aDescription$, .aValue) {
}
}
class Handler {
Private:
succesor=pointer()
Public:
module setSuccessor (handler as *Handler) {
.succesor<=handler
}
module handleRequest {
error "Abstract"
}
}
class ConcreteHandlerOne as Handler {
module handleRequest (request as Request) {
if request.getValue() < 0 then
Report "Negative values are handled by ConcreteHandlerOne "+request.getDescription$()
Report Format$("Value: {0}", request.getValue())
Else
.succesor=>handleRequest request
end if
}
}
class ConcreteHandlerTwo as Handler {
module handleRequest (request as Request) {
if request.getValue() = 0 then
Report "Zero values are handled by ConcreteHandlerTwo: "+request.getDescription$()
Report Format$("Value: {0}", request.getValue())
Else
.succesor=>handleRequest request
end if
}
}
class ConcreteHandlerThree as Handler {
module handleRequest (request as Request) {
if request.getValue() > 0 then
Report "Positive values are handled by ConcreteHandlerThree: "+request.getDescription$()
Report Format$("Value: {0}", request.getValue())
end if
}
}
h1 =ConcreteHandlerOne()
h2 =ConcreteHandlerTwo()
h3 =ConcreteHandlerThree()
h1.setSuccessor pointer(h2)
h2.setSuccessor pointer(h3)
\\ Send requests to the chain
h1.handleRequest Request("Negative Value ", -1)
h1.handleRequest Request("Zero Value ", 0)
h1.handleRequest Request("Positive Value ", 1)
h1.handleRequest Request("Posιtive Value ", 2)
h1.handleRequest Request("Negative Value ", -5)
Private:
aValue, aDescription$
Public:
function getDescription$ {
=.aDescription$
}
function getValue {
=.aValue
}
Class:
module Request (.aDescription$, .aValue) {
}
}
class Handler {
Private:
succesor=pointer()
Public:
module setSuccessor (handler as *Handler) {
.succesor<=handler
}
module handleRequest {
error "Abstract"
}
}
class ConcreteHandlerOne as Handler {
module handleRequest (request as Request) {
if request.getValue() < 0 then
Report "Negative values are handled by ConcreteHandlerOne "+request.getDescription$()
Report Format$("Value: {0}", request.getValue())
Else
.succesor=>handleRequest request
end if
}
}
class ConcreteHandlerTwo as Handler {
module handleRequest (request as Request) {
if request.getValue() = 0 then
Report "Zero values are handled by ConcreteHandlerTwo: "+request.getDescription$()
Report Format$("Value: {0}", request.getValue())
Else
.succesor=>handleRequest request
end if
}
}
class ConcreteHandlerThree as Handler {
module handleRequest (request as Request) {
if request.getValue() > 0 then
Report "Positive values are handled by ConcreteHandlerThree: "+request.getDescription$()
Report Format$("Value: {0}", request.getValue())
end if
}
}
h1 =ConcreteHandlerOne()
h2 =ConcreteHandlerTwo()
h3 =ConcreteHandlerThree()
h1.setSuccessor pointer(h2)
h2.setSuccessor pointer(h3)
\\ Send requests to the chain
h1.handleRequest Request("Negative Value ", -1)
h1.handleRequest Request("Zero Value ", 0)
h1.handleRequest Request("Positive Value ", 1)
h1.handleRequest Request("Posιtive Value ", 2)
h1.handleRequest Request("Negative Value ", -5)
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου
You can feel free to write any suggestion, or idea on the subject.