Design and Coding Patterns - PowerPoint PPT Presentation

About This Presentation
Title:

Design and Coding Patterns

Description:

Describe how classes and objects can be combined to form ... LIFO. Want Vector to look like a Stack. A. B. C. Push(C), push(B), push(A) Now: pop() - takes off A ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 34
Provided by: dra77
Category:
Tags: coding | design | lifo | patterns

less

Transcript and Presenter's Notes

Title: Design and Coding Patterns


1
Section 11_2
Design and Coding Patterns Adapter Pattern
2
Cat 2 Structural Patterns
Describe how classes and objects can be combined
to form larger structures. 2 Subcategories Cla
ss Patterns - based on inheritance Object
Patterns - based on composition
3
Adapter Patterns
- Change the interface associated with an
existing class 2 methods class Adapter Object
Adapter
4
Example existing ArrayList Class
int Add(object) - adds to end object
RemoveAt(int) remove element at
index Remove(object) bool Contains(object) in
t IndexOf(object) insert( int index, object
value ) int Count get
5
Want Vector to look like a Stack
LIFO
Push(C), push(B), push(A)
A B C
Now pop() - takes off A
B C
6
Want Vector to look like a Stack
Stack public boolean isEmpty () public object
pop() public boolean push(object obj) public
void print_list()
7
Class ArrayList int Add(object)
object RemoveAt(int) void
Remove(object) bool Contains(object)
int IndexOf(object) void
insert( int index, object value )
int Count get - must change to appear
like a Stack
8
1. Object Adapter
using System using System.Collections interfac
e Prototype_Stack bool isEmpty
() object pop() bool push(Object obj) void
print_list()
9
class Stack Prototype_Stack private
ArrayList data public Stack () data new
ArrayList () public bool isEmpty () if
(data.Count 0) return true else
return false public object
pop() object firstdata0 data.RemoveAt(0
) return first

10
public bool push(object obj)
data.Insert(0, obj) return
true public void print_list()
Console.WriteLine("of Elements") for (int
i0iltdata.Counti) Console.WriteLine(""
datai)

11
public class Teststack public static void
Main() Stack s new Stack() int
choice1 string name while(choice!4)
Console.WriteLine("") Console.WriteLine("1
push") Console.WriteLine("2 pop")
Console.WriteLine("3 print list")
Console.WriteLine("4 Exit")

12


Console.WriteLine("Enter Choice")
string tempConsole.ReadLine()
choiceConvert.ToInt32(temp) switch(choice)
case 1 Console.WriteLine("Enter
Name") nameConsole.ReadLine()
s.push(name)
break case 2 if (s.isEmpty()false)
name(String)
s.pop() Console.WriteLine("It
em poped "name) else
Console.WriteLine("Stack Empty")
break case 3 s.print_list()
break

13
(No Transcript)
14
(No Transcript)
15
2. Class Adapter
using System using System.Collections interfac
e Prototype_Stack bool isEmpty
() object pop() bool push(Object obj) void
print_list()
16
class Stack ArrayList,Prototype_Stack
public Stack ()base()
public bool isEmpty () if
(base.Count 0) return true else
return false public object
pop() object firstbase0 base.RemoveAt(0
) return first

17
public bool push(object obj)
base.Insert(0, obj) return
true public void print_list()
Console.WriteLine("of Elements") for (int
i0iltbase.Counti) Console.WriteLine(""
basei)
// main function unchanged

18
Final Example Assume the following class
exists using System using System.Collections c
lass Value private int number public
Value(int n)numbern public void step_up(int
v)numberv public void step_down(int
v)number-v public int read_val() return
number
19
Want to adapt this to behave like an Account
Account boolean deposit(int amt) boolean
withdraw(int amt) void print_details()
20
Object Adapter
class Value private int number public
Value(int n)numbern public void step_up(int
v)numberv public void step_down(int
v)number-v public int read_val() return
number interface Prototype_Account
bool deposit(int amt) bool withdraw(int
amt) void print_details()
21
class Account Prototype_Account
private Value v private string
name private int upper_limit public Account
(string n, int bal,int ul) v new
Value(bal) namen upper_limit
ul public bool deposit(int amt) int
c_balv.read_val() if ((amtc_bal)gtupper_limit
false)
v.step_up(amt) return
true else return false
22
public bool withdraw(int amt) int
c_balv.read_val() if (amtgtc_bal) return
false else v.step_down(amt)
return true public void
print_details() Console.WriteLine("Details"
) Console.WriteLine(" "name)
Console.WriteLine(" "v.read_val())

23
public class Ex2 public static void Main()
Account a new Account("J.Smith",10,100)
int choice1 int amount
while(choice!4) Console.WriteLine("")
Console.WriteLine("1 deposit")
Console.WriteLine("2 withdraw")
Console.WriteLine("3 check balance")
Console.WriteLine("4 Exit")

24
switch(choice)
case 1 Console.WriteLine("Enter Amount")
tempConsole.ReadLine()
amountConvert.ToInt32(temp)
bool resa.deposit(amount) if
(resfalse)
Console.WriteLine("Too
High") break case 2
Console.WriteLine("Enter Amount")
tempConsole.ReadLine()
amountConvert.ToInt32(temp) resa
.withdraw(amount) if (resfalse)

Console.WriteLine("Too Low") break
case 3 a.print_details() break
25
Class Adapter
26
class Account Value, Prototype_Account
private string name private int
upper_limit public Account (string n, int
bal,int ul)base(bal) namen upper_limit
ul public bool deposit(int amt) int
c_balbase.read_val() if ((amtc_bal)gtupper_li
mit false)
base.step_up(amt) return
true else return false

27
public bool withdraw(int
amt) int c_balbase.read_val() if
(amtgtc_bal) return false else
base.step_down(amt) return
true public void print_details()
Console.WriteLine("Details")
Console.WriteLine(" "name)
Console.WriteLine(" "base.read_val())

28
Exercise 1 Modify Stack Example so it behaves
like a Queue
Queue maxElements boolean isEmpty () boolean
isFull () Object delete() boolean insert(Object
obj) void print_list()
Note Queue is First in First Our Delete() like
Pop() except remove last element
29
Exercise 1 (continued) Need 2 Versions - Object
Adapter - Class Adapter Also have an additional
attribute maxElements If you to try to
insert more than maxElements you get an Error
Message
30
Exercise 2 Given the following existing class
class Value private int number public
Value(int n)numbern public void step_up(int
v)numberv public void step_down(int
v)number-v public int read_val() return
number
31
We want to adapt this functionality and provide
a new interface. An object of
type Counter should maintain an integer value.
This value may be incremented (stepped by 1)
up to an upper_limit. The value may
also be decremented, down to a minimum of 0.
Counter boolean increment() boolean
decrement() void print_details()
32
  • An object of type Counter should maintain an
    integer value.
  • This value may be incremented (stepped by 1) up
    to an upper_limit.
  • The value may also be decremented, down to a
    minimum of 0.
  • (i) Given these requirements show how the
    Value class may be
  • adapted to the desired interface using an
    Object Adapter pattern.
  • Outline UML Class and Interaction Diagrams
  • - A Complete Java Implementation

33
Q2 (continued)
ii)Briefly Highlight changes to the java code
that will translate it into an example of a
Class Adapter Pattern
Write a Comment
User Comments (0)
About PowerShow.com