Академический Документы
Профессиональный Документы
Культура Документы
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
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
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
16
22
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