90 likes | 216 Views
Roadmap. Generics Abstract Factory Annotations Model-Driven Engineering. 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
E N D
P2 — Clients and Servers Roadmap • Generics • Abstract Factory • Annotations • Model-Driven Engineering
P2 — 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
P2 — Clients and Servers Creational Patterns … deal with the best way to create objects
P2 — 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.
ConcreteFactory1 createProduct() ConcreteFactory2 createProduct() P2 — Clients and Servers Structure of the Abstract Factory Pattern AbstractFactory createProduct() Client AbstractProduct concreteProduct
P2 — Clients and Servers Abstract Factory with Generics: Ludo Example package ludo; // NOTE interface for "Abstract Factory" pattern. public interface Factory<T> { public T createInstance(); }
P2 — Clients and Servers The Concrete Factory package ludo; public class LudoFactory implements Factory<Ludo> { … public Ludo createInstance() { initializeHome(); initializeBranchings(); initializeMainRing(quarterLength); initializeHomeRuns(quarterLength); initializeNests(); return new Ludo(nest); } … We specify the type to be Ludo
«interface» AbstractFactory createInstance() : T «interface» Board addPlayer(Player ) … LudoFactory createInstance() : Ludo Ludo P2 — Clients and Servers The Ludo Example
P2 — 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.createInstance(); board.addPlayer(new Player()); board.addPlayer(new Player()); board.addPlayer(new Player()); } … We specify the Game Factory we want public class Main { public static void main(String[] args) { Runnable gameDriver = new Driver(new LudoFactory()); gameDriver.run(); }