Exam Task Identify Design Pattern

Moderatoren: pmueller, SE - Design and Construction

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Exam Task Identify Design Pattern

Beitrag von AizazZaidee » 14. Jul 2018 13:00

Code: Alles auswählen

trait Statement {
var errorListeners = Set.empty[(Statement) => Unit]
def registerErrorListener(f: (Statement) => Unit) = {errorListeners += f}
def fireInterpretationError(stat: Statement) = {errorListeners foreach( f => f(stat))}
def interpret(env: Map[Symbol , Int]): Statement
def accept(visitor: Visitor): Unit
}
case class InterpretedStatement(val result: Option[Int], val original: Statement) extends Statement{
override def interpret(env: Map[Symbol , Int]): Statement = result match {
case Some(result) => this
case None => original.interpret(env)
}
override def accept(visitor: Visitor): Unit = visitor.visit(this)
}
case class Return(val value: Symbol) extends Statement {
override def interpret(env: Map[Symbol , Int]): Statement = env.get(value) match {
case Some(value) => InterpretedStatement(Some(value), this)
case None => fireInterpretationError(this); InterpretedStatement(None , this)
}
override def accept(visitor: Visitor): Unit = visitor.visit(this)
}
case class Assignment(val name: Symbol , val value: Int, val next: Statement) extends Statement {
override def interpret(env: Map[Symbol , Int]): Statement = next.interpret(env.+(name -> value))
override def accept(visitor: Visitor): Unit = visitor.visit(this)
}
case class Condition(val lhs: Symbol , val rhs: Symbol , val check: (Int, Int) => Option[Boolean]){
def interpretCondition(env: Map[Symbol , Int]): Option[Boolean] = (env.get(lhs), env.get(rhs)) match {
case (Some(lhs), Some(rhs)) => check(lhs, rhs)
case _ => None
}
}
object Condition{
def createGreaterThan(lhs: Symbol , rhs: Symbol): Condition = Condition(lhs, rhs, (l, r) => Some(l > r))
}
case class Branch(val cond: Condition , val thenBranch: Statement , val elseBranch: Statement) extends Statement{
override def interpret(env: Map[Symbol , Int]): Statement = cond.interpretCondition(env) match {
case Some(true) => thenBranch.interpret(env)
case Some(false) => elseBranch.interpret(env)
case None => fireInterpretationError(this); this
}
override def accept(visitor: Visitor): Unit = visitor.visit(this)
}
trait Visitor {
def visit(ret: Return): Unit
def visit(intSmt: InterpretedStatement): Unit
def visit(assign: Assignment): Unit
def visit(branch: Branch): Unit
}
a) "Pattern Recognition" max 14P
Identify all design patterns used in the previous Scala code.
For each pattern instance, shortly state which classes are responsible for what role in the pattern. If the pattern has
methods that are characteristic, name the corresponding methods in the code.

I am able to identify Observer, Strategy, Visitor, Composition, anyone can help with other 10 design patterns?

VSchü
Windoof-User
Windoof-User
Beiträge: 26
Registriert: 5. Jul 2015 16:46

Re: Exam Task Identify Design Pattern

Beitrag von VSchü » 14. Jul 2018 13:30

It's 14 points, not 14 patterns. I guess InterpretedStatement is a decorator, so there's Decorator Pattern. I think together with yours that's all I found and I wasn't even sure if Composite counts as it's missing all of the child management. :?

Does anyone know which patterns are considered to be conflicting in this task?

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: Exam Task Identify Design Pattern

Beitrag von AizazZaidee » 14. Jul 2018 14:13

If I remember correctly professor said there will be one more design pattern than the points so to help students if someone miss one design pattern.so it's 14 points then should be 15 design patterns !!!

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: Exam Task Identify Design Pattern

Beitrag von AizazZaidee » 14. Jul 2018 14:14

so there's Decorator Pattern
.
Where is that?

VSchü
Windoof-User
Windoof-User
Beiträge: 26
Registriert: 5. Jul 2015 16:46

Re: Exam Task Identify Design Pattern

Beitrag von VSchü » 14. Jul 2018 14:31

Nah, he meant you need to identify only let's say 4/5 or 5/6 patterns to get full points, not that there would be in absolute numbers be more patterns than points.
Regarding Decorator Pattern: Statement is Component, InterpretedStatement is Decorator/ConcreteDecorator, Return and Assignment are ConcreteComponents.

megermaier
Neuling
Neuling
Beiträge: 3
Registriert: 15. Jul 2018 12:04

Re: Exam Task Identify Design Pattern

Beitrag von megermaier » 15. Jul 2018 12:38

Why decorator? There is no additional behaviour in any heritage of Statement :?:

VSchü
Windoof-User
Windoof-User
Beiträge: 26
Registriert: 5. Jul 2015 16:46

Re: Exam Task Identify Design Pattern

Beitrag von VSchü » 15. Jul 2018 13:05

InterpretedStatement is a transparent layer around other Statements that changes the behaviour of interpret, isn't it? Or did I misunderstand you?

megermaier
Neuling
Neuling
Beiträge: 3
Registriert: 15. Jul 2018 12:04

Re: Exam Task Identify Design Pattern

Beitrag von megermaier » 15. Jul 2018 13:46

I think there is missing a concrete decorator which inherits the base methods of the decorator. And if we would say, that InterpreterState is your Decorator, it should have an aggregation towards the component it inherits from. But in your case we would have an aggregation between a concrete component and the decorator. Not sure about that, because I'm not sure if it is in fact an aggregation.

VSchü
Windoof-User
Windoof-User
Beiträge: 26
Registriert: 5. Jul 2015 16:46

Re: Exam Task Identify Design Pattern

Beitrag von VSchü » 15. Jul 2018 13:57

If you only have one single decorator (InterpretedStatement), you do not necessarily need an abstract Decorator class. And InterpretedStatement has in fact an aggregation to the Component (Statement):

case class InterpretedStatement(val result: Option[Int], val original: Statement) extends Statement

megermaier
Neuling
Neuling
Beiträge: 3
Registriert: 15. Jul 2018 12:04

Re: Exam Task Identify Design Pattern

Beitrag von megermaier » 15. Jul 2018 14:11

Hmm, guess you are right. Thank you.

eichberg
Moderator
Moderator
Beiträge: 448
Registriert: 25. Sep 2007 12:12
Kontaktdaten:

Re: Exam Task Identify Design Pattern

Beitrag von eichberg » 16. Jul 2018 07:56

the number of points and the number of patterns do not correlate; an identification of the template pattern will give less points than an identification of the, e.g., decorator pattern...

Furthermore, when you identify a pattern make sure that the structure and behavior match.

Antworten

Zurück zu „Software Engineering - Design and Construction“