31
LECTURE 15: PROGRAMMING & RELIGION Religious Studies 313 – Advanced Programming Topics

Lecture 15: Programming & Religion

  • Upload
    koto

  • View
    38

  • Download
    1

Embed Size (px)

DESCRIPTION

Religious Studies 313 – Advanced Programming Topics. Lecture 15: Programming & Religion. Today’s Goals. Going to hell/ lightning striking me down avoided Need for factories with other patterns shown What is problem creating pattern instances? Simple factory trashed after its discussed - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 15: Programming & Religion

LECTURE 15:PROGRAMMING & RELIGION

Religious Studies 313 – Advanced Programming Topics

Page 2: Lecture 15: Programming & Religion

Today’s Goals

Going to hell/lightning striking me down avoided

Need for factories with other patterns shown What is problem creating pattern instances?

Simple factory trashed after its discussed First, we will be reviewing how these work Situation it helps will be examined in detail And then highlight limitations of this

approach Solve the problem with Factory Method

pattern Show how it provides better, more general,

approach

Page 3: Lecture 15: Programming & Religion

Why Use Factories?

Pizza pie = new DeepDish();pie = new Garlic(pie);pie = new Garlic(pie);pie = new Onion(pie);

OnionGarlic

GarlicDDish

pie

Page 4: Lecture 15: Programming & Religion

Speaking of Pizza ToppingsOtto von Bismarck _____ are like

sausages. It's better not to see

them being made.

Page 5: Lecture 15: Programming & Religion

Simple Factory Pattern

Pizza pie = PizzaFactory.createPizza(type, toppings);

Pizza createPizza(String type, String[] toppings) {Pizza ret;if (type.equals(“DeepDish")) ret = new DeepDish();else ret = new Cracker();for (String s : toppings) { if (s.equals(“Garlic")) ret = new Garlic(ret); else if (s.equals(“Onion")) ret = new Onion(ret); else if (s.equals(“Fish")) ret = new Anchovy(ret);}return ret;

}

Page 6: Lecture 15: Programming & Religion

Simple Factory Overview

Page 7: Lecture 15: Programming & Religion

Simple Factory Overview

Instantiation in method to limit instantiation Often have entire class only with method Static or instance-based method can be

used* Call simple factory to replace new

command Method contains all new keeping client

code pure When change occurs, update factory

method only

Page 8: Lecture 15: Programming & Religion

Simple Factory Overview

Instantiation in method to limit instantiation Often have entire class only with method Static or instance-based method can be

used* Call simple factory to replace new

command Method contains all new keeping client

code pure When change occurs, update factory

method only

* Do not make this method static

Page 9: Lecture 15: Programming & Religion

Simple Factory Overview

Little design required to use Simple Factory Within program, much easier to add & use

classes Easy to write since need factory & classes

to use Code that most often needs to change is

isolated Client relies on abstraction simple factory

provides

Page 10: Lecture 15: Programming & Religion

Simple Factory UML

Client code calls method in SimpleFactory

AbstractProduct returned by this method Specific type unknown, really a ConcreteProduct

Client need not know about ConcreteProducts

Page 11: Lecture 15: Programming & Religion

In The Beginning…

Page 12: Lecture 15: Programming & Religion

In The Beginning…

Creator

Product

Page 13: Lecture 15: Programming & Religion

Problem with Simple Factory Approach requires single way to view

world Assimilates everything that comes into

contact with itPizza createPizza(String type, String[] toppings) {

Pizza ret;if (type.equals(“DeepDish")) ret = new DeepDish();else ret = new Cracker();for (String s : toppings) { if (s.equals(“Garlic")) ret = new Garlic(ret); else if (s.equals(“Onion")) ret = new Onion(ret); else if (s.equals(“Fish")) ret = new Anchovy(ret);}return ret;

}

Page 14: Lecture 15: Programming & Religion

Dependency Inversion

Design like you code; Start with the abstractions

Page 15: Lecture 15: Programming & Religion

Dependency Inversion

Design like you I code; Start with the abstractions

Page 16: Lecture 15: Programming & Religion

Dependency Inversion

Design like you I someone good codes; Start with the abstractions

Page 17: Lecture 15: Programming & Religion

Bottoms-Up!

Design like you I someone good codes; Start with the abstractions

Begin by finding what ideas have in common Use for interface or abstract class used by

classes With concepts, design client code & write

classes

Limit the damage others can do to you If design include concrete classes, ask

questions

Page 18: Lecture 15: Programming & Religion

Design to Concept, Not Class Simple factory created as need

arises Many related classes created for use

later Methods becoming bloated with options Room for growth wanted even if not

used These cases are all about classes

Means to an end is only reason for simple factory

Concrete classes not conceptualized or planned

No design work going into Simple Factory

Page 19: Lecture 15: Programming & Religion

Problem with Simple Factory createPizza entirely dependent on 6 classes

Must change whenever these constructors modified

Pizza createPizza(String type, String[] toppings) {Pizza ret;if (type.equals(“DeepDish")) ret = new DeepDish();else ret = new Cracker();for (String s : toppings) { if (s.equals(“Garlic")) ret = new Garlic(ret); else if (s.equals(“Onion")) ret = new Onion(ret); else if (s.equals(“Fish")) ret = new Anchovy(ret);}return ret;

}

Page 20: Lecture 15: Programming & Religion

What if…

Page 21: Lecture 15: Programming & Religion

Or We Had…

Page 22: Lecture 15: Programming & Religion

Simple Factory

public class PizzaFactory { Pizza createPizza(String type, String[] toppings) {

Pizza ret;if (type.equals(“DeepDish")) ret = new DeepDish();else ret = new Cracker();for (String s : toppings) { if (s.equals(“Garlic")) ret = new Garlic(ret); else if (s.equals(“Onion")) ret = new Onion(ret); else if (s.equals(“Fish")) ret = new Anchovy(ret); else ret = new Cheese(ret);}return ret;

}}

Page 23: Lecture 15: Programming & Religion

Factory Method Pattern

public abstract class PizzaFactory { Pizza createPizza(String type, String[] toppings) {

Pizza ret = createPizzaType();for (String s : toppings) { if (s.equals(“Garlic")) ret = new Garlic(ret); else if (s.equals(“Onion")) ret = new Onion(ret); else if (s.equals(“Fish")) ret = new Anchovy(ret); else ret = new Cheese(ret);}return ret;

} abstract Pizza createPizzaType();}

Page 24: Lecture 15: Programming & Religion

Factory Method Pattern

public class DeepDishFactory extends PizzaFactory { Pizza createPizzaType() {

return new DeepDish();}

}

public class ThinCrustFactory extends PizzaFactory { Pizza createPizzaType() {

return new Cracker();}

}

Page 25: Lecture 15: Programming & Religion

Factory Method UML

Clients call method in AbstractFactory ConcreteFactory unknown by the clients

Page 26: Lecture 15: Programming & Religion

Factory Method UML

Clients call method in AbstractFactory ConcreteFactory unknown by the clients

Page 27: Lecture 15: Programming & Religion

Factory Method Process

AbstractCreator defines factory Usually either an interface or abstract class Does not instantiate any actual Products

Instances allocated by ConcreteCreators Each Product has own ConcreteCreator

Develop lines using many ConcreteCreators DeepDishFactory & ThinCrustFactory needed To create product lines for DeepDish & Cracker

Page 28: Lecture 15: Programming & Religion

Factory Method Intent

Interface & abstract class types always used for: Variables Fields Parameters Statics

Any & all required concreteness left for: Factory methods Code on critical path (when performance is

critical)

Page 29: Lecture 15: Programming & Religion

Factory Method Intent

Interface & abstract class types always used for: Variables Fields Parameters Statics

Any & all required concreteness left for: Factory methods Code on critical path (when performance is

critical)

Code passing “What would _______ code?” test

Page 30: Lecture 15: Programming & Religion

Factory Method Intent

Interface & abstract class types always used for: Variables Fields Parameters Statics

Any & all required concreteness left for: Factory methods Code on critical path (when performance is

critical)

Code passing “What would _______ code?” test

Page 31: Lecture 15: Programming & Religion

For Next Lecture

Lab #3 still available on Angel Use Assignment Submitter before Fri. lab

(Feb. 24)

Read pages 144 - 162 in the book How can we easily enable skinnable

applications? Cars made in factories, but what pattern do

they use?