POLA-POLA PERANCANGAN (PPP) Creational patterns: Factory Method, Abstract Factory, Singleton

POLA-POLA PERANCANGAN (PPP) Creational patterns: Factory Method, Abstract Factory, Singleton Tujuan perkuliahan Memahami creational pattern: Factory...
8 downloads 0 Views 11MB Size
POLA-POLA PERANCANGAN (PPP) Creational patterns: Factory Method, Abstract Factory, Singleton

Tujuan perkuliahan Memahami creational pattern: Factory Method Memahami creational pattern: Abstract Factory Memahami creational pattern: Singleton

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

2/41

Mulai dari sini, seluruh peserta mata kuliah PPP akan mempelajari jenis – jenis PPP berdasarkan kategorisasi GoF Diperlukan kemampuan pemrograman yang mumpuni serta pemahaman tentang pemodelan dan pemrograman berorientasi objek yang cukup baik Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

3/41

Factory Method Intent: – Define an interface for creating an object, but let subclasses decide which class to instantiate – Factory Method lets a class defer instantiation to subclasses

Also known as: – Virtual Constructor

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

4/41

Factory Method – Motivation (1)

Design should be “open for extension but closed for modification”

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

5/41

Factory Method – Motivation (2)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

6/41

Factory Method – Motivation (3)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

7/41

Factory Method – Motivation (4)

Pizza factory Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

8/41

Factory Method – Motivation (5)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

9/41

Factory Method – Motivation (6)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

10/41

Factory Method – Applicability A class can't anticipate the class of objects it must create A class wants its subclasses to specify the objects it creates Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

11/41

Factory Method – Structure

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

12/41

Factory Method – Participants Product – defines the interface of objects the factory method creates

ConcreteProduct – implements the product interface

Creator – declares the factory method which returns object of type product – may contain a default implementation of the factory method – relies on its subclasses to define the factory method so that it returns an instance of the appropriate Concrete Product.

ConcreteCreator – overrides factory method to return instance of ConcreteProduct Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

13/41

Factory Method Collaborations: – Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct

Consequences: – Factory eliminates the need to bind applicationspecific classes into your code  only deals with the Product interface, therefore it can work with any userdefined Concrete-Product classes – A potential disadvantage is that clients might have to sub-class the Creator class just to create a particular ConcreteProduct object Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

14/41

Factory Method – Implementation Two main variations: – Creator is an abstract class and does not provide an implementation for factory method  requires subclasses to define an implementation, but you do not have to instantiate unforeseeable classes – Creator is a concrete class and provides a default implementation for factory method  the Creator uses the factory method primarily for flexibility, allowing sub-classes to change the class of objects their parent class instantiates if necessary

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

15/41

Factory Method – Implementation Parameterized factory methods: – Create multiple kinds of products based on the given parameter Creator { –class All objects the factory method creates share the Product FactoryMethod(ProductID id){ Product interface if(id==MINE) return new MyProduct(); if(id==YOURS) return new YourProduct();

} }

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

16/41

Factory Method – Sample Code (1)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

17/41

Factory Method – Sample Code (2)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

18/41

Factory Method – Sample Code (3)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

19/41

Factory Method – Sample Code (4)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

20/41

Factory Method – Sample Code (5)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

21/41

Factory Method – Sample Code (6)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

22/41

Factory Method – Sample Code (7)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

23/41

Abstract Factory Intent: – Define an interface for creating families of related or dependent objects without specifying their concrete classes

Also known as: – Kit

Motivation: – Consider pizzas produced by different stores – Each pizza must have good quality ingredients according to its respective store  different store has different set of ingredients – To be portable across different stores, pizza should not hard-code its ingredients for a particular store Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

24/41

Abstract Factory – Applicability A system should be independent of how its products are created, composed and represented A system should be configured with one of multiple families of products A family of related product objects is designed to be used together, and you need to enforce this constraint You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

25/41

Abstract Factory – Structure

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

26/41

Abstract Factory – Participants AbstractFactory – declares an interface for operations that create abstract product objects

ConcreteFactory – implements the operations to create concrete products

AbstractProduct – declares an interface for a type of product

ConcreteProduct – defines a product object to be created by the corresponding concrete factory – implements the AbstractProduct interface

Client – uses only the interfaces declared by AbstractFactory and AbstractProduct Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

27/41

Abstract Factory Collaborations: – Normally a single instance of a ConcreteFactory class is created at run-time  ConcreteFactory is implemented as a Singleton – AbstractFactory defers the creation of product objects to its ConcreteFactory sub-class, using the Factory Method pattern

Implementation: – Factories as Singletons: An application typically needs only one instance of a ConcreteFactory per product family  Singleton – Creating the Products: AbstractFactory only declares an interface for creating products  It is up to the ConcreteProduct sub-classes to actually create them Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

28/41

Abstract Factory – Consequences Abstract Factory isolates classes – It helps you to control the classes of objects that an application creates – It isolates clients from implementation classes as the client manipulates instances solely through their abstract interfaces

It makes exchanging product families easy  the class of a concrete factory appears only once in an application – that is where it is instantiated: – This makes it easy to change the concrete factory an application uses – It can use different product configurations simply by changing the ConcreteFactory Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

29/41

Abstract Factory – Consequences It promotes consistency among products: – When product objects in a family are designed to work together, it is important that the application use objects from only one family at a time

Supporting new kinds of products is difficult: – Extending abstract factories to produce new kinds of Products is difficult, because the AbstractFactory interface fixes the set of products that can be created – Supporting new products requires extending the factory interface, which involves changing the AbstractFactory class and all its ConcreteFactory subclasses Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

30/41

Abstract Factory – Sample Code (1)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

31/41

Abstract Factory – Sample Code (2)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

32/41

Abstract Factory – Sample Code (3)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

33/41

Abstract Factory – Sample Code (4)

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

34/41

Abstract Factory vs Factory Method AbstractFactory  Creator ConcreteFactory  ConcreteCreator AbstractProduct  Product Product  ConcreteProduct

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

35/41

Singleton Intent: – Ensure a class only has one instance, and provide a global point of access to it

Motivation: – Some classes should have exactly one instance (one print spooler, one file system, one window manager) – How do we ensure that a class has only one instance and that the instance is easily accessible? – A global variable makes an object accessible but doesn’t prohibit instantiation of multiple objects – Class itself should be responsible for keeping track of its sole instance Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

36/41

Singleton – Applicability There must be exactly one instance of a class, and it must be accessible to clients from a wellknown access point When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

37/41

Singleton – Structure

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

38/41

Singleton – Participants/Collaborations Participants: Singleton – Defines an instance operation that lets clients access its unique interface – Instance is a class operation (static in Java) – May be responsible for creating its own unique instance

Collaborations: Client – Accesses a Singleton instance solely through the Singleton’s Instance() method

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

39/41

Singleton – Consequences Controlled access to sole instance – Because the Singleton class encapsulates its sole instance, it can have strict control over how and when clients access it.

Reduced name space – The Singleton pattern is an improvement over global variables  It avoids polluting the name space with global variables that store sole instances.

Permits refinement of operations and representation – The Singleton class may be subclassed, and it's easy to configure an application with an instance of this extended class  You can configure the application with an instance of the class you need at run-time

Permits a variable number of instances – The pattern makes it easy to change your mind and allow more than one instance of the Singleton class Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

40/41

Singleton – Sample Code

Bahan Kuliah PPP - Creational patterns | Tri A. Kurniawan, S.T, M.T, Ph.D

41/41

Suggest Documents