P 2 Clients and Servers Roadmap Generics Abstract
P 2 — Clients and Servers Roadmap > Generics > Abstract Factory > Annotations > Model-Driven Engineering © O. Nierstrasz, O. Greevy, A. Kuhn 1
P 2 — Clients and Servers Design Patterns, a preview… “Each pattern describes a problem which occurs over and over again in our environment; and it describes the core of the solution to that problem, in such a way that you can use this solution a million times over…” — Christopher Alexander © O. Nierstrasz, O. Greevy, A. Kuhn 2
P 2 — Clients and Servers Creational Patterns … deal with the best way to create objects © O. Nierstrasz, O. Greevy, A. Kuhn 3
P 2 — Clients and Servers Abstract Factory Pattern > A Factory is the location in the code at which objects are created. > Provides an interface for creating families of related dependent objects. > Let clients create products in any family of products in an abstract way, without having to specify their concrete class. > Separates the details of implementation of a set of objects from its general usage. © O. Nierstrasz, O. Greevy, A. Kuhn 4
P 2 — Clients and Servers Structure of the Abstract Factory Pattern Abstract. Factory Client create. Product() Abstract. Product Concrete. Factory 1 create. Product() © O. Nierstrasz, O. Greevy, A. Kuhn Concrete. Factory 2 create. Product() concrete. Product 5
P 2 — Clients and Servers Abstract Factory with Generics: Ludo Example package ludo; // NOTE interface for "Abstract Factory" pattern. public interface Factory<T> { public T create. Instance(); } © O. Nierstrasz, O. Greevy, A. Kuhn 6
P 2 — Clients and Servers The Concrete Factory package ludo; public class Ludo. Factory implements Factory<Ludo> { … We specify the type to be Ludo public Ludo create. Instance() { initialize. Home(); initialize. Branchings(); initialize. Main. Ring(quarter. Length); initialize. Home. Runs(quarter. Length); initialize. Nests(); return new Ludo(nest); } … © O. Nierstrasz, O. Greevy, A. Kuhn 7
P 2 — Clients and Servers The Ludo Example «interface» Abstract. Factory create. Instance() : T Ludo. Factory «interface» Board add. Player(Player ) … Ludo create. Instance() : Ludo © O. Nierstrasz, O. Greevy, A. Kuhn 8
P 2 — Clients and Servers The Client drives the Ludo Game The Driver can drive any Board game public class Driver implements Runnable { private Board board; public Driver(Factory<? extends Board> factory) { this. board = factory. create. Instance(); board. add. Player(new Player()); } … We specify the Game Factory we want public class Main { public static void main(String[] args) { Runnable game. Driver = new Driver(new Ludo. Factory()); game. Driver. run(); } © O. Nierstrasz, O. Greevy, A. Kuhn 9
- Slides: 9