Week 08 - The Abstract Factory DP.pdf - SOEN6461 Software...

This preview shows page 1 - 9 out of 36 pages.

This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 3.0 Unported License Amin Ranj Bar SOEN6461: Software Design Methodologies The Abstract Factory DP
2 Context From Encapsulation, Inheritance, Types, Overloading, Overriding, Polymorphism, and Abstraction
3 Context Take a sort algorithm, e.g. , an insertion sort package ca.concordia.soen6461; import java.util.List; public class InsertionSort<E> { public List<E> sort( final List<E> aList) { final List<E> temporaryList = null ; // Some implementation... return temporaryList; } } public class Client { public static void main( final String[] args) { final InsertionSort<String> s = new InsertionSort<String>(); final List<String> l = ; s.sort(l); } }
4 Context Take a sort algorithm, e.g. , an insertion sort package ca.concordia.soen6461; import java.util.List; public class InsertionSort<E> { public List<E> sort( final List<E> aList) { final List<E> temporaryList = null ; // Some implementation... return temporaryList; } } public class Client { public static void main( final String[] args) { final InsertionSort<String> s = new InsertionSort<String>(); final List<String> l = ; s.sort(l); } } Everything that InsertionSort offers is available to Client
5 Context Problems with InsertionSort Exposes the implementation of the sort What if we want to replace this implementation by another? Exposes the public interface of the class What if we want to share some public method with other library class BUT NOT with our clients?
6 Context We can “abstract” all sort algorithms package ca.concordia.soen6461; import java.util.List; public abstract class AbstractSort<E> { public abstract List<E> sort( final List<E> aList); public void someHelperMethod() { // Some implementation... } protected void someOtherHelperMethod() { // Some implementation... } } public class Client { public static void main( final String[] args) { final AbstractSort<String> s = new InsertionSort<String>(); final List<String> l = ; s.sort(l); } }
7 Context We can “abstract” all sort algorithms package ca.concordia.soen6461; import java.util.List; public abstract class AbstractSort<E> { public abstract List<E> sort( final List<E> aList); public void someHelperMethod() { // Some implementation... } protected void someOtherHelperMethod() { // Some implementation... } } public class Client { public static void main( final String[] args) { final AbstractSort<String> s = new InsertionSort<String>(); final List<String> l = ; s.sort(l); } } Only public methods of AbstractSort are available to Client
8 Context Advantage with the abstract class Code reuse Problems with AbstractSort Exposes the public interface of the class

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture