I have code in Swift.
enum QuestionnaireViewMode {
case add(input: Add)
case edit(input: Edit)
enum Add {
case building(input: BuildingInput)
case car(input: CarInput)
case park
struct BuildingInput {
let address: String
let placeName: String
}
struct CarInput {
let name: String
}
}
enum Edit {
case profile(input: ProfileInput)
struct ProfileInput {
let name: String
}
}
}
This is enum class which very easy to use, for example i can create different type of object just like this: .add(input: .car(input: .init(name: "bmw"))). But for me not clear enum classes in kotlin, i found some similar - sealed class and i tryed converted to:
sealed class QuestionnaireViewMode {
sealed class add(input: Add)
sealed class edit(input: Edit)
sealed class Add {
sealed class building(input: BuildingInput)
sealed class car(input: CarInput)
sealed class park
data class BuildingInput(val address: String, val placeName: String)
data class CarInput(val name: String)
}
sealed class Edit {
sealed class profile(input: ProfileInput)
data class ProfileInput(val name: String)
}
}
is this correct?
Your sealed classes need to extend their parent. And you are using sealed class instead of fun for your builder functions, which doesn’t make sense. But since these are classes you don’t need that because you can call the constructors directly. Also, since you have no commonly shared state, these can be sealed interfaces instead of sealed classes, which is a little simpler because you don’t have to worry about constructors.
Here is how I would design it:
sealed interface QuestionnaireViewMode {
sealed interface Add: QuestionaireViewMode {
data class BuildingInput(val address: String, val placeName: String): Add
data class CarInput(val name: String): Add
}
sealed interface Edit: QuestionaireViewMode {
data class ProfileInput(val name: String): Edit
}
}
Usage:
val myInput: QuestionaireViewMode =
QuestionaireViewMode.Add.CarInput(“bmw”)
Related
I want to ask a question that I have some clues about, but I don't want to influence the answers I will get. I have the following class hierarchy:
abstract class MyAbstractClass {
fun displayStuff(id: String) {
println("My id is $id.")
}
}
interface MyInterface {
fun displayThis() {
displayStuff("some-value")
}
fun displayStuff(id: String) // Not implemented here
}
class MyConcreteClass(): MyAbstractClass(), MyInterface {
fun doStuff() {
displayThis()
}
}
fun main() {
val result = MyConcreteClass()
result.doStuff()
result.displayStuff("id")
}
What's wrong with this design, and how do you suggest I fix it?
It would probably not be a bad idea to extract the displayStuff into another interface. Then MyAbstractClass and MyInterface can both derive from the same interface.
One overrides the displayStuff function, hence providing something like an abstract base implementation for the interface.
The other one is using the function in a specific way, thereby extending the functionality of the interface.
interface DisplayStuff {
fun displayStuff(id: String)
}
abstract class MyAbstractClass: DisplayStuff {
override fun displayStuff(id: String) = println("My id is $id.")
}
interface MyInterface : DisplayStuff {
fun displayThis() = displayStuff("some-value")
}
It is code worked in java but after convert to kotlin it does not compile.
Having a base class which has some defines as static protected member in the companion object:
abstract class ParentClass {
companion object {
#JvmField
final protected val SERVICE_TYPE_A = "the_service_type_a"
}
}
and the child class:
class ChildClass: ParentClass {
public override fun getServiceType(): String {
return SERVICE_TYPE_A. //<== got compile error
}
}
it does not compile.
how to access a parent class static protected member from subclass?
You need to use #JvmStatic instead as follows:
abstract class ParentClass {
companion object {
#JvmStatic
protected val SERVICE_TYPE_A = "the_service_type_a"
}
abstract fun getServiceType(): String
}
The final keyword in SERVICE_TYPE_A is redundant since everything is final by default in Kotlin. This also mean that if you want ParentClass to be extended, then you need to explicitly define it as open.
Then your ChildClass would look as follows:
class ChildClass: ParentClass() {
override fun getServiceType(): String {
return SERVICE_TYPE_A
}
}
I have a problem to chose the between an abstract class without abstract methods OR a base class with an interface.
I have two implementation in my mind:
1.
Let's say I have a AbstractRenderer:
abstract class AbstractRenderer
{
protected $shape;
public function __construct(AbstractShape $shape)
{
$this->shape = $shape;
}
public function render(): string
{
return $this->shape->generate()->asArray();
}
}
and the WebRenderer would be like this:
class WebRenderer extends AbstractRenderer
{
}
2.
Have a base class and an interface like this:
Interface InterfaceRenderer
{
public function __construct(AbstractShape $shape);
public function render(): string;
}
and a base class that impediments the interface:
class BaseRenderer implements InterfaceRenderer
{
protected $shape;
public function __construct(AbstractShape $shape)
{
$this->shape = $shape;
}
public function render(): string
{
return $this->shape->generate()->toString();
}
}
again, my WebRenderer would be like this:
class WebRenderer extends BaseRenderer
{
}
I don't know which is the correct implementation, or there is a better way to implement this and what is the pros and cons of each.
Thanks
From the Renderer client’s perspective the 2 solutions are basically identical. As long as they depend on an abstract object (interface or an abstract class), you’ll have benefits of polymorphism. You’d lose those if you make them depend on WebRenderer (concrete object).
Interface’s benefits over abstract classes
doesn’t occupy inheritance
no fragile base class problem
Abstract classes provide
static methods (in many languages interface can’t have these)
protected implementation
In Kotlin I have an abstract class that other classes can inherit from. I would like to have some functions that the class that inherits this class can optionally implement. In the code below, the function is protected abstract. This however requires that the class that is inheriting this class MUST implement these functions. Is there a way to make it so that the class that is inheriting can choose to implement the functions or not implement them?
abstract class BaseDialogFragment {
protected abstract fun getButton1Text(): String
protected abstract fun getButton2Text(): String
}
It is very simple, you just provide the default implementation like in the example below and your inheritors can override them:
abstract class BaseDialogFragment {
open fun getButton1Text(): String {
TODO("Your default implementation here")
}
open fun getButton2Text(): String {
TODO("Your default implementation here")
}
}
Hi I would like know why the following example doesn't work
abstract class BaseClass {
}
class ConcretClasOne : BaseCalculator {
}
class ConcretClasTwo : BaseCalculator {
}
abstract class BaseRun {
abstract fun run(param: BaseClass): Int
}
class ConcretRun : BaseRun {
override fun run(param: ConcretClasOne): Int {
return 0
}
}
this shows me a message run overrides nothing.
I suppose that kotlin isn't able to match the abstract class and the concrete implementation, but what other alternative is there to emulate this behavior, that the run method in the concrete class ConcretRun should receive a concrete param ConcretClasOne?
Generics
Using generics, you can make the base class have a type extending the base class, so that the run method can take that type in.
abstract class BaseClass {
}
class ConcretClasOne: BaseCalculator {
}
class ConcretClasTwo: BaseCalculator {
}
abstract class BaseRun<T: BaseClass> {
abstract fun run(param: T): Int
}
class ConcretRun: BaseRun<ConcretClasOne> {
override fun run(param: ConcretClasOne): Int {
return 0
}
}
Why your code doesn't work
At the moment you are trying to override a method with a more specific type, but as the more general base method can accept more types the more specific method cannot override it.