Created
May 21, 2017 09:03
-
-
Save fermopili/9a9969f462cf6c139ea7a9f72bb49611 to your computer and use it in GitHub Desktop.
com.javarush.task.task37.task3711 Фасад
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3711; | |
/** | |
* Created by Administrator on 21.05.2017. | |
*/ | |
public class Computer | |
{ | |
private CPU cpu; | |
private Memory memory; | |
private HardDrive hardDrive; | |
public Computer() | |
{ | |
this.cpu = new CPU ( ); | |
this.memory = new Memory ( ); | |
this.hardDrive = new HardDrive ( ); | |
} | |
public void run() | |
{ | |
cpu.calculate ( ); | |
memory.allocate ( ); | |
hardDrive.storeData ( ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3711; | |
public class CPU { | |
void calculate() { | |
System.out.println("Making some CPU calculations..."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3711; | |
public class HardDrive { | |
void storeData() { | |
System.out.println("Storing data to HDD..."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3711; | |
public class Memory { | |
void allocate() { | |
System.out.println("Allocating additional RAM..."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task37.task3711; | |
/* | |
Фасад | |
*/ | |
public class Solution | |
{ | |
public static void main(String[] args) | |
{ | |
/* CPU cpu = new CPU ( ); | |
Memory memory = new Memory ( ); | |
HardDrive hardDrive = new HardDrive ( ); | |
cpu.calculate ( ); | |
memory.allocate ( ); | |
hardDrive.storeData ( ); | |
*/ | |
Computer computer=new Computer (); | |
computer.run (); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
taskKey="com.javarush.task.task37.task3711" | |
Фасад | |
Паттерн Фасад позволяет скрыть сложность системы путём сведения всех возможных | |
внешних вызовов к одному объекту, делегирующему их соответствующим объектам системы. | |
В данном случае, в классе Solution по отдельности создаются объекты типов CPU, Memory и HardDrive | |
и, в дальнейшем, на этих объектах вызываются некоторые методы. | |
Клиенту (в данном случае классу Solution), было бы намного удобнее, если бы существовал класс | |
Computer, инкапсулирующий создание объектов и предоставляющий единую точку взаимодействия с ними. | |
Проведем рефакторинг: | |
1) Создай класс Computer в котором создай и инициализируй поля типов CPU, Memory и HardDrive. | |
2) В классе Computer реализуй метод void run(), в котором вызови необходимые методы у объектов CPU, Memory и HardDrive. | |
3) Удали содержимое метода main класса Solution, после чего добавь туда создание нового компьютера и его запуск. | |
Требования: | |
1. В классе Computer должны быть созданы и инициализированы поля типов CPU, Memory и HardDrive. | |
2. В методе run класса computer должны быть вызваны методы calculate, allocate и storeData у объектов CPU, Memory и HardDrive. | |
3. В классе Solution должен быть создан новый объект типа Computer и вызван его метод run. | |
4. В классе Solution не должны явно создаваться объекты типов CPU, Memory и HardDrive. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment