Factory(Design(Pa0ern(COMP(401(Spring(2013(Lecture(16(3/5/2013(Factory(Design(Pa0ern(• When(direct(construcCon(of(an(object(is(harmful(– …(or(at(least(undesired(– By(“direct(construcCon”,(I(mean(by(using(the(new(keyword(• Several(different(contexts(when(factory(design(pa0ern(is(appropriate:(– Singleton(• When(there(should(only(be(one(instance(of(a(parCcular(class(in(the(whole(system.(– MulCton(• When(there(should(only(be(one(instance(of(of(an(object(associated(with(some(idenCfying(property(– Dynamic(subclass(binding(• When(the(specific(subclass(of(an(object(can(only(be(determined(at(the(Cme(that(it(is(created(– Complex(construcCon(• When(the(construcCon(of(the(object(requires(complex(validaCon(and/or(side(effects(that(must(be(handled(• When(null(should(be(a(valid(result(The(Basic(Factory(Pa0ern(• Make(the(constructor(private(– This(prevents(direct(use(of(the(new(keyword(for(creaCng(new(objects(outside(of(the(class.(• Provide(staCc(class(methods(that(construct(new(instances(and(return(them.(– These(are(the(“factories”(– Return(type(of(the(factory(method(is(the(same(as(the(class.(class(FPClass({((private(FPClass()({(((//(Private(constructor((}(((public(staCc(FPClass(factoryMethod()({(((return(new(FPClass();((}(}(FPClass(o(=(FPClass.factoryMethod();(Singleton(One%Object%To%Rule%Them%All%• When(there(should(only(be(one(instance(of(a(parCcular(class(in(the(whole(system(at(any(given(Cme.(– Generally(the(object(represents(some(sort(of(systemawide(resource(that(may(be(needed(by(many(objects(in(different(parts(of(the(sobware.(– Modifies(basic(factory(pa0ern(by(maintaining(a(single(instance.(» Created(on(demand(when(first(requested(• Lazy(iniCaCon(» Stored(in(a(private(staCc(variable(and(retrieved(by(a(staCc(ge0er(• StaCc(ge0er(is(the(factory(method((• lec16.v1(– Simple(logging(mechanism.(• lec16.v2(– A(singleton(variant(that(employs(delegaCon.((MulCton(• Maintains(a(single(instance(of(the(object(with(regard(to(a(uniquely(idenCfying(characterisCc(of(object.(• MulCton(factory(method(– StaCc((as(per(the(general(factory(pa0ern)(– Provided(all(info.(needed(to(create(a(new(object(if(necessary.(– Determines(if(corresponding(object(already(exists.(• If(so,(returns(the(exisCng(object(• If(not,(creates(the(new(object(and(inserts(it(into(a(staCc(structure(• Usually(implemented(using(a(“map”(Map<K,V>(and(HashMap<K,V>(• Map(is(a(collecCon(of(key/value(pairs(– SomeCmes(called(a(“dicConary”(• Java(CollecCons(Framework(– Interface:(Map<K,V>(– ImplementaCon:(HashMap<K,V>(– K(and(V(are(placeholders(for(type(names(• K(for(the(type(of(the(key,(and(V(for(the(type(of(the(value.(• Must(be(reference(types(• Basic(operaCons:(– CreaCng(a(new(map(Map<K,V> m = new HashMap<K,V>()!– InserCng(a(value(associated(with(a(key(m.put(key, value);!– Retrieving(a(value(associated(with(a(key(V value = m.get(key);!– Checking(to(see(if(key(already(in(the(map(m.containsKey(key); // Returns boolean!– Removing(a(key/value(pair(from(the(map(m.remove(key)!MulCton(Example(• lec16.v3(– Student(is(a(class(that(models(students(• Uniquely(idenCfied(by(PID(property(– getStudent()(is(factory(method(• NoCce(that(all(info(needed(to(create(student(is(provided.(– lookupStudent()(is(a(factory(method(variant(that(only(retrieves(exisCng(objects.(• Does(not(create(object(if(it(doesn’t(exist((but(mu st(signal (that(f act)(• In(example,(this(is(done(by(returning(null(• What(would(another(opCon(h ave(been?(– lec16.v4(• Integer(reference(type(– Map<K,V>(can(only(work(with(reference(types(but(the(key(we(want(to(use(is(simple(integer(– Java(provides(reference(type(versions(of(basic(value(types(for(these(kinds(of(situaCons.(Dynamic(Subclass(Binding(• Useful(when(choice(needs(to(be(made(between(several(different(subclasses(– Delegates(decision(about(which(to(use(to(factory(• Factory(method(given(any/all(informaCon(that(is(relevant(to(decision(and(for(creaCng(new(object(if(necessary.(• lec16.v5((A(rose(by(any(other(name(• Some(design(pa0ern(textbooks(/(frameworks(associate(the(name(“Factory”(specifically(and(only(for(dynamic(subclass(binding(use(case.(– Other(cases(described(as(separate(pa0erns(• In(my(presentaCon,(I’ve(grouped(all(of(these(use(cases(under(the(name(“Factory”(more(generally.(–
View Full Document