代写代考 Property of Penn Engineering

Property of Penn Engineering

Copyright By PowCoder代写 加微信 powcoder

Property of Penn Engineering

1. Modeling: identify concepts and their relationships

2. Apply good architecture

3. Use appropriate data structures

4. Use design patterns when appropriate

5. Refactor (change the design) as necessary

How Do We Design Software?

Property of Penn Engineering

1. Modeling: identify concepts and their relationships

2. Apply good architecture

3. Use appropriate data structures

4. Use design patterns when appropriate

5. Refactor (change the design) as necessary

How Do We Design Software?

Property of Penn Engineering

• A general reusable solution to a commonly occurring

• A description or template for how to solve a problem that can
be used in many different situations.

What is a Design Pattern?

Property of Penn Engineering

• Creational: how objects are created

• Structural: how objects are composed

• Behavioral: how objects communicate

Categories of Design Patterns

Property of Penn Engineering

• “Separate the code that creates an object from the code that
uses an object.”

• Hide the implementation details of how objects are created.

• Benefits: stability, changeability

Creational Design Patterns

Property of Penn Engineering

• Goal: limit the number of instances of a class to one object

• Primary benefits:
• ensure that all code is using the same instance
• allow code to use the instance without having to create it

• Secondary benefits:
• simplify access to the single instance
• allow a class to control how it is instantiated

Problem Statement

Property of Penn Engineering

• Design pattern that ensures that there is only one instance
of a class, and that it can be accessed easily

• Approach:
• make the class’ constructor private
• expose a public static method that returns the singleton instance

Solution: of Penn Engineering

public class Logger {

private PrintWriter out;

// 1. private constructor
private Logger() {}

// 2. singleton instance
private static Logger instance = new Logger();

// 3. singleton accessor method
public static Logger getInstance() { return instance; }

: Logger.java (1)

Property of Penn Engineering

public class Logger {

// non-static method
public void log(String msg) {

out.println(msg);
out.flush();

: Logger.java (2)

Property of Penn Engineering

public class Logger {
private Logger() {}
private static Logger instance = new Logger();
public static Logger getInstance() { return instance; }
public void log(String msg) { . . . }

// other code that wants to write to the log file
Logger l = Logger.getInstance();
l.log(“this is a test”); // NOT Logger.log(“this is a test”)

Property of Penn Engineering

• To ensure that initialization has already been done in
constructor

• So that other fields and methods don’t also have to be static

• So that an instance of the Singleton can be passed as an
argument or returned by a method

Why Not Just Use Static Methods?

Property of Penn Engineering

• Creational design patterns: “Separate the code that creates an
object from the code that uses an object.”

• : Ensures that there is only one instance of a
class, and that it can be accessed easily
• make the class’ constructor private
• expose a public static method that returns the singleton instance

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com