TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle...
Transcript of TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle...
![Page 1: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/1.jpg)
TDDB84: Lecture 09SOLID, Language design, Summary
fredag 11 oktober 13
![Page 2: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/2.jpg)
SOLID
•Single responsibility principle
• Open/closed principle
• Liskov substitution principle
• Interface segregation principle
• Dependency inversion principle
fredag 11 oktober 13
![Page 3: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/3.jpg)
SOLID
•Single responsibility principle
• Open/closed principle
• Liskov substitution principle
• Interface segregation principle
• Dependency inversion principle
fredag 11 oktober 13
![Page 4: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/4.jpg)
Single responsibility• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled design
fredag 11 oktober 13
![Page 5: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/5.jpg)
Single responsibility• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled design
fredag 11 oktober 13
![Page 6: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/6.jpg)
Single responsibility• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled design
fredag 11 oktober 13
![Page 7: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/7.jpg)
Open/closed• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled designfredag 11 oktober 13
![Page 8: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/8.jpg)
Open/closed• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled designfredag 11 oktober 13
![Page 9: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/9.jpg)
Liskov substitution• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled designfredag 11 oktober 13
![Page 10: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/10.jpg)
Liskov substitution• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled designfredag 11 oktober 13
![Page 11: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/11.jpg)
Interface segregation• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled designfredag 11 oktober 13
![Page 12: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/12.jpg)
Interface segregation• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled designfredag 11 oktober 13
![Page 13: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/13.jpg)
Dependency inversion• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled designfredag 11 oktober 13
![Page 14: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/14.jpg)
Dependency inversion• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled designfredag 11 oktober 13
![Page 15: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/15.jpg)
Dependency inversion• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled designfredag 11 oktober 13
![Page 16: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/16.jpg)
Dependency inversion• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled designfredag 11 oktober 13
![Page 17: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/17.jpg)
• Encapsulate what varies
• Strive for loosely-coupled design
fredag 11 oktober 13
![Page 18: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/18.jpg)
• Encapsulate what varies
• Strive for loosely-coupled design
Too general?
fredag 11 oktober 13
![Page 19: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/19.jpg)
• Encapsulate what varies
• Strive for loosely-coupled design
Too general?
A consequence of applying SOLID?
fredag 11 oktober 13
![Page 20: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/20.jpg)
• Encapsulate what varies
• Strive for loosely-coupled design
Too general?
A consequence of applying SOLID?
Too trivial?
fredag 11 oktober 13
![Page 21: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/21.jpg)
Language Designvs
Design Patterns
fredag 11 oktober 13
![Page 22: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/22.jpg)
• Open classes: The ability to redefine classes
• Metaprogramming: The ability to inspect and manipulate programs and their processes from within the language
• Multiple dispatch: The ability to select a function based on the runtime types of all arguments
• First-order functions: The ability to use functions as objects
Language features
fredag 11 oktober 13
![Page 23: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/23.jpg)
Open classes
fredag 11 oktober 13
![Page 24: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/24.jpg)
Open classes public static string ToXML(this object objectToSerialize) { MemoryStream mem=new MemoryStream(); XmlSerializer ser=new XmlSerializer(objectToSerialize.GetType()); ser.Serialize(mem,objectToSerialize); ASCIIEncoding ascii=new ASCIIEncoding(); return ascii.GetString(mem.ToArray()); }
public static class ObjectExtensions { ! ! ! 1.ToXML ();! ! ! new Person ().ToXML ();
C#
fredag 11 oktober 13
![Page 25: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/25.jpg)
Open classes public static string ToXML(this object objectToSerialize) { MemoryStream mem=new MemoryStream(); XmlSerializer ser=new XmlSerializer(objectToSerialize.GetType()); ser.Serialize(mem,objectToSerialize); ASCIIEncoding ascii=new ASCIIEncoding(); return ascii.GetString(mem.ToArray()); }
public static class ObjectExtensions { ! ! ! 1.ToXML ();! ! ! new Person ().ToXML ();
C#
class Object
def to_xml vars = instance_variables.map do |var| name=var.to_s.sub('@','') "<#{name}>#{instance_variable_get(var)}</#{name}>" end.join(" ") "<root>#{vars}</root>" end
end
class Person attr_accessor :name, :ageend
p = Person.newp.name = "Ola"p.age = 34
irb(main):081:0> p.to_xml"<root><name>Ola</name> <age>34</age></root>"
Ruby
fredag 11 oktober 13
![Page 26: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/26.jpg)
Metaprogramming public class FieldMappingConfigurator<T> where T: class, new() { private Configurator Configurator { get; set; } private string FieldName { get; set; } internal FieldMappingConfigurator(string fieldName, Configurator configurator) { Configurator = configurator; FieldName = fieldName; }
public void To<T1>(Expression<Func<T,T1>> propertySelector) { var selectorExpression = (MemberExpression) propertySelector.Body; var prop = (PropertyInfo) selectorExpression.Member; Configurator.CustomFieldsToPropertiesMap[FieldName]=prop; } }
GenericDAO<DomainObject>.Configure("some_StoredProcedure") .Map("AFieldInTheResultSetReturned") .To(s => s.Id);
C#
fredag 11 oktober 13
![Page 27: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/27.jpg)
Metaprogramming public class FieldMappingConfigurator<T> where T: class, new() { private Configurator Configurator { get; set; } private string FieldName { get; set; } internal FieldMappingConfigurator(string fieldName, Configurator configurator) { Configurator = configurator; FieldName = fieldName; }
public void To<T1>(Expression<Func<T,T1>> propertySelector) { var selectorExpression = (MemberExpression) propertySelector.Body; var prop = (PropertyInfo) selectorExpression.Member; Configurator.CustomFieldsToPropertiesMap[FieldName]=prop; } }
GenericDAO<DomainObject>.Configure("some_StoredProcedure") .Map("AFieldInTheResultSetReturned") .To(s => s.Id);
C#
fredag 11 oktober 13
![Page 28: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/28.jpg)
Metaprogramming public class FieldMappingConfigurator<T> where T: class, new() { private Configurator Configurator { get; set; } private string FieldName { get; set; } internal FieldMappingConfigurator(string fieldName, Configurator configurator) { Configurator = configurator; FieldName = fieldName; }
public void To<T1>(Expression<Func<T,T1>> propertySelector) { var selectorExpression = (MemberExpression) propertySelector.Body; var prop = (PropertyInfo) selectorExpression.Member; Configurator.CustomFieldsToPropertiesMap[FieldName]=prop; } }
GenericDAO<DomainObject>.Configure("some_StoredProcedure") .Map("AFieldInTheResultSetReturned") .To(s => s.Id);
C#
fredag 11 oktober 13
![Page 29: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/29.jpg)
Metaprogramming public class FieldMappingConfigurator<T> where T: class, new() { private Configurator Configurator { get; set; } private string FieldName { get; set; } internal FieldMappingConfigurator(string fieldName, Configurator configurator) { Configurator = configurator; FieldName = fieldName; }
public void To<T1>(Expression<Func<T,T1>> propertySelector) { var selectorExpression = (MemberExpression) propertySelector.Body; var prop = (PropertyInfo) selectorExpression.Member; Configurator.CustomFieldsToPropertiesMap[FieldName]=prop; } }
GenericDAO<DomainObject>.Configure("some_StoredProcedure") .Map("AFieldInTheResultSetReturned") .To(s => s.Id);
Program analysisC#
fredag 11 oktober 13
![Page 30: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/30.jpg)
Metaprogrammingclass SetupProjectBuilder
class << self
def setup_classes @setup_classes ||= [] end
def inherited(sub_class) setup_classes << sub_class end
def setup_project?(project_file) !setup_class(project_file).nil? end
def setup_class(project_file) setup_classes.find {|c| c.project_file_pattern =~ project_file } end end
end
class VDProjBuilder < SetupProjectBuilder
def self.project_file_pattern /\.vdproj$/ end
[ ... ]
end
Ruby
fredag 11 oktober 13
![Page 31: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/31.jpg)
Metaprogrammingclass SetupProjectBuilder
class << self
def setup_classes @setup_classes ||= [] end
def inherited(sub_class) setup_classes << sub_class end
def setup_project?(project_file) !setup_class(project_file).nil? end
def setup_class(project_file) setup_classes.find {|c| c.project_file_pattern =~ project_file } end end
end
class VDProjBuilder < SetupProjectBuilder
def self.project_file_pattern /\.vdproj$/ end
[ ... ]
end
RubyProcess analysis
fredag 11 oktober 13
![Page 32: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/32.jpg)
Metaprogrammingclass SetupProjectBuilder
class << self
def setup_classes @setup_classes ||= [] end
def inherited(sub_class) setup_classes << sub_class end
def setup_project?(project_file) !setup_class(project_file).nil? end
def setup_class(project_file) setup_classes.find {|c| c.project_file_pattern =~ project_file } end end
end
class VDProjBuilder < SetupProjectBuilder
def self.project_file_pattern /\.vdproj$/ end
[ ... ]
end
RubyProcess analysis
Method-related hooks
method_missingmethod_addedsingleton_method_addedmethod_removedsingleton_method_removedmethod_undefinedsingleton_method_undefinedClass & Module Hooks
inheritedappend_featuresincludedextend_objectextendedinitialize_copyconst_missing
fredag 11 oktober 13
![Page 33: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/33.jpg)
Metaprogrammingclass SetupProjectBuilder
class << self
def setup_classes @setup_classes ||= [] end
def inherited(sub_class) setup_classes << sub_class end
def setup_project?(project_file) !setup_class(project_file).nil? end
def setup_class(project_file) setup_classes.find {|c| c.project_file_pattern =~ project_file } end end
end
class VDProjBuilder < SetupProjectBuilder
def self.project_file_pattern /\.vdproj$/ end
[ ... ]
end
RubyProcess analysis
Method-related hooks
method_missingmethod_addedsingleton_method_addedmethod_removedsingleton_method_removedmethod_undefinedsingleton_method_undefinedClass & Module Hooks
inheritedappend_featuresincludedextend_objectextendedinitialize_copyconst_missing
fredag 11 oktober 13
![Page 34: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/34.jpg)
Metaprogrammingclass SetupProjectBuilder
class << self
def setup_classes @setup_classes ||= [] end
def inherited(sub_class) setup_classes << sub_class end
def setup_project?(project_file) !setup_class(project_file).nil? end
def setup_class(project_file) setup_classes.find {|c| c.project_file_pattern =~ project_file } end end
end
class VDProjBuilder < SetupProjectBuilder
def self.project_file_pattern /\.vdproj$/ end
[ ... ]
end
RubyProcess analysis
Method-related hooks
method_missingmethod_addedsingleton_method_addedmethod_removedsingleton_method_removedmethod_undefinedsingleton_method_undefinedClass & Module Hooks
inheritedappend_featuresincludedextend_objectextendedinitialize_copyconst_missing
Factory Method
fredag 11 oktober 13
![Page 35: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/35.jpg)
Metaprogramming in Ruby
class YamlConf
def conf @conf ||= load(self.class.simple_name+".yml") end
# Look up keys in the configuration hash def method_missing(method,*args) method_name=method.to_s conf[method_name] end end
fredag 11 oktober 13
![Page 36: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/36.jpg)
Metaprogramming in Ruby
class YamlConf
def conf @conf ||= load(self.class.simple_name+".yml") end
# Look up keys in the configuration hash def method_missing(method,*args) method_name=method.to_s conf[method_name] end end
class Mail < YamlConf; end
fredag 11 oktober 13
![Page 37: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/37.jpg)
Metaprogramming in Ruby
class YamlConf
def conf @conf ||= load(self.class.simple_name+".yml") end
# Look up keys in the configuration hash def method_missing(method,*args) method_name=method.to_s conf[method_name] end end
class Mail < YamlConf; end from: "[email protected]"to: - "[email protected]" - "[email protected]"host: "smtp.somehost.com"
config/mail.yml
fredag 11 oktober 13
![Page 38: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/38.jpg)
Metaprogramming in Ruby
class YamlConf
def conf @conf ||= load(self.class.simple_name+".yml") end
# Look up keys in the configuration hash def method_missing(method,*args) method_name=method.to_s conf[method_name] end end
class Mail < YamlConf; end from: "[email protected]"to: - "[email protected]" - "[email protected]"host: "smtp.somehost.com"
config/mail.yml
mail = Mail.new do from Conf::Mail.from to Conf::Mail.to subject s body b end
fredag 11 oktober 13
![Page 39: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/39.jpg)
Metaprogramming in Ruby
class YamlConf
def conf @conf ||= load(self.class.simple_name+".yml") end
# Look up keys in the configuration hash def method_missing(method,*args) method_name=method.to_s conf[method_name] end end
class Mail < YamlConf; end from: "[email protected]"to: - "[email protected]" - "[email protected]"host: "smtp.somehost.com"
config/mail.yml
mail = Mail.new do from Conf::Mail.from to Conf::Mail.to subject s body b end
fredag 11 oktober 13
![Page 40: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/40.jpg)
Metaprogramming in Ruby
class YamlConf
def conf @conf ||= load(self.class.simple_name+".yml") end
# Look up keys in the configuration hash def method_missing(method,*args) method_name=method.to_s conf[method_name] end end
class Mail < YamlConf; end from: "[email protected]"to: - "[email protected]" - "[email protected]"host: "smtp.somehost.com"
config/mail.yml
mail = Mail.new do from Conf::Mail.from to Conf::Mail.to subject s body b end
fredag 11 oktober 13
![Page 41: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/41.jpg)
Metaprogramming in Ruby
class YamlConf
def conf @conf ||= load(self.class.simple_name+".yml") end
# Look up keys in the configuration hash def method_missing(method,*args) method_name=method.to_s conf[method_name] end end
class Mail < YamlConf; end from: "[email protected]"to: - "[email protected]" - "[email protected]"host: "smtp.somehost.com"
config/mail.yml
mail = Mail.new do from Conf::Mail.from to Conf::Mail.to subject s body b end
Proxyfredag 11 oktober 13
![Page 42: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/42.jpg)
Metaprogramming in Java public State() { ClassWithState obj = ClassWithState.this;
manipulateFields(obj, new FieldManipulator() {
@Override public void manipulateField(Object obj, Field field) throws IllegalAccessException {
getState().put(field.getName(), field.get(obj)); }
}); }
private void manipulateFields(Object obj, FieldManipulator fieldManipulator) { Class<?> cl = obj.getClass(); for (Field field : cl.getDeclaredFields()) { field.setAccessible(true); try { fieldManipulator.manipulateField(obj, field); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }
fredag 11 oktober 13
![Page 43: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/43.jpg)
Metaprogramming in Java public State() { ClassWithState obj = ClassWithState.this;
manipulateFields(obj, new FieldManipulator() {
@Override public void manipulateField(Object obj, Field field) throws IllegalAccessException {
getState().put(field.getName(), field.get(obj)); }
}); }
private void manipulateFields(Object obj, FieldManipulator fieldManipulator) { Class<?> cl = obj.getClass(); for (Field field : cl.getDeclaredFields()) { field.setAccessible(true); try { fieldManipulator.manipulateField(obj, field); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }
Mementofredag 11 oktober 13
![Page 44: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/44.jpg)
Metaprogramming in Java
@Override public Object invoke(Object arg0, Method m, Object[] arg2) throws Throwable { Object result = m.invoke(instance, arg2); if (m.getName().equals("getEmployments") && result == null) {
System.out.println(MessageFormat.format( "Trying to access uninstantiated field {0}", m .getName().substring(3))); [ some magic happens ] Method setter = m.getDeclaringClass().getMethod( "setEmployments", Collection.class); System.out.println(MessageFormat.format( "Instantiated field {0}", m.getName().substring(3))); setter.invoke(instance, employments); } return m.invoke(instance, arg2); }
fredag 11 oktober 13
![Page 45: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/45.jpg)
Metaprogramming in Java
@Override public Object invoke(Object arg0, Method m, Object[] arg2) throws Throwable { Object result = m.invoke(instance, arg2); if (m.getName().equals("getEmployments") && result == null) {
System.out.println(MessageFormat.format( "Trying to access uninstantiated field {0}", m .getName().substring(3))); [ some magic happens ] Method setter = m.getDeclaringClass().getMethod( "setEmployments", Collection.class); System.out.println(MessageFormat.format( "Instantiated field {0}", m.getName().substring(3))); setter.invoke(instance, employments); } return m.invoke(instance, arg2); }
Proxyfredag 11 oktober 13
![Page 46: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/46.jpg)
Multiple dispatch(defclass duck () ((name :initarg :name :accessor name)))(defclass mallard-duck (duck) ())(defclass red-duck (duck) ())(defclass rubber-duck (duck) ())
;; In another file, we can add flying behavior
;; What design pattern is this an implementation of?
(defclass fly-behavior () ())(defclass fly-with-wings (fly-behavior) ())(defclass no-fly (fly-behavior) ())
(defgeneric fly (duck fly-behavior) (:documentation "A method to allow ducks to fly") (:method ((d mallard-duck) (b fly-with-wings)) (format t "The mallard duck ~A is flying with wings!" (name d))) (:method ((d rubber-duck) (b fly-with-wings)) (format t "Rubber duck ~A is flying with wings!" (name d))) (:method ((d rubber-duck) (b no-fly)) (format t "Rubber duck ~A is not flying at all" (name d))) )
fredag 11 oktober 13
![Page 47: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/47.jpg)
Multiple dispatch(defclass duck () ((name :initarg :name :accessor name)))(defclass mallard-duck (duck) ())(defclass red-duck (duck) ())(defclass rubber-duck (duck) ())
;; In another file, we can add flying behavior
;; What design pattern is this an implementation of?
(defclass fly-behavior () ())(defclass fly-with-wings (fly-behavior) ())(defclass no-fly (fly-behavior) ())
(defgeneric fly (duck fly-behavior) (:documentation "A method to allow ducks to fly") (:method ((d mallard-duck) (b fly-with-wings)) (format t "The mallard duck ~A is flying with wings!" (name d))) (:method ((d rubber-duck) (b fly-with-wings)) (format t "Rubber duck ~A is flying with wings!" (name d))) (:method ((d rubber-duck) (b no-fly)) (format t "Rubber duck ~A is not flying at all" (name d))) )
Method description
fredag 11 oktober 13
![Page 48: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/48.jpg)
Multiple dispatch(defclass duck () ((name :initarg :name :accessor name)))(defclass mallard-duck (duck) ())(defclass red-duck (duck) ())(defclass rubber-duck (duck) ())
;; In another file, we can add flying behavior
;; What design pattern is this an implementation of?
(defclass fly-behavior () ())(defclass fly-with-wings (fly-behavior) ())(defclass no-fly (fly-behavior) ())
(defgeneric fly (duck fly-behavior) (:documentation "A method to allow ducks to fly") (:method ((d mallard-duck) (b fly-with-wings)) (format t "The mallard duck ~A is flying with wings!" (name d))) (:method ((d rubber-duck) (b fly-with-wings)) (format t "Rubber duck ~A is flying with wings!" (name d))) (:method ((d rubber-duck) (b no-fly)) (format t "Rubber duck ~A is not flying at all" (name d))) )
Method description
;; CL-USER> (fly (make-instance 'rubber-duck :name "Ducky") (make-instance 'fly-with-wings));; Rubber duck Ducky is flying with wings!;; NIL;; CL-USER> (fly (make-instance 'rubber-duck :name "Ducky") (make-instance 'no-fly));; Rubber duck Ducky is not flying at all
fredag 11 oktober 13
![Page 49: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/49.jpg)
Multiple dispatch(defclass duck () ((name :initarg :name :accessor name)))(defclass mallard-duck (duck) ())(defclass red-duck (duck) ())(defclass rubber-duck (duck) ())
;; In another file, we can add flying behavior
;; What design pattern is this an implementation of?
(defclass fly-behavior () ())(defclass fly-with-wings (fly-behavior) ())(defclass no-fly (fly-behavior) ())
(defgeneric fly (duck fly-behavior) (:documentation "A method to allow ducks to fly") (:method ((d mallard-duck) (b fly-with-wings)) (format t "The mallard duck ~A is flying with wings!" (name d))) (:method ((d rubber-duck) (b fly-with-wings)) (format t "Rubber duck ~A is flying with wings!" (name d))) (:method ((d rubber-duck) (b no-fly)) (format t "Rubber duck ~A is not flying at all" (name d))) )
Method description
;; CL-USER> (fly (make-instance 'rubber-duck :name "Ducky") (make-instance 'fly-with-wings));; Rubber duck Ducky is flying with wings!;; NIL;; CL-USER> (fly (make-instance 'rubber-duck :name "Ducky") (make-instance 'no-fly));; Rubber duck Ducky is not flying at all
Method dispatch
fredag 11 oktober 13
![Page 50: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/50.jpg)
Multiple dispatch(defclass duck () ((name :initarg :name :accessor name)))(defclass mallard-duck (duck) ())(defclass red-duck (duck) ())(defclass rubber-duck (duck) ())
;; In another file, we can add flying behavior
;; What design pattern is this an implementation of?
(defclass fly-behavior () ())(defclass fly-with-wings (fly-behavior) ())(defclass no-fly (fly-behavior) ())
(defgeneric fly (duck fly-behavior) (:documentation "A method to allow ducks to fly") (:method ((d mallard-duck) (b fly-with-wings)) (format t "The mallard duck ~A is flying with wings!" (name d))) (:method ((d rubber-duck) (b fly-with-wings)) (format t "Rubber duck ~A is flying with wings!" (name d))) (:method ((d rubber-duck) (b no-fly)) (format t "Rubber duck ~A is not flying at all" (name d))) )
Method description
;; CL-USER> (fly (make-instance 'rubber-duck :name "Ducky") (make-instance 'fly-with-wings));; Rubber duck Ducky is flying with wings!;; NIL;; CL-USER> (fly (make-instance 'rubber-duck :name "Ducky") (make-instance 'no-fly));; Rubber duck Ducky is not flying at all
Method dispatch
Strategyfredag 11 oktober 13
![Page 51: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/51.jpg)
(defclass expression () ())
(defclass compound-expression (expression) ((left :initarg :left :accessor left) (operator :initarg :operator :accessor operator) (right :initarg :right :accessor right)))
(defclass atomic (expression) ((value :initarg :value :accessor value)))
;;;;;;;; Independently of the classes above, we define a way to;;;;;;;; perform different operations on the objects of each class
;; What pattern would this correspond in the GoF book?
(defgeneric get-value (Expression) (:documentation "Calculates the value of the expression ") (:method ((self compound-expression)) (funcall (operator self) ! (get-value (left self)) ! (get-value (right self)))) (:method ((self atomic)) (value self)))
(defvar expression (make-instance 'compound-expression ! ! ! :left (make-instance 'atomic :value 3) ! ! ! :operator #'*! ! ! :right (make-instance 'atomic :value 4)))
(defvar expression2 (make-instance 'compound-expression! ! ! :left expression! ! ! :operator #'+! ! ! :right (make-instance 'atomic :value 5)! ! ! ))
(get-value expression2)
expression
compound-expression atomic
3
*
4
+
5
expression
expression2
fredag 11 oktober 13
![Page 52: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/52.jpg)
(defclass expression () ())
(defclass compound-expression (expression) ((left :initarg :left :accessor left) (operator :initarg :operator :accessor operator) (right :initarg :right :accessor right)))
(defclass atomic (expression) ((value :initarg :value :accessor value)))
;;;;;;;; Independently of the classes above, we define a way to;;;;;;;; perform different operations on the objects of each class
;; What pattern would this correspond in the GoF book?
(defgeneric get-value (Expression) (:documentation "Calculates the value of the expression ") (:method ((self compound-expression)) (funcall (operator self) ! (get-value (left self)) ! (get-value (right self)))) (:method ((self atomic)) (value self)))
(defvar expression (make-instance 'compound-expression ! ! ! :left (make-instance 'atomic :value 3) ! ! ! :operator #'*! ! ! :right (make-instance 'atomic :value 4)))
(defvar expression2 (make-instance 'compound-expression! ! ! :left expression! ! ! :operator #'+! ! ! :right (make-instance 'atomic :value 5)! ! ! ))
(get-value expression2)
expression
compound-expression atomic
3
*
4
+
5
expression
expression2
Visitorfredag 11 oktober 13
![Page 53: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/53.jpg)
First-order functions
[1,2,3].collect { |x| x+2 } => [3,4,5]
new List<int> () { 1, 2, 3 }.Select (x => x +2);=> { 3, 4, 5 }C#
Ruby
Lisp (mapcar #'(lambda (x) (+ x 2)) '(1 2 3))=> (3 4 5)
fredag 11 oktober 13
![Page 54: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/54.jpg)
First-order functions
[1,2,3].collect { |x| x+2 } => [3,4,5]
new List<int> () { 1, 2, 3 }.Select (x => x +2);=> { 3, 4, 5 }C#
Ruby
Lisp (mapcar #'(lambda (x) (+ x 2)) '(1 2 3))=> (3 4 5)
fredag 11 oktober 13
![Page 55: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/55.jpg)
First-order functions
[1,2,3].collect { |x| x+2 } => [3,4,5]
new List<int> () { 1, 2, 3 }.Select (x => x +2);=> { 3, 4, 5 }C#
Ruby
Lisp (mapcar #'(lambda (x) (+ x 2)) '(1 2 3))=> (3 4 5)
fredag 11 oktober 13
![Page 56: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/56.jpg)
First-order functions
[1,2,3].collect { |x| x+2 } => [3,4,5]
new List<int> () { 1, 2, 3 }.Select (x => x +2);=> { 3, 4, 5 }C#
Ruby
Lisp (mapcar #'(lambda (x) (+ x 2)) '(1 2 3))=> (3 4 5)
fredag 11 oktober 13
![Page 57: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/57.jpg)
First-order functions
[1,2,3].collect { |x| x+2 } => [3,4,5]
new List<int> () { 1, 2, 3 }.Select (x => x +2);=> { 3, 4, 5 }C#
Ruby
Lisp (mapcar #'(lambda (x) (+ x 2)) '(1 2 3))=> (3 4 5)
Strategyfredag 11 oktober 13
![Page 58: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/58.jpg)
Open classes vs Design Patterns
Behavioral
Structural
Creational
Memento
Command
Interpreter
Facade
Prototype
Flyweight
Visitor
State Templatemethod
Iterator
Abstract Factory
Composite
Builder
Singleton
Proxy
Mediator
Adapter
Bridge
Observer
Chain of responsibility
Factory method
Decorator
Strategy
Highlighted patterns simplifiedfredag 11 oktober 13
![Page 59: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/59.jpg)
Open classes vs Design Patterns
Behavioral
Structural
Creational
Memento
Command
Interpreter
Facade
Prototype
Flyweight
Visitor
State Templatemethod
Iterator
Abstract Factory
Composite
Builder
Singleton
Proxy
Mediator
Adapter
Bridge
Observer
Chain of responsibility
Factory method
Decorator
Strategy
Highlighted patterns simplifiedfredag 11 oktober 13
![Page 60: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/60.jpg)
Metaprogramming vs Design Patterns
Behavioral
Structural
Creational
Memento
Command
Interpreter
Facade
Prototype
Flyweight
Visitor
State Templatemethod
Iterator
Abstract Factory
Composite
Builder
Singleton
Proxy
Mediator
Adapter
Bridge
Observer
Chain of responsibility
Factory method
Decorator
Strategy
Highlighted patterns simplifiedfredag 11 oktober 13
![Page 61: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/61.jpg)
Metaprogramming vs Design Patterns
Behavioral
Structural
Creational
Memento
Command
Interpreter
Facade
Prototype
Flyweight
Visitor
State Templatemethod
Iterator
Abstract Factory
Composite
Builder
Singleton
Proxy
Mediator
Adapter
Bridge
Observer
Chain of responsibility
Factory method
Decorator
Strategy
Highlighted patterns simplifiedfredag 11 oktober 13
![Page 62: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/62.jpg)
Multiple dispatch vs Design Patterns
Behavioral
Structural
Creational
Memento
Command
Interpreter
Facade
Prototype
Flyweight
Visitor
State Templatemethod
Iterator
Abstract Factory
Composite
Builder
Singleton
Proxy
Mediator
Adapter
Bridge
Observer
Chain of responsibility
Factory method
Decorator
Strategy
Highlighted patterns simplifiedfredag 11 oktober 13
![Page 63: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/63.jpg)
Multiple dispatch vs Design Patterns
Behavioral
Structural
Creational
Memento
Command
Interpreter
Facade
Prototype
Flyweight
Visitor
State Templatemethod
Iterator
Abstract Factory
Composite
Builder
Singleton
Proxy
Mediator
Adapter
Bridge
Observer
Chain of responsibility
Factory method
Decorator
Strategy
Highlighted patterns simplifiedfredag 11 oktober 13
![Page 64: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/64.jpg)
First-order functions vs Design Patterns
Behavioral
Structural
Creational
Memento
Command
Interpreter
Facade
Prototype
Flyweight
Visitor
State Templatemethod
Iterator
Abstract Factory
Composite
Builder
Singleton
Proxy
Mediator
Adapter
Bridge
Observer
Chain of responsibility
Factory method
Decorator
Strategy
Highlighted patterns simplifiedfredag 11 oktober 13
![Page 65: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/65.jpg)
First-order functions vs Design Patterns
Behavioral
Structural
Creational
Memento
Command
Interpreter
Facade
Prototype
Flyweight
Visitor
State Templatemethod
Iterator
Abstract Factory
Composite
Builder
Singleton
Proxy
Mediator
Adapter
Bridge
Observer
Chain of responsibility
Factory method
Decorator
Strategy
Highlighted patterns simplifiedfredag 11 oktober 13
![Page 66: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/66.jpg)
Summary
fredag 11 oktober 13
![Page 67: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/67.jpg)
What is Good Design?
fredag 11 oktober 13
![Page 68: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/68.jpg)
• Encapsulate what varies
• Program to an interface, not to an implementation
• Favor composition over inheritance
• Classes should be open for extension but closed for modification
• Don’t call us, we’ll call you
• Depend on abstractions, do not depend on concrete classes
• Classes should only have one reason to change
• Strive for loosely-coupled design
fredag 11 oktober 13
![Page 69: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/69.jpg)
Or ...
fredag 11 oktober 13
![Page 70: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/70.jpg)
• Testable programs
Or ...
fredag 11 oktober 13
![Page 71: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/71.jpg)
• Testable programs
• Changeable programs
Or ...
fredag 11 oktober 13
![Page 72: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/72.jpg)
• Testable programs
• Changeable programs
Or ...
fredag 11 oktober 13
![Page 73: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/73.jpg)
• Testable programs
• Changeable programs
ChangeTest
Or ...
fredag 11 oktober 13
![Page 74: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/74.jpg)
What is a Design Pattern?
fredag 11 oktober 13
![Page 75: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/75.jpg)
What is a Design Pattern?
Why are there Design Patterns?
fredag 11 oktober 13
![Page 76: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/76.jpg)
What is a Design Pattern?
Why are there Design Patterns?
How are Design Patterns used/recognized?
fredag 11 oktober 13
![Page 77: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/77.jpg)
Why
fredag 11 oktober 13
![Page 78: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/78.jpg)
1. There are OO Programming Languages™
Why
fredag 11 oktober 13
![Page 79: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/79.jpg)
1. There are OO Programming Languages™
2. There are Common Problems™
Why
fredag 11 oktober 13
![Page 80: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/80.jpg)
1. There are OO Programming Languages™
2. There are Common Problems™
3. There are Design Principles™
Why
fredag 11 oktober 13
![Page 81: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/81.jpg)
1. There are OO Programming Languages™
2. There are Common Problems™
3. There are Design Principles™
➡1+2+3 = Design Pattern
Why
fredag 11 oktober 13
![Page 82: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/82.jpg)
How
fredag 11 oktober 13
![Page 83: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/83.jpg)
1. There are Structure Diagrams™
How
fredag 11 oktober 13
![Page 84: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/84.jpg)
1. There are Structure Diagrams™
2. There are A Bunch of Other Attributes™
How
fredag 11 oktober 13
![Page 85: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/85.jpg)
1. There are Structure Diagrams™
2. There are A Bunch of Other Attributes™
➡1+2 = Design Pattern
How
fredag 11 oktober 13
![Page 86: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/86.jpg)
Other Patterns
fredag 11 oktober 13
![Page 87: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/87.jpg)
Other PatternsSimpleton
The Simpleton Pattern is an extremely complex pattern used for themost trivial of tasks. The Simpleton is an accurate indicator of theskill level of its creator.
fredag 11 oktober 13
![Page 88: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/88.jpg)
Other PatternsSimpleton
The Simpleton Pattern is an extremely complex pattern used for themost trivial of tasks. The Simpleton is an accurate indicator of theskill level of its creator.
Commando
The Commando Pattern is used to get in and out quick, and get the jobdone. This pattern can break any encapsulation to accomplish itsmission. It takes no prisoners.
fredag 11 oktober 13
![Page 89: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/89.jpg)
Other PatternsSimpleton
The Simpleton Pattern is an extremely complex pattern used for themost trivial of tasks. The Simpleton is an accurate indicator of theskill level of its creator.
Commando
The Commando Pattern is used to get in and out quick, and get the jobdone. This pattern can break any encapsulation to accomplish itsmission. It takes no prisoners.
http://franksworld.com/blog/archive/2005/01/04/600.aspxfredag 11 oktober 13
![Page 90: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/90.jpg)
The Exam
fredag 11 oktober 13
![Page 91: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/91.jpg)
Oct 25th 8-12Bring written notes & the books. Justify all your
reasoning. Good luck!
fredag 11 oktober 13
![Page 92: TDDB84: Lecture 09TDDB84/lectures/2013/Lecture09/...SOLID •Single responsibility principle •Open/closed principle •Liskov substitution principle •Interface segregation principle](https://reader034.fdocuments.in/reader034/viewer/2022051811/601c221d0cc5787d3f652edc/html5/thumbnails/92.jpg)
Thank you!
fredag 11 oktober 13