Skip to content

Instantly share code, notes, and snippets.

Avatar

Chiba Toshinori rinotc

View GitHub Profile
@rinotc
rinotc / english-name.ts
Created May 21, 2022
typescriptで値オブジェクト書いてみたサンプル
View english-name.ts
import {PreferNominal} from "../../prefer-nominal";
import * as E from "fp-ts/Either";
export class EnglishName {
// noinspection JSUnusedGlobalSymbols
readonly _englishNameBrand: PreferNominal;
constructor(readonly value: string) {
if (!EnglishName.isValid(value)) {
throw new TypeError(this.requirementErrorMessage);
View precondition-adt-sample2-CreateUserInteractor.scala
package sample2
import usecase.create.CreateUserOutput.UserNameConflict
import usecase.create.{CreateUserInput, CreateUserOutput, CreateUserUseCase}
class CreateUserInteractor(
userNameConflictValidator: UserNameConflictValidator,
userRepository: UserRepository
) extends CreateUserUseCase {
View precondition-adt-sample-use-adt-User.scala
package sample2
import user.{UserId, UserName, UserNameDuplicateError}
/**
* ユーザー
*
* @note デフォルトコンストラクタは `private` にして生成処理を絞っている。
* ADTを利用した状態遷移はインスタンスの生成方法の制御が重要である。
* 型パラメータを自由につけてインスタンスを生成できては、制約の効果がないためだ。
View precondition-adt-sample-Person.scala
class Person private (val firstName: String, val lastName: String, val age: Int)
object Person {
class Builder {
var firstName: String = ""
var lastName: String = ""
var age: Int = 0
private var hasFirstName: Boolean = false
private var hasLastName: Boolean = false
@rinotc
rinotc / precondition-adt-sample_first_CreateUserInteractor.scala
Created May 20, 2022
ドメインサービスでバリデーション入れた場合のサンプル
View precondition-adt-sample_first_CreateUserInteractor.scala
package first
import first.usecase.{CreateUserInput, CreateUserOutput, CreateUserUseCase}
final class CreateUserInteractor(userService: UserService, userRepository: UserRepository) extends CreateUserUseCase {
override def handle(input: CreateUserInput): CreateUserOutput = {
val newUser = User.create(input.name)
userService.exists(newUser) match { // このバリデーションが必ず必要だが、知らないとできない。
View PurchaseOrder.scala
package com.github.rinotc.purchaseSample.models.purchase
import com.github.rinotc.purchaseSample.models.money.Money
import com.github.rinotc.purchaseSample.models.part.Part
import com.github.rinotc.purchaseSample.models.{Aggregate, Entity}
import java.util.Currency
/**
* 購入注文
@rinotc
rinotc / TypeSafeBuilderSample.scala
Last active May 20, 2022
Generalized type constrains を使ったtype-safe builder
View TypeSafeBuilderSample.scala
class Person(
val firstName: String,
val lastName: String,
val age: Int
) {
protected def this() = this("", "", 0)
protected def this(pb: Person.Builder[_]) = this(pb.firstName, pb.lastName, pb.age)
}
object Person {
@rinotc
rinotc / ObjectCreationBuilderJavaLikeSample.scala
Last active Aug 1, 2021
Java like なビルダーパターンのサンプル
View ObjectCreationBuilderJavaLikeSample.scala
class NutrionFacts(builder: NutrionFacts.Builder) {
val servingSize: Int = builder.servingSize
val sergings: Int = builder.sergings
val calories: Int = builder.calories
val fat: Int = builder.fat
val sodium: Int = builder.sodium
val carbohydrate: Int = builder.carbohydrate
}
object NutrionFacts {
@rinotc
rinotc / ObjectCreationSampleCaseClass.scala
Created Aug 1, 2021
Scala で case class を使った場合の、デフォルトパラメータが必要な時のBuilder Patternサンプル
View ObjectCreationSampleCaseClass.scala
case class NutritionFacts(
servingSize: Int,
servings: Int,
calories: Int = 0,
fat: Int = 0,
sodium: Int = 0,
carbohydrate: Int = 0
)
val nf = NutritionFacts(1, 1, fat = 10)
@rinotc
rinotc / ObjectCreationBuilderPatternSample.java
Created Aug 1, 2021
オブジェクト生成、ビルダーパターンのサンプル
View ObjectCreationBuilderPatternSample.java
class NutritionFacts {
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
public static class Builder {
// 必須パラメータ