Introduction to OO Program Design

Slides:



Advertisements
Similar presentations
系統分析與設計 楊子青 H-1 H 、物件導向技術 n 物件導向的基本概念 – 物件、類別 – 封裝、繼承 – 同名異式 ( 多型 ) 、超荷 ( 過載 ) n 物件導向分析與設計及塑模工具 n UML 塑模工具.
Advertisements

3.2 Java的类 Java 类库的概念 语言规则——程序的书写规范 Java语言 类库——已有的有特定功能的Java程序模块
JAVA 编 程 技 术 主编 贾振华 2010年1月.
檔案處理.
Java Programming Hygiene - for DIDC
算法设计与分析 Algorithm Design and Analysis
第一章 面向对象程序设计.
程設一.
第二章 JAVA语言基础.
第二部分 Java语言基础篇 第4章 Java语言与面向对象 (之一).
Ch07 介面與多重繼承 物件導向程式設計(II).
第八章 分析與設計階段 – 物件導向設計(OOD)
创意源自激情,技术成就梦想 畅翼创新俱乐部 2012年春俱乐部第三次技术培训 赵程.
JAVA程序设计 (03) JAVA Programming
第5章 Java中类、对象、接口 及包的概念 5.1 类的基本概念 5.2 类的继承概念 5.3 抽象类和接口 5.4 包.
鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
Design Pattern (2) 设计模式(2) Institute of Computer Software 2018/9/21
程設一.
Design Patterns Lecture 4.
Android + JUnit 單元測試 建國科技大學資管系 饒瑞佶 2012/8/19V4.
2.1 基本資料型別 2.2 變數 2.3 運算式與運算子 2.4 輸出與輸入資料 2.5 資料型別轉換 2.6 實例
第5章 面向对象程序设计 本章要点 5.1 面向对象程序设计概述 5.2 Java语言的面向对象程序设计 5.3 方法的使用和对象数组
API设计实例分析 通用IO API.
H、物件導向技術 物件導向的基本概念 物件、類別 封裝、繼承 同名異式(多型) 、超荷(過載) 物件導向分析與設計及塑模工具 UML塑模工具.
物件導向程式設計 (Object-Oriented rogramming)
Java基础 JavaSE异常.
第六章 类的扩展与继承.
程式敘述執行順序的轉移 控制與重複、方法 Lecturer:曾學文.
Ch13 集合與泛型 物件導向程式設計(2).
1 巨集 2 資料型態 3 物件、屬性、方法與事件 4 陳述式與副函式 5 其他注意事項 6 範例
西南科技大学网络教育系列课程 高级语程序设计(Java) 第五章 继承、接口与范型.
JAVA程序设计 第5章 深入理解JAVA语言----补充.
程式設計實作.
CH09 套件 物件導向程式設計(II).
创建型设计模式.
周宇 南京航空航天大学 软件设计模式与体系结构 周宇 南京航空航天大学
软件建模训练(6-2) 设计模式 张静 南京理工大学计算机科学与工程学院.
程式撰寫流程.
职责链模式.
Java语言程序设计 第五部分 Java异常处理.
第9章 類別圖與物件圖 9-1 類別圖與物件圖的基礎 9-2 類別圖的符號 9-3 類別關係 9-4 物件圖 9-5 繪製類別圖與物件圖
Java程序设计 第9章 继承和多态.
王豐緒 銘傳大學資訊工程學系 問題:JAVA 物件檔輸出入.
第一次课后作业 1. C/C++/Java 哪些值不是头等程序对象 2. C/C++/Java 哪些机制采用的是动态束定
例外處理與 物件序列化(Exception Handling and Serialization of Objects)
檔案讀寫與例外處理 (File IO and Exception Handling)
组合模式.
2019/1/16 Java语言程序设计-类与对象 教师:段鹏飞.
集合框架和泛型(一).
C/C++/Java 哪些值不是头等程序对象
Chapter 5 Recursion.
简单工厂模式.
JAVA 编 程 技 术 主编 贾振华 2010年1月.
《JAVA程序设计》 语音答疑 辅导老师:高旻.
Speaker: Liu Yu-Jiun Date: 2009/4/29
第12章 组合模式 Deng Song
徐迎晓 复旦大学软件学院 实现模型 徐迎晓 复旦大学软件学院.
Interfaces and Packages
第二章 Java语法基础.

计算机问题求解 – 论题1-5 - 数据与数据结构 2018年10月16日.
第二章 Java基本语法 讲师:复凡.
方法進階及物件導向基礎 Lecturer: 楊昌樺.
RecyclerView and CardView
第6單元 6-1 類別的繼承 (Class Inheritance) 6-2 抽象類別 (Abstract Class)
第2章 Java语言基础.
Usage Eclipse 敏捷方法工具介紹 實驗室網站:
第二章 Java基础语法 北京传智播客教育
輸出執行結果到螢幕上 如果要將執行結果的文字和數值都「輸出」到電腦螢幕時,程式要怎麼寫? class 類別名稱 {
第6章 继承和多态 伍孝金
Presentation transcript:

Introduction to OO Program Design Software College of SCU Instructor: Shu Li Review

Unit 1 Class Design Basic Console I/O StringTokenizer Exception UML class diagram

Console I/O private static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); private static PrintWriter stdOut = new PrintWriter(System.out, true); private static PrintWriter stdErr = new PrintWriter(System.err, true); true, autoflush

StringTokenizer import java.util.*; StringTokenizer tokenizer = new StringTokenizer(data, "_"); tokenizer.countTokens(); //number of tokens while(tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); } // traverse each token

Exception (checked/unchecked) try { stdErr.print("Enter an integer > "); stdErr.flush(); // print without auto flush return Integer.parseInt(stdIn.readLine()); } catch (IOException ioe) { ioe.printStackTrace(); System.exit(1); // terminate the program } catch (NumberFormatException nfe) { stdErr.println("Invalid number format"); }

UML class diagram (Inheritance/Association) Solid line: extends Dashed line: implements

Unit 2 Class Implementation equals()/toString() Vector/ArrayList JDK 5 Vector<String> ArrayList<String> For each loop (Iterable interface/iterator method) JUnit Design Pattern

Vector and Iterator

methods defined in class Vector Vector(). Constructs an empty collection. int size(). Returns the number of objects in the collection. boolean isEmpty(). Determines if there are no objects in the collection. boolean contains(Object elem). Determines if the specified object is an element of the collection (as determined by the method equals). boolean add(E o). Appends the specified object to the end of the collection. void add(int index, E element). Inserts the specified object at the specified index position, shifting any subsequent elements to the right (adds one to their indices). E get(int index). Returns the object at the specified position. public E set(int index, E element). Replaces the element at the specified index position with the specified object. public boolean remove(Object o). Removes the first occurrence of the specified object (using method equals), shifting any subsequent elements to the left (subtracts one from their indices). E remove(int index). Returns the object at the specified position after first removing it from the collection and shifting any subsequent elements to the left (subtracts one from their indices).

Methods Defined in Class ArrayList ArrayList(). Constructs an empty collection. int size(). Returns the number of objects in the collection. boolean isEmpty(). Determines if there are no objects in the collection. boolean contains(Object elem). Determines if the specified object is an element of the collection (as determined by the method equals). boolean add(E o). Appends the specified object to the end of the collection. void add(int index, E element). Inserts the specified object at the specified index position, shifting any subsequent elements to the right (adds one to their indices). E get(int index). Returns the object at the specified position. public E set(int index, E element). Replaces the element at the specified index position with the specified object. public boolean remove(Object o). Removes the first occurrence of the specified object (using method equals), shifting any subsequent elements to the left (subtracts one from their indices). E remove(int index). Returns the object at the specified position after first removing it from the collection and shifting any subsequent elements to the left (subtracts one from their indices).

Code Demo See: LibrarySystem import java.util.*; public class Client { private String name; private Vector accounts; public Client(String initialName) { name = initialName; accounts = new Vector(); } public void addAccount(BankAccount bankAccount) { accounts.add(bankAccount); public Iterator getAccountIterator() { return accounts.iterator(); public int getNumberOfAccounts() { return accounts.size(); See: LibrarySystem

JDK 5 public class Client implements Iterable<BankAccount> { … private Vector<BankAccount> accounts; public Client(String initialName) { accounts = new Vector<BankAccount>(); } public Iterator<BankAccount> iterator() { return accounts.iterator(); for (BankAccount account : client) { totalBalance += account.getBalance();

Creating a test class in JUnit Define a subclass of TestCase Override the setUp() method to initialize object(s) under test. Override the tearDown() method to release object(s) under test. Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.

JUnit Methods assertEquals(x, y) – Test passes if x and y are equal x and y can be primitives or any type with an appropriate equals method Three argument versions exist for floating point numbers assertFalse(b) – Test passes if boolean value b is false assertTrue(b) – Test passes if boolean value b is true assertNull(o) – Test passes if object o is null assertNotNull(o) – Test passes if object o is not null assertSame(ox, oy) – Test passes if ox and oy refer to the same object assertNotSame(ox, oy) – Test passes if ox and oy do not refer to the same object

Design Pattern

Singleton Pattern Constructor private

Strategy Pattern

Composite Design Pattern To allow clients to treat both single components and collections of components identically To define recursive data structures such as trees uses Leaf operation() Component Composite add(Component) remove(Component) * Client 18 18

Decorator Pattern

Observer Pattern

Unit 3Advanced Class Implementation 3.1 Input and Output Programming

Abstract Classes InputStream & OutputStream Reader & Writer

Line-oriented I/O

Design Pattern - Factory Pattern Creational pattern One of the most used design patterns in Java. Create object without exposing the creation logic to the client and refer to newly created object using a common interface.

Factory Pattern - Demo FactoryPatternDemo, our demo class will use ShapeFactory to get a Shape object. It will pass information (CIRCLE / RECTANGLE / SQUARE) to ShapeFactory to get the type of object it needs.

Factory Pattern - Demo 1. create a Shape interface and concrete classes implementing the Shape interface. //Shape.java public interface Shape { void draw(); } //Square.java public class Square implements Shape { public void draw() { System.out.println("Inside Square::draw() method."); } //Rectangle.java public class Rectangle implements Shape { public void draw() { System.out.println("Inside Rectangle::draw() method."); } //Circle.java public class Circle implements Shape { public void draw() { System.out.println("Inside Circle::draw() method."); }

Factory Pattern - Demo 2.Create a Factory to generate object of concrete class based on given information. //ShapeFactory.java public class ShapeFactory { //use getShape method to get object of type shape public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE")){ return new Circle(); } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null;

Factory Pattern - Demo 3. Use the Factory to get object of concrete class by passing an information such as type. public class FactoryPatternDemo { public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory(); //get an object of Circle and call its draw method. Shape shape1 = shapeFactory.getShape("CIRCLE"); //call draw method of Circle shape1.draw(); //get an object of Rectangle and call its draw method. Shape shape2 = shapeFactory.getShape("RECTANGLE"); //call draw method of Rectangle shape2.draw(); //get an object of Square and call its draw method. Shape shape3 = shapeFactory.getShape("SQUARE"); //call draw method of circle shape3.draw(); }

Simple Factory 简单工厂模式是类的创建模式,又叫做静态工厂方法模式,是由一个工厂类根据传入的参量决定创建出哪一种产品类的实例,涉及到工厂角色、抽象产品角色以及具体产品角色。 一: 简单工厂类是整个模式的关键所在,包含了必要的逻辑判断,根据给定信息,决定究竟应该创建哪个类的对象。客户端可以免除直接创建产品对象的责任,而仅仅负责消费对象就可以了,这种做法就实现了对职责权利的分割,有利于优化结构。 二:工厂类集中了所有实例的逻辑,违反了高内聚这一原则,它所能创建的类只能是事先考虑好的,如果一旦添加新的类就需要改变工厂类了;同时,当具体产品增多时,可能就会出现工厂类根据不同的条件创建不同实例的需求,这种对条件的判断和对具体产品类型的判断交错在一起,容易产生错误,对系统的维护和扩展也不利。这些都在工厂方法模式中得到改善。

Factory Method 一:在工厂方法模式中,核心的工厂类不再负责所有产品的创建,而是将具体创建工作交给子类去完成,这就允许系统在不修改工厂角色的情况下,创建新的产品。 二:工厂方法模式集中封装了对象的创建,在更换对象时,不要做大的改变就可以实现,降低了客户程序与产品对象的耦合,是简单工厂模式的进一步抽象和推广。

Abstract Factory 抽象工厂是应对产品族概念的。比如说,每个汽车公司可能要同时生产轿车,货车,客车,那么每一个工厂都要有创建轿车,货车和客车的方法。 应对产品族概念而生,增加新的产品线很容易,但是无法增加新的产品。