## Qs to exe10 solution

tz33quvu
Windoof-User
Beiträge: 40
Registriert: 29. Jan 2017 09:26

### Qs to exe10 solution

Hi all,

I am redoing the exes and found in exe10 there are something not so clear to me.

1)In the build() of taskbuilder, it has:

Code: Alles auswählen

    def addDependency(task: Task): TaskBuilder = {
require(dependence.isEmpty)
this
}

So is that means if the dependence is defined, then this function will stop and throw an exception?
And the definition of dependence is Option[Task] , so why :

Code: Alles auswählen

def build(): Task = {
startDate.orElse(Some(Instant.now())), context, priority)
dependence.foreach { dependece ⇒
}
rate.foreach { rate ⇒
}
}

Here we apply a foreach for dependence, but actually dependence is not a list, so here foreach is equals to get?
And this build function seems not able to build a task like:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3)An other additional question, if I defined a companion object for a trait, then this companion object works for all subclasses of this trait?

4)In task 9, if we do not define the child management method in the trait but only in the subGraph, then can we consider class edge as a special composite which only contains child access but no add/remove method?

5) Suppose I have a piece of codes like this:

Code: Alles auswählen

trait task{
def name
def accept(v:visitor)
}
}

trait visitor
{
}

Should I consider dependenttask as a violation of LSP because it did not def accept method.

6) scala Obejct is not the super type of Seq? So where is Object's position in scala's type hierarchy?

7) According to the rectangle and squre example shown in the slides, could we also consider it is a violation of ISP?

meichholz
Endlosschleifenbastler
Beiträge: 167
Registriert: 30. Mär 2016 08:28

### Re: Qs to exe10 solution

Hi,
tz33quvu hat geschrieben:
10. Feb 2018 14:45
So is that means if the dependence is defined, then this function will stop and throw an exception?
Yes, this exactly what require() does.
tz33quvu hat geschrieben:
10. Feb 2018 14:45
And this build function seems not able to build a task like:
Yes, this Builder is not able to create a such a structure.
We left the require calls in the methods of the TaskBuilder intentionally.
tz33quvu hat geschrieben:
10. Feb 2018 14:45
Here we apply a foreach for dependence, but actually dependence is not a list, so here foreach is equals to get?
Option.foreach is defined as of Scala 2.12.3 with the following:

Code: Alles auswählen

        def foreach[U](f: A => U) {
if (!isEmpty) f(this.get)
}

It is not the same as get, as it does nothing in the case that the Option is None.
tz33quvu hat geschrieben:
10. Feb 2018 14:45
This is an omission on our part.

tz33quvu hat geschrieben:
10. Feb 2018 14:45
An other additional question, if I defined a companion object for a trait, then this companion object works for all subclasses of this trait?

What do you mean here with 'work' exactly? Scala companion objects are similar to the static parts of a class in Java (there is more to it), e.g.

Code: Alles auswählen

object Foo {
private var x = 0
var y = 0
}

trait Foo {
Foo.x = 1 // does compile
Foo.y = 1 // does compile
}

class Bar extends Foo {
Foo.x = 2 // does not compile since x is private
Foo.y = 2 // does compile
}

tz33quvu hat geschrieben:
10. Feb 2018 14:45
In task 9, if we do not define the child management method in the trait but only in the subGraph, then can we consider class edge as a special composite which only contains child access but no add/remove method?
No. The difference here(at least in the solution) is that the Edge only represents itself, i.e. you have to add the corresponding nodes to the graph by themselves (which you can argue is a design issue), whereas the idea for the subgraph is that you only add it to another graph and all GrapElements that are part of the Subgraph are automatically part of that other Graph.
tz33quvu hat geschrieben:
10. Feb 2018 14:45
Should I consider dependenttask as a violation of LSP because it did not def accept method.
LSP is not violated here. Why do you think this is the case? This is just a bad implementation of the Visitor Pattern, since calling accept on a dependenttask will call the wrong method on the visitor, i.e. the visit method for task and not dependenttask.
tz33quvu hat geschrieben:
10. Feb 2018 14:45
scala Obejct is not the super type of Seq? So where is Object's position in scala's type hierarchy?
AnyRef corresponds to java.lang.Object. Maybe this helps: [url]https://docs.scala-lang.org/tour/unified-types.html[\url]. Seq is a subtype of AnyRef.

Best,
Matthias

tz33quvu
Windoof-User
Beiträge: 40
Registriert: 29. Jan 2017 09:26

### Re: Qs to exe10 solution

Hi,

In my 4th question, if we change the definition of Edge like

Code: Alles auswählen

class Edge(var node1, GraphElement, var node2: GraphElement) extends GraphElement{...}

Then can we consider edge as a composite? Because now we can flexibly change the left part and right part of edge.

And I think you missed my 7th question

Should we write down AnyRef and Any in the linearization steps(also in the final result)?

And what is the difference between Evt and Event? also Event.apply() and Evt.fire()?

Thanks!

meichholz
Endlosschleifenbastler
Beiträge: 167
Registriert: 30. Mär 2016 08:28

### Re: Qs to exe10 solution

Hi,
tz33quvu hat geschrieben:
12. Feb 2018 12:48
In my 4th question, if we change the definition of Edge like

class Edge(var node1, GraphElement, var node2: GraphElement) extends GraphElement{...}
Then can we consider edge as a composite? Because now we can flexibly change the left part and right part of edge.
The definition of the Edge is not the deciding Factor here. It is fine to only allow certain Leaf classes in a Composite (e.g. Node in our case). The actual usage and intention is relevant here. A subgraph represents all its elements. An Edge does not. It is a single Element that refers to other Elements(Nodes) that are already part of the Graph (at least in our solution). For example:
If you want 2 Nodes that are connected with an Edge you need to add both the Nodes and the Edge to that Graph. If you have a Subgraph that consists of two Nodes and want to add those to a Graph you only have to add that Subgraph.
According to the rectangle and squre example shown in the slides, could we also consider it is a violation of ISP?
No, maybe you could explain your reasoning (and maybe refer to a concrete slide) why you think this is the case.

tz33quvu hat geschrieben:
12. Feb 2018 12:48
Should we write down AnyRef and Any in the linearization steps(also in the final result)?
Yes, but you will also get an example of how to write it down.

tz33quvu hat geschrieben:
12. Feb 2018 12:48
And what is the difference between Evt and Event? also Event.apply() and Evt.fire()?
Evt can be fired Event not. Evt.apply() is just and alias for Evt.fire(), however, Event.apply() is not related to this. Event.apply() can used inside of Event expressions, which are similar to Signal expressions (using the apply method in Signal expressions allows you to refer to the current value of the respective Signal). I showed them at some point in the solution of one of the exercises, however, they have not been covered in the lecture.

Best,
Matthias

tz33quvu
Windoof-User
Beiträge: 40
Registriert: 29. Jan 2017 09:26

### Re: Qs to exe10 solution

Hi,

Take the example in the lecture

Code: Alles auswählen

trait Rectangle
{
def setWdith
def setHeight
def area
}
class Square extends Rectangle
{
def setWidth
def setHeight
}

It violates LSP. And I think it also violates ISP, I can do it like

Code: Alles auswählen

trait shape
{
def area()
}
class Rectangle extends shape{def setWdith, def setHeight}
class Square extends shape{def setLength}

Is that right?

And there may be an error in RP2 silde 30 where
val s: Signal[Option[Int]] = e.latestOption(e)
it should be changed to e.latestOption() I think

meichholz
Endlosschleifenbastler
Beiträge: 167
Registriert: 30. Mär 2016 08:28

### Re: Qs to exe10 solution

Hi,
tz33quvu hat geschrieben:
12. Feb 2018 16:13
Take the example in the lecture

Code: Alles auswählen

trait Rectangle
{
def setWdith
def setHeight
def area
}
class Square extends Rectangle
{
def setWidth
def setHeight
}

It violates LSP. And I think it also violates ISP, I can do it like

Code: Alles auswählen

trait shape
{
def area()
}
class Rectangle extends shape{def setWdith, def setHeight}
class Square extends shape{def setLength}

Is that right?
Your solution here solves the violation of the LSP (exactly as in the lecture), by extracting a shape interface.
tz33quvu hat geschrieben:
12. Feb 2018 16:13
And there may be an error in RP2 silde 30 where
val s: Signal[Option[Int]] = e.latestOption(e)
it should be changed to e.latestOption() I think
That's true.

Best,
Matthias