Twin A Design Pattern for Modeling Multiple Inheritance Mahmoud ghorbanzadeh.

Slides:



Advertisements
Similar presentations
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
Advertisements

Chain of Responsibility Pattern Gof pp Yuyang Chen.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Inheritance Inheritance Reserved word protected Reserved word super
1 Objects and ClassesStefan Kluth 1.6Objects and Classes 1.6What is an Object? 1.7Objects and Classes 1.8Object Interface, Class Inheritance, Polymorphism.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Ch 13. Features Found Only in Java Timothy Budd Oregon State University.
Abstract Classes and Interfaces
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Unified Modeling Language
OOP Languages: Java vs C++
Composite Design Pattern. Motivation – Dynamic Structure.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Lecture 8: Object-Oriented Design. 8-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Good object-oriented programming is really.
6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance.
Pattern Hatching - John Vlissides Pages 85 – 101 Todd Anderson
Session 21 Chapter 10: Mechanisms for Software Reuse.
Inheritance in the Java programming language J. W. Rider.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Structural Design Patterns
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Session 13 Pinball Game Construction Kit (Version 3):
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Chain of Responsibility A graphical user interface and a user that needs help; a security system with multiple sensors; a banking automated coin storage.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Abstract Classes and Interfaces. Let’s say we are working on a video game together… Our job is to independently write two different enemies for the game.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
This computer science resource was developed through a collaboration between IBM Corporation and CSTA. 1 Lesson 2: Pong Class Design.
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.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Interfaces CMSC 202. Public Interfaces Objects define their interaction with the outside world through the their public interface. A class' public interface.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Modern Programming Tools And Techniques-I
Sixth Lecture ArrayList Abstract Class and Interface
EECE 310: Software Engineering
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Factory Patterns 1.
Interfaces.
Creational Pattern: Prototype
Software Design and Architecture
Object Oriented Analysis and Design
Design Patterns A Case Study: Designing a Document Editor
Behavioral Design Pattern
Software Design Lecture : 12.
BRIDGE PATTERN.
Software Design Lecture : 39.
Expanding the PinBallGame
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

Twin A Design Pattern for Modeling Multiple Inheritance Mahmoud ghorbanzadeh

Motivation  Twin allows a programmer to simulate multiple inheritance in languages which do not support this feature directly.  Multiple inheritance allows one to inherit data and code from more than one base class. 2

Applicability The Twin pattern can be used :  to simulate multiple inheritance in a language that does not support this feature.  to avoid certain problems of multiple inheritance such as name clashes. 33

44 Typical structure of multiple inheritance

Typical structure of the Twin pattern 55

Participants Parent1 (GameItem) and Parent2 (Thread)  The classes from which you want to inherit. Child1 (BallItem) and Child2 (BallThread)  The subclasses of Parent1 and Parent2. They are mutually linked via fields. 66

Collaborations  Every child class is responsible for the protocol inherited from its parent.  Clients of the twin pattern reference one of the twin objects directly (e.g. ballItem).  Clients that rely on the protocols of Parent1 or Parent2 communicate with objects of the respective child class (Child1 or Child2). 77

Consequences  There are several problems : 1.Subclassing the Twin pattern 2.More than two parent classes 88

 If the twin pattern should again be subclassed, it is often sufficient to subclass just one of the partners, for example Child1.  In order to pass the interface of both partner classes down to the subclass, it is convenient to collect the methods of both partners in one class. One can add the methods of Child2 also to Child1 and let them forward requests to the other partner. 9 Subclassing the Twin pattern 9

Subclassing a twin class. Child1.M2() forwards the message to Child2.M2(). 10 Subclassing the Twin pattern 10

 This solution has the problem that Sub is only compatible with Child1 but not with Child2.  If one wants to make the subclass compatible with both Child1 and Child2 one has to model it according to the Twin pattern again 11 Subclassing the Twin pattern 11

12 The subclass of Child1 and Child2 is again a Twin class 12

 The Twin pattern can be extended to more than two parent classes in a straightforward way.  For every parent class there must be a child class. All child classes have to be mutually linked via fields.  Although this is considerably more complex than multiple inheritance, it is rare that a class inherits from more than two parent classes. 13 More than two parent classes 13

More than two parent classes A Twin class derived from three parent classes 14

Implementation The following issues should be considered when implementing the Twin pattern: 1.Data abstraction : The partners of a twin class have to cooperate closely. They probably have to access each others' private fields and methods. Most languages provide features to do that. 1.Efficiency : multiple inheritance is anyway slightly less efficient than single inheritance. so that the additional run time costs of the Twin pattern are not a major problem. 15

Example  consider a computer ball game consisting of active and passive game objects. The active objects are balls that move across the screen at a certain speed. The passive objects are paddles, walls and other obstacles that are either fixed at a certain screen position or can be moved under the control of the user. 16

Class hierarchy of a computer ball game 17

 The basic idea is as follows: Instead of having a single class Ball that is derived from both GameItem and Thread, we have two separate classes BallItem and BallThread, which are derived from GameItem and Thread, respectively 18 Twin pattern 18

Twin pattern 19

Sample Code public class Gameboard extends Canvas { public int width, height; public GameItem firstItem; … } 20

Sample Code public abstract class GameItem { Gameboard board; int posX, posY; GameItem next; public abstract void draw(); public abstract void click (MouseEvent e); public abstract boolean intersects (GameItem other); public abstract void collideWith (GameItem other); public void check() { … } } 21

Sample Code public void check() { GameItem x; for (x = board.firstItem; x != null; x = x.next) if (intersects(x)) collideWith(x); } The method check() is a template method, which checks if this object intersects with any other object on the board. If so, it does whatever it has to do for a collision. 22

Sample Code public class BallItem extends GameItem { BallThread twin; int radius; int dx, dy; boolean suspended; public void draw() { … } public void move() { posX += dx; posY += dy; } public void click() {…} public boolean intersects (GameItem other) {…} public void collideWith (GameItem other) {…} } 23

A collision with a wall changes the direction of the ball, which can be implemented as : public void collideWith (GameItem other) { Wall wall = (Wall) other; if (wall.isVertical) dx = - dx; else dy = - dy; } 24 Sample Code 24

Sample Code public void click() { if (suspended) twin.resume(); else twin.suspend(); suspended = ! suspended; } 25

Sample Code  The class BallThread is derived from the standard class java.lang.Thread.  The implementation of other methods such as suspend() and resume() is inherited from Thread. public class BallThread extends Thread { BallItem twin; public void run() { while (true) { twin.draw(); /*erase*/ twin.move(); twin.draw(); } 26

Sample Code When a new ball is needed, the program has to create both a BallItem and a BallThread object and link them together, for example: public static BallItem newBall (int posX, int posY, int radius) { //method of GameBoard BallItem ballItem = new BallItem(posX, posY, radius); BallThread ballThread = new BallThread(); ballItem.twin = ballThread; ballThread.twin = ballItem; return ballItem; } 27

 The returned ball item can be linked into the list of game items in the game board. The corresponding ball thread can be started to make the ball move. 28 Sample Code 28

29