Did you know ... Search Documentation:
Pack logtalk -- logtalk-3.77.0/examples/design_patterns/structural/decorator/SCRIPT.txt

This file is part of Logtalk https://logtalk.org/ SPDX-FileCopyrightText: 1998-2023 Paulo Moura <pmoura@logtalk.org> SPDX-License-Identifier: Apache-2.0

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

% start by loading the design pattern sample implementations:

| ?- logtalk_load(design_patterns('structural/decorator/loader')). ...

% send the string/0 message to the decorator object:

| ?- colored_shape(circle, red)::string.

A circle of radius 10.0 which is colored red yes

% the diameter/1 predicate in not defined for shape or for % colored_shape; thus our decorator forwards the message % to the decorated circle:

| ?- colored_shape(circle, red)::diameter(Diameter).

Diameter = 20.0. yes

% same queries but with a dynamically created object:

| ?- create_object(Circle, [extends(circle)], [], [radius(7.0)]).

Circle = o1 yes

| ?- colored_shape(o1, blue)::string.

A circle of radius 7.0 which is colored blue yes

| ?- colored_shape(o1, blue)::diameter(Diameter).

Diameter = 14.0. yes

% we can define a pipeline of decorators; e.g. a colored, named % shape:

| ?- create_object(NamedShape, [extends(named_shape)], [], [shape(colored_shape(o1,blue)), name(thingy)]).

NamedShape = o2 yes

| ?- o2::string.

A circle of radius 7.0 which is colored blue which is named thingy yes

| ?- o2::diameter(Diameter).

Diameter = 14.0. yes

% same queries using the decorator defined in the source file:

| ?- my_named_shape::string.

A circle of radius 10.0 which is colored green which is named Mr. Round yes

| ?- my_named_shape::diameter(Diameter).

Diameter = 20.0. yes