Nov 2005 MSc Slide 1 First we look at a standard Dos based application Combining Decorator & Factory Patterns.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Java Language Quick-Reference Guide B. Oracle10g: Java Programming B - 2 Console Output Java applications and applets can output simple messages to the.
Nov 2005 MSc Slide 1 - Another Example of a Structural Pattern Alternative way of adding Functionality to an existing class (alternative to a derived class)
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 Various Methods of Populating Arrays Randomly generated integers.
Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
CPSC 333: Foundations of Software EngineeringJ. Denzinger Small Test: Bank account manager System has to run on an automated teller machine. User must.
Requirements and Design
1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Behavioral Pattern: Template Method C h a p t e r 5 – P a g e 217 On occasion, two different components in a software system will have significant similarities,
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Fundamentals of Software Development 1Slide 1 Programming Patterns: Motivation Many problems fit a “pattern” that experienced software developers recognizeMany.
Object Concepts in C# Class, object, attribute, method, message, instance, encapsulation, polymorphism, inheritance, association, persistence, Generalization/specialization,
SOS OOP Fall 2001 Object Oriented Programming in Java Week 1 Read a design Design a small program Extract a design Run a VAJ program Change that program,
1 Advanced Material The following slides contain advanced material and are optional.
1 Lab Session-VI CS121 Fall 2000 l HW#2 Assigned l Multiple Choice Selectors l The While Loop l Switch-Case-Break Exercise l The Counter Controlled Loops.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
Inheritance Chapter 14. What is inheritance?  The ability to create a class from another class, the “parent” class, extending the functionality and state.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Introduction to Java Class Diagrams. Classes class Account { … } Account.
CS61B L03 Building Objects (1)Garcia / Yelick Fall 2003 © UCB  Dan Garcia ( Kathy Yelick  (
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Sep 2005 SDP-MSc Slide 1 Section 7 Object Oriented Programming.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
Information Systems Engineering
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Savitch - Chapter 11CS Extending the Class Capabilities Let’s extend the definition of the BankAccount class to illustrate some additional capabilities.
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
Chapter 13 ATM Case Study Part 2: Implementing an Object-Oriented Design Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Jeopardy Print Me Which loop? Call Me Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool, string.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Banking Service class BankingService { LinkedList accounts; LinkedList customers; double getBalance(int forAcctNum) { for (Account acct:accounts) { if.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Operational Semantics of Scheme
Methods Attributes Method Modifiers ‘static’
Packages, Interfaces & Exception Handling
What is an object? An object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the.
Functions Used to write code only once Can use parameters.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Decorator Pattern.
More on Classes and Objects
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Welcome back to Software Development!
CIS 199 Final Review.
Recursion Pepper.
Previous Lecture: Today’s Lecture: Reading (JV):
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Nov 2005 MSc Slide 1 First we look at a standard Dos based application Combining Decorator & Factory Patterns

Nov 2005 MSc Slide 2 class Account{ private int balance; public Account(int b) {balance=b;} public Account(Account acc) {balance=acc.balance;} public int read_bal() {return balance;} public void deposit(int amt) {balance +=amt;} public void withdraw(int amt) {balance -=amt;} } Combining Decorator & Factory Patterns

public class Ex1{ public static void Main(){ Account a = new Account(20); int choice=1,amt=0; string temp; while(choice!=4){ Console.WriteLine(); Console.WriteLine("1: Deposit"); Console.WriteLine("2: Withdraw"); Console.WriteLine("3: Read Bal"); Console.WriteLine("4: Exit"); Console.Write("Enter Choice:"); temp=Console.ReadLine(); choice=Convert.ToInt32(temp); :

switch(choice){ case 1: Console.Write("Enter Amount: "); temp=Console.ReadLine(); amt=Convert.ToInt32(temp); a.deposit(amt); break; case 2: Console.Write("Enter Amount: "); temp=Console.ReadLine(); amt=Convert.ToInt32(temp); a.withdraw(amt); break; case 3: int b=a.read_bal(); Console.WriteLine("Balance="+b); break; }}}} }

Nov 2005 MSc Slide 5 - we introduce trace printouts public void deposit(int amt){Cosole.WriteLine("deposit()"); balance +=amt;} During Testing Phase

Nov 2005 MSc Slide 6 class Account{ private int balance; public Account(int b) {balance=b;} public Account(Account acc) {balance=acc.balance;} public int read_bal() {Console.WriteLine(”read_bal()"); return balance;} public void deposit(int amt) {Console.WriteLine(”deposit()"); balance +=amt;} public void withdraw(int amt){ Console.WriteLine(”withdraw()"); balance -=amt;} }

Nov 2005 MSc Slide 7 Must be removed after testing What if you need to retest later Different test phases my require different types of traces Unsatisfactory Alternative Solution using Decorator Pattern

Nov 2005 MSc Slide 8 Account TestAcc SafeAcc DecTest DecoratorAccount UML Class Diagram

Nov 2005 MSc Slide 9 class Account{ private int balance; public Account(int b) {balance=b;} public Account(Account acc) {balance=acc.balance;} public int read_bal() {return balance;} public void deposit(int amt) {balance +=amt;} public void withdraw(int amt) {balance -=amt;} } Combining Decorator & Factory Patterns As Before

Nov 2005 MSc Slide 10 class Decorator : Account{ protected Account account; public Decorator(Account acc):base(acc){ account=acc;} } Combining Decorator & Factory Patterns

Nov 2005 MSc Slide 11 class TestAcc : Decorator{ public TestAcc(Account acc):base(acc){ } new public int read_bal(){ print("read_bal()"); return account.read_bal();} new public void deposit(int amt){print("deposit()"); account.deposit(amt);} new public void withdraw(int amt){ print("withdraw()"); account.withdraw(amt);} public void print(String s){ Console.WriteLine(s);} }

Nov 2005 MSc Slide 12 public class Ex1{ public static void Main(){ TestAcc a = new TestAcc(new Account(20)); int choice=1,amt=0; string temp; while(choice!=4){ Console.WriteLine(); Console.WriteLine("1: Deposit"); Console.WriteLine("2: Withdraw"); Console.WriteLine("3: Read Bal"); Console.WriteLine("4: Exit"); Console.Write("Enter Choice:"); temp=Console.ReadLine(); choice=Convert.ToInt32(temp); :

Trace Printout

public class Ex1{ public static void Main(){ Account a = new Account(20); int choice=1,amt=0; : After Testing we change back

Safe Account Seeks confirmation before Deposit, Withdraw We can create other decorators

class SafeAcc : Decorator{ public SafeAcc(Account acc):base(acc); {} new public int read_bal() {Console.Write("Enter 1 to confirm: "); string temp=Console.ReadLine(); int choice=Convert.ToInt32(temp); if (choice==1) return account.read_bal(); else return 0;} :

new public void deposit(int amt){ Console.Write("Enter 1 to confirm: "); string temp=Console.ReadLine(); int choice=Convert.ToInt32(temp); if (choice==1) account.deposit(amt);} new public void withdraw(int a) { Console.Write("Enter 1 to confirm: "); string temp=Console.ReadLine(); int choice=Convert.ToInt32(temp); if (choice==1) account.withdraw(a);} }

public class Ex1{ public static void Main(){ SafeAcc a = new SafeAcc(new Account(20)); int choice=1,amt=0; : After Testing we change back

Nov 2005 MSc Slide 19 Confirmation Requested

public class Ex1{ public static void Main(){ Account ac=new Account(20); TestAcc a = new TestAcc(new SafeAcc(null,ac),ac); int choice=1,amt=0; : Can combine these

Nov 2005 MSc Slide 21 Trace Printout and Confirmation Requested

Nov 2005 MSc Slide 22 public class Ex1{ public static void Main(){ Account ac=new Account(20); SafeAcc a = new SafeAcc(new TestAcc(null,ac),ac); int choice=1,amt=0; : Can combine these

Nov 2005 MSc Slide 23 Confirmation Requested then Trace Printout

class TestAcc : Decorator{ private SafeAcc sacc; public TestAcc(SafeAcc sacc,Account acc):base(acc) {this.sacc=sacc;} new public int read_bal(){ print("read_bal()"); if (sacc!=null) return sacc.read_bal(); else return account.read_bal();} new public void deposit(int amt){print("deposit()"); if (sacc!=null) sacc.deposit(amt); else account.deposit(amt);} : To achieve this we must modify code of decorators

class SafeAcc : Decorator{ private TestAcc tacc; public SafeAcc(TestAcc tacc,Account acc):base(acc) {this.tacc=tacc;} new public int read_bal(){ Console.Write("Enter 1 to confirm: "); string temp=Console.ReadLine(); int choice=Convert.ToInt32(temp); if (tacc!=null) return tacc.read_bal(); else return account.read_bal();} :

new public void deposit(int amt){ Console.Write("Enter 1 to confirm: "); string temp=Console.ReadLine(); int choice=Convert.ToInt32(temp); if (tacc!=null) tacc.deposit(amt); else account.deposit(amt);} new public void withdraw(int amt) { Console.Write("Enter 1 to confirm: "); string temp=Console.ReadLine(); int choice=Convert.ToInt32(temp); if (tacc!=null) tacc.withdraw(amt); else account.withdraw(amt);} }

1. Account a = new Account(bal); 2. SafeAcc a = new SafeAcc(null,new Account(20)); Or alternative Account ac=new Account(20); SafeAcc a = new SafeAcc(null,ac); 3.TestAcc a = new TestAcc(null,new Account(20)); 4.Account ac=new Account(20); TestAcc a = new TestAcc(new SafeAcc(null,ac),ac); Can add functionality by adding objects Options

Improve by using Factory Pattern Factory creates an Account object based on data In the following example base choice on user input Better solution would be to read value from a configuration file Could change the value in file after test etc First We must make some minor changes to introduce Polymorphism

using System; abstract class Decorator :Account{ protected Account account; public Decorator(Account acc):base(acc){ account=acc;} } class AccInterface{ virtual public int read_bal(){return 0;} virtual public void deposit(int amt){} virtual public void withdraw(int amt){}} :

using System; abstract class Decorator :Account{ protected Account account; public Decorator(Account acc):base(acc){ account=acc;} } class AccInterface{ virtual public int read_bal(){return 0;} virtual public void deposit(int amt){} virtual public void withdraw(int amt){}} :

class Account:AccInterface{ private int balance; public Account(int b) {balance=b;} public Account(Account acc) {balance=acc.balance;} override public int read_bal() {return balance;} override public void deposit(int amt) {balance +=amt;} override public void withdraw(int amt) {balance -=amt;} }

class TestAcc : Decorator{ private SafeAcc sacc; public TestAcc(SafeAcc sacc,Account acc):base(acc) {this.sacc=sacc;} override public int read_bal(){ print("read_bal()"); if (sacc!=null) return sacc.read_bal(); else return account.read_bal();} override public void deposit(int amt){print("deposit()"); if (sacc!=null) sacc.deposit(amt); else account.deposit(amt);} etc

public class Ex1{ public static void Main(){ Account ac=new Account(20); AccInterface a = new TestAcc(new SafeAcc(null,ac),ac); int choice=1,amt=0; etc Now we can use Polymorphism to get the same result

Now we can use a Factory to decide type of Account

public class Ex1{ public static void Main(){ Console.WriteLine("Enter Account Type:"); Console.WriteLine("1: Normal"); Console.WriteLine("2: Test"); Console.WriteLine("3: Safe"); Console.WriteLine("4: Safe and Test"); Console.Write("Enter Choice:"); string t=Console.ReadLine(); int option=Convert.ToInt32(t); AccInterface a = AccountFactory.createAccount(20,option); int choice=1,amt=0; (as before)

class AccountFactory{ static public AccInterface createAccount(int bal,int type){ Account ac=new Account(bal); if (type==1) return ac; if (type==2) return new TestAcc(null,ac); if (type==3) return new SafeAcc(null,ac); else return new TestAcc(new SafeAcc(null,ac),ac); }

Nov 2005 MSc Slide 37 Input to Factory

public class Ex1{ public static void Main(){ Console.Write("Enter Balance:"); string t=Console.ReadLine(); int bal=Convert.ToInt32(t); AccInterface a = AccountFactory.createAccount(bal); int choice=1,amt=0; : (as before) Could also have a Factory Based on a single parameter

class AccountFactory{ static public AccInterface createAccount(int bal){ if (bal < 100) return new Account(bal); else return new SafeAcc(null,new Account(bal)); }

Nov 2005 MSc Slide 40 Exercise1: Given the class class Counter{ private int value; public Counter(int v){value=v;} public Counter(Counter cc){ value=cc.value;} public int read_value(){return value;} public void increment(){value++;} public void decrement(){ value--;} } Now we want to decorate the Counter (similar to Account Ex)

Nov 2005 MSc Slide 41 (i) class UpperLimit extends Decorator{ same as before except increment will only increment if value <20 otherwise will print out “Too High” Hint: public void increment(){if (counter.read_value()<20) (ii) class LowerLimit extends Decorator{ same as before except decrement will only decrement if value >0 otherwise will print out “Too Low” (iii) Want a factory to decide on the of counter See next Dos screen

Nov 2005 MSc Slide 42

Nov 2005 MSc Slide 43 Exercise 1(b): Now Modify the Application so the Factory is based on just an initial counter value read in from the Console: - Values < 5 will generate a Normal Counter Object inclusive will generate a LowerLimit Object an UpperLimit Object - Values > 15 will produce one with both an Upper & Lower Limit

Nov 2005 MSc Slide 44 Exercise2: Given the class class Display{ private String name; public Display(String n){name=n;} public Display(Display dd){ name=dd.name;} public void print(){System.out.println("\t"+name);} public void setName(String n){name=n;} } Will Display a name: Now we want to decorate the Display (similar to Account Ex)

Nov 2005 MSc Slide 45 Normal Display Athlone UpperLine ******************* Athlone LowerLine Athlone ********************* Upper and Lower Line ******************* Athlone *********************

Nov 2005 MSc Slide 46 (a) class UpperLine extends Decorator{ print a line of ‘*’ before name (b) class LowerLine extends Decorator{ print a line of ‘*’ after name (c) Want a factory to decide on the of Display Type See next Dos screen

Nov 2005 MSc Slide 47

Nov 2005 MSc Slide 48