Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 83 additions & 2 deletions design/pattern/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ a package per pattern inside it. Plain Kotlin, no framework; adapters come as mo
`semantic-pattern-spring` for Spring.

In place so far: a registry with its store, a registrar and a factory in the creational group;
the contracts of Composite, Decorator and Proxy and the annotations of Facade in the structural one;
a resolver and the base types of Visitor in the behavioral one; the contracts of Plugin in the enterprise one.
the base type of Adapter, the contracts of Composite, Decorator and Proxy and the annotations of
Facade in the structural one; a resolver and the base types of Visitor in the behavioral one; the
contracts of Plugin in the enterprise one.

## Packages

Expand All @@ -21,6 +22,7 @@ Packages of `semantic-pattern`, under `pro.vlprojects.semantic.pattern`:
| `creational.registry.error` | `InvalidNameException`, `InstanceAlreadyExistsException`, `InstanceNotFoundException`, `DefaultInstanceNotFoundException`; `InstanceRegistrationException` of a registrar |
| `creational.factory` | `Factory`, an instance made of its definition; `Factory.Definition`, the marker of a definition |
| `creational.factory.error` | `InvalidDefinitionException`; `MultipleFactoriesException`, `FactoryNotFoundException` of a registrar |
| `structural.adapter` | `Adapter`, the base type of an object adapter: equal to an adapter of its class over an equal adaptee; the adaptee is hidden from its clients |
| `structural.composite` | `Composite`, the base type of a hierarchy whose containers and leaves a client treats alike; `MutableComposite`, a container whose children change after it is made |
| `structural.decorator` | `Decorator`, a wrapper that holds its component and adds a responsibility to it; `DecoratorFactory`, a component wrapped in a decorator of its type |
| `structural.facade` | `Facade`, an entry to the subsystems it names; `Facade.Subsystem`, marks the annotation that the classes of one subsystem carry |
Expand Down Expand Up @@ -129,6 +131,85 @@ startup, as `MBeanRegistrationException` of JMX wraps a failure of registration.
Registrars run in their order. A name that two of them take stops the startup, unless
the later one replaces it on purpose: a bean from code overrides a setting by `replace`.

## Adapter

Adapter, as the Gang of Four describe it, converts the interface of a class into the one a client
expects: a class that exists, a foreign one for the most part, works with code written for another
interface. Of its two forms the library takes the object adapter, which holds its adaptee. A class
adapter extends the adaptee, and Kotlin has no private inheritance: every method of the adaptee
would be a method of the adapter for its clients.

| Role | Who plays it | What it holds |
| --- | --- | --- |
| Target | an interface the client expects | the operations the client calls |
| Client | code over the target | nothing of the adaptee |
| Adaptee | a class that exists and whose code stays as it is | the operations to adapt |
| Adapter | a class that extends `Adapter` and implements the target | the adaptee and the operations of the target over it |

The example of the book, a text view of a toolkit read as a shape of a drawing editor, with an area
in place of a bounding box:

```kotlin
interface Shape {

val area: Int
}

class TextShape(view: TextView) :
Adapter<TextView>(view),
Shape {

override val area
get() = this.adaptee.width * this.adaptee.height
}
```

`Adapter` fixes what each adapter would otherwise write by hand, or forget:

- The adaptee is `protected`, as `delegate()` of a Guava `ForwardingObject` is: a client reads the
target and cannot reach the adaptee through it.
- Two adapters of one class over equal adaptees are equal and share a hash code, as two instances
of a data class with one property are. A `Set`, a `Map` and `contains` hold them as one, though
an adapter that implements `Composite` makes new adapters of the children on each read of
`children`. `equals` and `hashCode` are final, so no subclass rewrites them.
- An adapter is not equal to its adaptee: an equality between two classes would hold one way only.
- `toString` is that of the adaptee, as in `ForwardingObject`.

`Adapter` is an abstract class, since an interface of Kotlin cannot implement `equals` and
`hashCode`. Where the target is an abstract class itself, the adapter cannot extend both and is
written by hand. The hash code follows the adaptee: a mutable adaptee changed while its adapter is
in a `HashSet` is lost there, as any element is.

An adapter holds nothing but its adaptee. One with state of its own, a scale or a charset, does
not extend `Adapter`: its equality would ignore that state.

The adaptee is an open property, as `identifier` of `AbstractEntity` is: a class proxy of Spring,
of a bean with `@Transactional` for one, cannot override the final `equals` and `hashCode`, and
they run on the proxy, whose own field is empty. Through the property they read the adaptee of the
target.

A function that wraps an adaptee is named `as…`, as `asList` of `Arrays` and `asSequence` of Kotlin
are: it returns a view of the same object, where `to…` returns a copy.

```kotlin
class JsonComposite(node: JsonNode) :
Adapter<JsonNode>(node),
Composite<JsonComposite> {

override val children
get() = this.adaptee
.values()
.map(::JsonComposite)
}

fun JsonNode.asComposite() = JsonComposite(this)
```

Adapter changes the interface of an object. Decorator keeps the interface and adds to what the
object does; Proxy keeps it and controls the access to the object. Bridge has the structure of an
object adapter, but parts an abstraction from its implementation up front, where Adapter joins
classes that exist.

## Composite

Composite, as the Gang of Four describe it, lets a client treat a container and a leaf
Expand Down
8 changes: 8 additions & 0 deletions design/pattern/core/api/semantic-pattern.api

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pro.vlprojects.semantic.pattern.structural.adapter

/** The base type of an object adapter: equal to one of its class over an equal [adaptee], hidden from clients. */
public abstract class Adapter<out A : Any>(adaptee: A) {

// Open, so that a class proxy of Spring reads the adaptee of its target, not an empty field.
protected open val adaptee: A = adaptee

final override fun equals(other: Any?): Boolean = when {
this === other -> true
other !is Adapter<*> -> false
this::class != other::class -> false
else -> other.adaptee == this.adaptee
}

final override fun hashCode(): Int = this.adaptee.hashCode()

override fun toString(): String = this.adaptee.toString()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package pro.vlprojects.semantic.pattern.unit.structural.adapter

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class AdapterTests {

private val view = TextView(width = 4, height = 2)

@Test
fun `an adapter answers the target through its adaptee`() {
assertEquals(8, TextShape(view).area)
}

@Test
fun `adapters of one class over equal adaptees are equal`() {
val shape = TextShape(view)
val same = TextShape(view.copy())

assertEquals(shape, same)
assertEquals(shape.hashCode(), same.hashCode())
assertEquals(1, setOf(shape, same).size)
}

@Test
fun `adapters over different adaptees are not equal`() {
val other = TextView(width = 2, height = 4).let(::TextShape)

assertNotEquals(TextShape(view), other)
}

@Test
fun `an adapter and its subclass over one adaptee are not equal either way`() {
assertNotEquals(TextShape(view), CaptionShape(view))
assertNotEquals(CaptionShape(view), TextShape(view))
}

@Test
fun `a container holds a child made again on another read of its children`() {
val docs = Folder("docs")
val root = Folder("root", listOf(docs)).let(::FolderComposite)
val child = root.children.first()

assertTrue(child in root.children)
}

@Test
fun `an adapter is not equal to its adaptee`() {
assertNotEquals(TextShape(view), view)
assertNotEquals(view, TextShape(view))
}

@Test
fun `an adapter reads as its adaptee`() {
assertEquals(view.toString(), TextShape(view).toString())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package pro.vlprojects.semantic.pattern.unit.structural.adapter

class CaptionShape(view: TextView) : TextShape(view)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package pro.vlprojects.semantic.pattern.unit.structural.adapter

data class Folder(val name: String, val folders: List<Folder> = emptyList())
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pro.vlprojects.semantic.pattern.unit.structural.adapter

import pro.vlprojects.semantic.pattern.structural.adapter.Adapter
import pro.vlprojects.semantic.pattern.structural.composite.Composite

class FolderComposite(folder: Folder) :
Adapter<Folder>(folder),
Composite<FolderComposite> {

override val children
get() = this.adaptee.folders.map(::FolderComposite)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package pro.vlprojects.semantic.pattern.unit.structural.adapter

interface Shape {

val area: Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pro.vlprojects.semantic.pattern.unit.structural.adapter

import pro.vlprojects.semantic.pattern.structural.adapter.Adapter

open class TextShape(view: TextView) :
Adapter<TextView>(view),
Shape {

override val area
get() = this.adaptee.width * this.adaptee.height
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package pro.vlprojects.semantic.pattern.unit.structural.adapter

data class TextView(val width: Int, val height: Int)
Loading