Download presentation
Presentation is loading. Please wait.
Published byMargaretMargaret Nicholson Modified over 10 years ago
1
Object-Oriented Design Patterns Composite Singleton State Observer … Autumn 2012UCN Technology: IT/Computer Science1
2
Autumn 2012UCN Technology: IT/Computer Science2 Composite Pattern The concept of patterns originates from architecture (Christopher Alexander, 1977): “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” (Christopher Alexander e. a.: “A Pattern Language”. Oxford University Press, New York, 1977.)
3
Autumn 2012UCN Technology: IT/Computer Science3 (OO) Design Patterns A well known and widely accepted concept in software engineering Developed in the early 1990s and published by Gamma e.a. (“Gang of Four”, GoF) in 1995: “(…) design patterns (…) are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.” (Erich Gamma e.a.:”Design Patterns. Elements of Reusable Object- Oriented Software”. Addison-Wesley. 1995.)
4
Autumn 2012UCN Technology: IT/Computer Science4 The benefits of patterns A pattern captures a proven good design: –A pattern is based on experience –A pattern is discovered – not invented It introduces a new (and higher) level of abstraction, which makes it easier: –to talk and reason about design on a higher level –to document and communicate design One doesn’t have to reinvent solutions over and over again Patterns facilitate reuse not only of code fragments, but of ideas.
5
Autumn 2012UCN Technology: IT/Computer Science5 Patterns as a learning tool It is often said that good skills in software construction require experience and talent …and neither can be taught or learned at school Patterns capture experience (and talent) in a way that is communicable and comprehensible …and hence experience can be taught (and learned) So one should put a lot of effort in studying patterns
6
Autumn 2012UCN Technology: IT/Computer Science6 Example: Composite-pattern Composite: Graphical editor, Word processor, Inventories etc.. (What will a Show()-method look like on Shape, Circle, Picture etc.) View CodeCode
7
Autumn 2012UCN Technology: IT/Computer Science7 public abstract class Shape { private int id; private String color; private int xpos; private int ypos; public void MoveTo(Position newPos){ // PRE none // POST pos'=newPos } public abstract void Show(); // PRE none // POST figuren er tegnet public abstract void Hide(); // PRE none // POST figuren er skjult public abstract float Area(); // PRE none // POST Computes the area with 4 digits. }//end Shape
8
Autumn 2012UCN Technology: IT/Computer Science8 public class Circle: Shape{ private int r; //radius public override void Show(){ //PRE none //POST the circle is drawn //Must be implemented using appropriate //graphical routines } public override void Hide(){ //PRE none //POST the circle is hidden //Must be implemented using appropriate //graphical routines } public virtual float Area(){ // PRE none // POST Computes the area with more than 4 digits } }//end Circle
9
Autumn 2012UCN Technology: IT/Computer Science9 public class Picture: Shape{ List pictList; // operations for adding shapes (removing etc.) public override void Show(){ //PRE none //POST The composite shape is drawn foreach(Shape s in pictList) s.Show(); } public override float Area() { float result=0; for(int i=0;i<pictList.Count i++) { result= result+pictList[i].Area(); } return result; }//end Picture Dynamic type definesshow() Static type
10
More Examples Organisational Structures (example)Organisational Structures Project Management (exercise)Project Management In the library: demos\RecursiveDirectoryTraverse demos\RecursiveDirectoryTraverse Autumn 2012UCN Technology: IT/Computer Science10
11
Architecture: Controller Object So far we have had our business logic and “user interface” in the Main method This obviously not a good design! Normally one will used what is known as a layered ( or n tier) architecture, for instance: –UI –Controller –Model –Database Autumn 2012UCN Technology: IT/Computer Science11 Application For now we focus on the application layer
12
More Banking View Code (Banking4View Code (Banking4) Autumn 2012UCN Technology: IT/Computer Science12
13
Design This design is based on the fact that accounts normally are accessed from Customer. (Hence the complicated and inefficient implementation of GetAllOwners). Autumn 2012UCN Technology: IT/Computer Science13
14
Singleton Pattern Used when we want to allow only one instance of an object: Autumn 2012UCN Technology: IT/Computer Science14
15
Other Patterns Observer Visitor Proxy State … Autumn 2012UCN Technology: IT/Computer Science15
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.