Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science Victoria University of Wellington Copyright: david streader, Victoria University of Wellington Simple Design COMP 112 2015.

Similar presentations


Presentation on theme: "Computer Science Victoria University of Wellington Copyright: david streader, Victoria University of Wellington Simple Design COMP 112 2015."— Presentation transcript:

1 Computer Science Victoria University of Wellington Copyright: david streader, Victoria University of Wellington Simple Design COMP 112 2015

2 © Peter Andreae COMP 112 :2 Outline Design is essential and informal and should be easy for customers to relate to. 1.Design for the moment and refactor Agile approach Do not guess the future just code for now – requires extensive executable tests to allow easy refactoring 2.Design for the future Traditional approach Code to subsequently make likely amendments easy to implement

3 © Peter Andreae COMP 112 :3 Customer language High level structure should be understandable with out reading all the detail. High level structure to reflect Customers language Design and protocol stacks! Design a stack of “models” a model at one layer of abstraction is implemented on the model beneath it. The model at each layer in the stack should be understandable on its own. Ideally you do not want the details in the lower layers to appear in at the layer you are working on.

4 © Peter Andreae COMP 112 :4 Private data Public Customers Data is private but access to the data is public Thus customers talk more abstractly then programmers As a part of a large algorithm a customer wants to: get the cost of item with known id. A programmer knows that the inventory of items is held in an ArrayList So a part of the large algorithm becomes: …….. for(Item p: inv){ if (p.getID() == id) { c = p.getCost(); break; } } ……

5 © Peter Andreae COMP 112 :5 How can you do better To the customer “get the cost of item with know id” is an atomic action. Can we design code to reflect this? Wrapper classes can hide data structures and provide functionality

6 © Peter Andreae COMP 112 :6 Why is this better! When the high level structure of your code reflect the customers world view then: By talking to the customer you can debug your design. Changes, described by the customer, are more easy to make. The basic customer concepts are often repeated in different situations over and over again. Hence less code duplication should result. Changing the underlying data structure e.g. from an ArrayList to a data base, only requires changing the Wrapper class.

7 © Peter Andreae COMP 112 :7 Some OO design principles DRY – Do not Repeat Yourself A class should do one thing and do it well Data encapsulation: means more than make fields private

8 © Peter Andreae COMP 112 :8 Objects as parameters When you pass an object as a parameter you are passing both data and code. Calling a method can: 1.Return a value 2.Change the state of the object 3.Interact with the environment Pure methods do not change the state of the object. Pure methods are functions mapping the parameters input to the value returned. An Object with one pure method is just like a function.

9 © Peter Andreae COMP 112 :9 Pass Functions in place of Objects A Predicate provides a function test from some domain (here Items) and returns a Boolean A Consumer provides a function accept from some domain and returns nothing (+side effect). Predicate is predefined - any one method object will work. Next we use lambdas when we call printSome Predicate is an Interface with one method test(Item i)

10 © Peter Andreae COMP 112 :10 Functions - Lambdas In place of defining an object with one method you can define a function in java: “ -> ” Examples p -> p.getID p -> p.getID == x z -> z.print() Lambdas can be used in place of an object with one method. Lambdas offer no new functionality but are shorter to code and easier to understand.

11 © Peter Andreae COMP 112 :11 Passing function not Objects Even though lambdas add no new functionality to Java because we are all lazy the easy of coding with lambdas changes how we code and even how we design!

12 © Peter Andreae COMP 112 :12 Stateless programming Functional programming : programming with functions : methods without side effects. Use functional language Use Java with streams and lambdas For example: int sum = wdts.stream().filter(b -> b.getColor() == RED).mapToInt(b ->b.getWeight()).sum(); Here we use widgets, a Collection, as a source for a stream. Are Java Lambdas Pure?

13 © Peter Andreae COMP 112 :13 Simple Design Metaphors Make your high level design closely reflect the language of the customer – domain expert. Use stateless programming to reduce side effects and hence errors. Custom build your own metaphor for the problem at hand Protocol stack Client Server Divide and conquer To do lists … The more clearly you understand the problem the easier it is to construct a good architecture for your code


Download ppt "Computer Science Victoria University of Wellington Copyright: david streader, Victoria University of Wellington Simple Design COMP 112 2015."

Similar presentations


Ads by Google