Вы находитесь на странице: 1из 26

Java Patterns

Sang Shin Java Technology Architect Sun Microsystems, Inc. sang.shin@sun.com www.javapassion.com

Topics
What is a pattern? Why patterns? Singleton pattern FactoryMethod pattern AbstractFactory pattern MVC pattern Observer pattern

What is a Pattern?

What is a Pattern?
Patterns are a software engineering problem solving discipline that emerged from the ob!ect oriented comm"nity

Why Patterns?
#t solves a problem$ Patterns capt"re sol"tions% not !"st abstract principles or strategies& #t is a proven concept$ Patterns capt"re sol"tions with a trac' record% not theories or spec"lation& #t describes a relationship$ Patterns don(t !"st describe mod"les% b"t describe deeper system str"ct"res and mechanisms&

Elements Of a Pattern
Pattern name Problem Sol"tion Conse)"ences

List of Common Patterns

Common Patterns
Singleton Factorymethod Abstract Factory MVC *Model View Controller+ Observer Facade

Singleton Pattern

Singleton Pattern
,sed when there is a need to have e-actly one instance of a class
> .-ample$ Window manager% Print spooler

.nforced by having a global point of access Allow m"ltiple instances in the f"t"re witho"t affecting a singleton class(s clients

10

Singleton Class
public class Singleton { private static Singleton singleton = new Singleton(); / ! private "onstructor prevents an# ot$er class %ro& instantiating' / private Singleton() { ( / Static )instance) &et$o* / public static Singleton get+nstance() { return singleton; ( // ot$er &et$o*s protecte* b# singleton,ness woul* be $ere''' / ! si&ple *e&o &et$o* / public String *e&o-et$o*() { return .*e&o.; ( (

11

Singleton Pattern Example


public class Singleton/attern { public static voi* &ain(String01 args) { Singleton s1 = Singleton'get+nstance(); S#ste&'out'println(."alling a &et$o* o% a Singleton2 . 3 s1'*e&o-et$o*()); Singleton s2 = Singleton'get+nstance(); S#ste&'out'println(."alling a &et$o* o% a Singleton2 . 3 s2'*e&o-et$o*()); boolean b1 = (s1 == s2); S#ste&'out'println(.4b5ect instance s1 an* s2 are t$e sa&e ob5ect2 . 3 b1); ( (

12

FactoryMetho Pattern

FactoryMetho Pattern
/i'e other creational patterns% it deals with the problem of creating ob!ects *prod"cts+ witho"t specifying the e-act class of ob!ect that will be created Factory method handles this problem by defining a separate method for creating the ob!ects% which s"bclasses can then override to specify the derived type of prod"ct that will be created More generally% the term factory method is often "sed to refer to any method whose main p"rpose is creation of ob!ects&

14

FactoryMetho Pattern
Common in tool'its and framewor's where library code needs to create ob!ects of types which may be s"bclassed by applications "sing the framewor' Factory Method pattern is a simplified version of Abstract Factory pattern
> Factory Method pattern is responsible of creating prod"cts

that belong to one family% while Abstract Factory pattern deals with m"ltiple families of prod"cts&

15

FactoryMetho Pattern

16

!"stract Factory Pattern

!"stract Factory Pattern


Provides a way to encaps"late a gro"p of individ"al factories that have a common theme #n normal "sage% the client software wo"ld create a concrete implementation of the abstract factory and then "se the generic interfaces to create the concrete ob!ects that are part of the theme 0he client does not 'now *nor care+ abo"t which concrete ob!ects it gets from each of these internal factories since it "ses only the generic interfaces of their prod"cts
18

!"stract Factory Pattern


0his pattern separates the details of implementation of a set of ob!ects from its general "sage 0ypically "ses FactoryMethod pattern "nderneath

16

!"stract Factory Pattern Example


An e-ample of this wo"ld be an abstract factory class DocumentCreator that provides interfaces to create a n"mber of prod"cts *eg& createLetter*+ and createResume*++ 0he system wo"ld have any n"mber of derived concrete versions of the DocumentCreator class li'e FancyDocumentCreator or ModernDocumentCreator% each with a different implementation of createLetter*+ and createResume*+ that wo"ld create a corresponding ob!ect li'e FancyLetter or ModernResume
20

!"stract Factory Pattern Example


.ach of these prod"cts is derived from a simple abstract class li'e /etter or 1es"me of which the client is aware 0he client code wo"ld get an appropriate instantiation of the DocumentCreator and call its factory methods .ach of the res"lting ob!ects wo"ld be created from the same DocumentCreator implementation and wo"ld share a common theme& *0hey wo"ld all be fancy or modern ob!ects&+
21

!"stract Factory Pattern Example


0he client wo"ld need to 'now how to handle only the abstract Letter or Resume class% not the specific version that it got from the concrete factory

22

!"stract Factory Pattern Example

27

M#C Pattern

Faca e Pattern

Faca e Pattern
#ntent 2 Provide a simple interface to a s"bsystem Motivation 2 A comple- system may have many pieces that need to be e-posed& 0his co"ld be conf"sing& S"pply a simple interface on top of the comple- system Participants 2 Facade% S"bsystemClasses

26

Вам также может понравиться