.


:




:

































 

 

 

 


6.




 

GoF , . . .

:

1) . . ;

2) . ;

3) . ;

4) .

, . , .

, , , , .

Creator GRASP.

:

Abstract Factory ( ) (families of product objects);

Factory Method ( ) (subclass of object that is instantiated);

Builder () (how a composite object gets created);

Singleton () (the sole instance of a class);

Prototype () .

, , (class of object that is instantiated).

GoF , , .

:

Adapter () . (interface to an object);

Bridge () , (implementation of an object);

Composite () , (structure and composition of an object);

Decorator () . (responsibilities of an object without subclassing);

Facade () (interface to a subsystem);

Flyweight () (storage costs of objects);

Proxy () (how an object is accessed... its location).

GoF .

:

Chain of Responsibility ( ) - -, (object that can fulfill a request);

Command () , (when and how a request is fulfilled);

Iterator () , (how an aggregates elements are accessed, traversed);

Mediator () , , (how and which objects interact with each other);

Memento () (what private information is stored outside an object, and when);

Observer () (number of objects that depend on another object; how the dependent objects stay up to date);

State () (states of an object);

Strategy () (an algorithm);

Template Method ( ) , , (steps of an algorithm);

Visitor () , (operations that can be applied to object(s) without changing their class(es));

Interpreter () (grammar and interpretation of a language).

 

. Java Factory, Builder Chain of Responsibility.

Factory

, debug error . ILogger debug msg error msg, msg - , . , ILogger, ConsoleLoggerImpl FileLoggerImpl . Factory, getLogger FileLoggerImpl ConsoleLoggerImpl FileLogging = true ( false), logger.properties. true, getLogger FileLoggerImpl, ConsoleLoggerImpl. FactoryMethodPatternDemo Factory . . .

 

ILogger.java

package by.bsac.patterns.factory;

 

public interface ILogger {

// Write out a debug message

public void debug(String msg);

 

// Write out an error message

public void error(String msg);

}

 

ConsoleLoggerImpl.java

package by.bsac.patterns.factory;

 

public class ConsoleLoggerImpl implements ILogger {

 

ConsoleLoggerImpl() {} // Not accessible from other packages!

 

public void debug(String msg) {

System.out.println("DEBUG: " + msg);

}

 

public void error(String msg) {

System.err.println("ERROR: " + msg);

}

}

 

FileLoggerImpl.java

package by.bsac.patterns.factory;

 

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

 

public class FileLoggerImpl implements ILogger {

 

public void debug(String msg) {

try {

File file = new File("DebugLog.log");

 

// create file if doesn't exist

if (!file.exists()) {

file.createNewFile();

}

 

// true = append file

FileWriter fileWritter = new FileWriter(file.getName(), true);

BufferedWriter bufferWritter = new BufferedWriter(fileWritter);

bufferWritter.write("DEBUG:" + msg);

bufferWritter.close();

} catch (IOException e) {

e.printStackTrace();

}

}

 

public void error(String msg) {

try {

File file = new File("ErrorLog.log");

 

// create file if doesn't exist

if (!file.exists()) {

file.createNewFile();

}

 

// true = append file

FileWriter fileWritter = new FileWriter(file.getName(), true);

BufferedWriter bufferWritter = new BufferedWriter(fileWritter);

bufferWritter.write("ERROR:" + msg);

bufferWritter.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

 

Factory.java

package by.bsac.patterns.factory;

 

import java.io.IOException;

import java.util.Properties;

 

public class Factory {

 

public ILogger getLogger() {

if (isFileLoggingEnabled()) {

return new FileLoggerImpl();

} else {

return new ConsoleLoggerImpl();

}

}

 

// helper method, check if FileLogging is ON

// if so, log message to a file else print it to console.

private boolean isFileLoggingEnabled() {

Properties p = new Properties();

try {

p.load(getClass().getResourceAsStream("logger.properties"));

String fileLoggingValue = p.getProperty("FileLogging");

if ("true".equalsIgnoreCase(fileLoggingValue))

return true;

else

return false;

} catch (IOException e) {

return false;

}

}

}

 





:


: 2016-12-28; !; : 698 |


:

:

.
==> ...

1831 - | 1638 -


© 2015-2024 lektsii.org - -

: 0.027 .