001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005//     http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.ioc.services;
014
015import java.util.Map;
016
017import org.apache.tapestry5.commons.util.StrategyRegistry;
018
019/**
020 * A service implementation builder that operates around a {@link StrategyRegistry}, implementing a version of the Gang
021 * of Four Strategy pattern.
022 *
023 * The constructed service is configured with a number of adapters (that implement the same service interface). Method
024 * invocations on the service are routed to one of the adapters.
025 *
026 * The first parameter of each method is used to select the appropriate adapter.
027 *
028 * The ideal interface for use with this builder has only one method.
029 */
030public interface StrategyBuilder
031{
032    /**
033     * Given a number of adapters implementing the service interface, builds a "dispatcher" implementations that
034     * delegates to the one of the adapters. It is an error if any of the methods takes no parameters.
035     *
036     * @param <S>      the service interface type
037     * @param registry defines the adapters based on parameter type (of the first parameter)
038     * @return a service implementation
039     */
040    <S> S build(StrategyRegistry<S> registry);
041
042    /**
043     * @param registrations map frm class to the adapter type
044     * @param <S>
045     * @return the dispatcher
046     * @since 5.1.0.0
047     */
048    <S> S build(Class<S> adapterType, Map<Class, S> registrations);
049}